<?php

namespace MessagePack;

use PhpCsFixer\Config;
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Tokenizer\Tokens;

final class FilterableFixer implements FixerInterface
{
    private $fixer;
    private $pathRegex;

    public function __construct(FixerInterface $fixer, string $pathRegex)
    {
        $this->fixer = $fixer;
        $this->pathRegex = $pathRegex;
    }

    public function isCandidate(Tokens $tokens) : bool
    {
        return $this->fixer->isCandidate($tokens);
    }

    public function isRisky() : bool
    {
        return $this->fixer->isRisky();
    }

    public function fix(\SplFileInfo $file, Tokens $tokens) : void
    {
        $this->fixer->fix($file, $tokens);
    }

    public function getName() : string
    {
        return 'MessagePack/'.$this->fixer->getName();
    }

    public function getPriority() : int
    {
        return $this->fixer->getPriority();
    }

    public function supports(\SplFileInfo $file) : bool
    {
        if (1 !== preg_match($this->pathRegex, $file->getRealPath())) {
            return false;
        }

        return $this->fixer->supports($file);
    }
};

$header = <<<EOF
This file is part of the rybakit/msgpack.php package.

(c) Eugene Leonovich <gen.work@gmail.com>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

return Config::create()
    ->setUsingCache(false)
    ->setRiskyAllowed(true)
    ->registerCustomFixers([
        new FilterableFixer(new NativeConstantInvocationFixer(), '/\bsrc\b/'),
        new FilterableFixer(new NativeFunctionInvocationFixer(), '/\bsrc\b/'),
    ])
    ->setRules([
        '@Symfony' => true,
        '@Symfony:risky' => true,
        'array_syntax' => ['syntax' => 'short'],
        'binary_operator_spaces' => ['operators' => ['=' => null, '=>' => null]],
        'MessagePack/native_constant_invocation' => true,
        'MessagePack/native_function_invocation' => true,
        'no_useless_else' => true,
        'no_useless_return' => true,
        'ordered_imports' => true,
        'phpdoc_order' => true,
        'phpdoc_align' => false,
        'return_type_declaration' => ['space_before' => 'one'],
        'strict_comparison' => true,
        'header_comment' => [
            'header' => $header,
            'location' => 'after_declare_strict',
            'separate' => 'both',
        ],
    ])
;
