jsnfwlr
jsnfwlr

Reputation: 3798

PHP-CS-FIXER is ignoring my config

I have created a file called .gm_cs in the root of my project and php-cs-fixer is installed globally.

In the config file is the following

<?php
 $rules = array(
     '@PSR2' => true,
     'combine_consecutive_unsets' => true,
     'no_extra_consecutive_blank_lines' => array('break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'),
     'no_useless_else' => true,
     'no_useless_return' => true,
     'ordered_class_elements' => true,
     'array_syntax' => array('syntax' => 'short'),
     'ordered_imports' => true,
     'phpdoc_add_missing_param_annotation' => true,
     'psr4' => true,
     'strict_comparison' => true,
     'strict_param' => true,
 );

 $fixers = array(
     '-pre_increment'
 );

 return PhpCsFixer\Config::create()->setRiskyAllowed(false)->setRules($rules)->fixers($fixers)->in(__DIR__);

I am calling the command from git-bash on windows with the following:
php-cs-fixer fix -vvv ./private --config-file=gm_cs

I have also tried these:
php-cs-fixer fix -vvv ./private --config-file=.gm_cs
php-cs-fixer fix -vvv ./private --config-file=./.gm_cs
php-cs-fixer fix -vvv ./private --config gm_cs
php-cs-fixer fix -vvv ./private --config .gm_cs
php-cs-fixer fix -vvv ./private --config ./.gm_cs
php-cs-fixer fix -vvv ./private --config=gm_cs
php-cs-fixer fix -vvv ./private --config=.gm_cs
php-cs-fixer fix -vvv ./private --config=./.gm_cs

In desperation I copied the .php_cs from my composer/vendor folder:

<?php
Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);

return Symfony\CS\Config::create()
    // use default SYMFONY_LEVEL and extra fixers:
    ->fixers(array(
        '-pre_increment',
    ))
    ->finder(
        Symfony\CS\Finder::create()
            ->exclude('Symfony/CS/Tests/Fixtures')
            ->in(__DIR__)
    )
;

And reduced the fixers to just stop the pre-increment one, and then ran it with all the commands listed previously, and it still hasn't solved the issue.

More than anything else, I just want it to stop swapping the $i++ to ++$i because this is a massive project I have inherited and I don't have time to test all those changes yet (no unit tests exist yet).

Any advice or help anyone could provide would be greatly appreciated.

Upvotes: 2

Views: 5019

Answers (2)

BentCoder
BentCoder

Reputation: 12750

Symfony independent example: http://www.inanzzz.com/index.php/post/m9yq/creating-a-standalone-composer-json-library-or-package

.php_cs

return PhpCsFixer\Config::create()
    ->setUsingCache(false)
    ->setRules([
        'array_syntax' => ['syntax' => 'short'],
        'ordered_imports' => true,
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->exclude(['bin', 'vendor'])
            ->in(__DIR__)
    );

Symfony dependent example:

.php_cs

$finder = Symfony\CS\Finder::create()
    ->exclude(['app', 'spec', 'build', 'bin', 'web', 'vendor'])
    ->in(__DIR__);

return  Symfony\CS\Config::create()
    ->fixers(['short_array_syntax', '-phpdoc_align', 'ordered_use'])
    ->finder($finder);

Example without .php_cs file:

$ bin/php-cs-fixer fix src --fixers=short_array_syntax

Upvotes: 1

jsnfwlr
jsnfwlr

Reputation: 3798

Turns out the version of php-cs-fixer I am using is the Symfony\CS\Fixer\ one, and it doesn't take $rules.

Additionally, the file needs to be named .php_cs

Upvotes: 3

Related Questions