Git hook running PHP CS Fixer on staged files using CaptainHook
Report Bug
·
Request Feature
Git hook that with each git commit
command runs Php Cs Fixer
on staged files to automatically fix them and re-stage before committing.
- PHP >= 8.0
- CaptainHook >= 5.0
- PHP CS Fixer
- Install package as a dev dependency using composer
composer require --dev unh3ck3d/php-cs-fixer-git-hook
- Add the following code to your
captainhook.json
configuration file{ "pre-commit": { "enabled": true, "actions": [ { "action": "\\Unh3ck3d\\PhpCsFixerGitHook\\LintStagedFiles" } ] } }
- Install newly added hook by following CaptainHook docs
That's it. From now on after running git commit
files that were staged will be
automatically fixed by Php Cs Fixer.
You can customize the behaviour how git hook is run by changing following options
Option | Description |
---|---|
phpCsFixerPath | Path to Php Cs Fixer executable. Defaults to ./vendor/bin/php-cs-fixer . |
pathMode | path-mode cli option of Php Cs Fixer. Defaults to intersection . |
config | config cli option of Php Cs Fixer. Defaults to .php-cs-fixer.dist.php . |
additionalArgs | String of additional arguments that will be passed to Php Cs Fixer process. |
e.g.
{
"pre-commit": {
"enabled": true,
"actions": [
{
"action": "\\Unh3ck3d\\PhpCsFixerGitHook\\LintStagedFiles",
"options": {
"phpCsFixerPath": "php-cs-fixer.phar",
"pathMode": "overwrite",
"config": ".php-cs-fixer.php",
"additionalArgs": "-v --dry-run --diff"
}
}
]
}
}
Distributed under the MIT License. See LICENSE
for more information.