How to limit PhpCsFixer to staged files?

Viewed 924

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 ",
        ...
   }
2 Answers

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
Related