Alexandre Tranchant
Alexandre Tranchant

Reputation: 4551

How to limit PhpCsFixer to staged files?

I'm using PhpCsFixer on a project using Composer and Git for the version control.

When I launch PhpCsFixer, the tool updates all files. I'm searching solutions :

I already tried to create a command via composer composer fix-staged-files but it affects all files.

   ...
   "scripts": {
        ...
        "fix-staged-files": "php-cs-fixer fix --config=quality/.php_cs.dist --allow-risky yes ",
        ...
   }

Upvotes: 2

Views: 2614

Answers (2)

Alex Khonko
Alex Khonko

Reputation: 121

command:

$ php ./vendor/bin/php-cs-fixer fix --using-cache=no \
    --config .php-cs-fixer.dist.php  --diff --allow-risky=yes \
    --path-mode=intersection -- \
    $(git diff --name-only  --diff-filter=ACMRTUXB \
        $(git cherry origin/master | head -1 | awk '{print $2}')..HEAD)

or alias style:

# /etc/bash.bashrc

function ff(){
    php ./vendor/bin/php-cs-fixer fix --using-cache=no --config .php-cs-fixer.dist.php  --diff --allow-risky=yes --path-mode=intersection -- $(git diff --name-only  --diff-filter=ACMRTUXB $(git cherry origin/master | head -1 | awk '{print $2}')..HEAD)
}
export -f ff

Upvotes: 1

kuba
kuba

Reputation: 3870

There is a section explaining how to check only changed files: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v3.0.0/doc/usage.rst#using-php-cs-fixer-on-ci

But I've never used it, why not simply replying on cache? When you run fixer 1st time it builds a cache and every consecutive run checks only files that were modified since previous run.

Upvotes: 2

Related Questions