2

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 :

  • to limit fixes on the modified files,
  • to limit fixes on the staged files.

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 ",
        ...
   }
Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70

2 Answers2

2

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.

kuba
  • 3,670
  • 3
  • 31
  • 41
1

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
hakre
  • 193,403
  • 52
  • 435
  • 836
Alex Khonko
  • 121
  • 7
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 12 '22 at 00:45