Helpers for PHP Code Sniffer.
composer require --dev nelson6e65/code-sniffer-helpers
A wrapper to fix your staged code (or argumented files/folders) using the PHP Code Sniffer auto-fixer.
There is a bug that does not allows you to use it directly as autofixer (squizlabs/PHP_CodeSniffer#1818). There is a workarround for using it as a composer script, but does not works for using it in a lint-staged pre-commit hook.
This helper is designed to be run with lint-staged, but you can also use it directly in your composer script.
Add the script to your composer.json:
{
"scripts": {
"cs:fix-filtered": ["NelsonMartell\\PhpCodeSniffer\\ComposerScripts::phpcbf"]
}
}
I used
"cs:fix-filtered"
name, but you can use any script name you like.
Configure your Husky + lint-staged in your package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.php": "composer cs:fix-filtered"
}
}
Example for Husky 4. Adapt it if you use Husky 5.
You can also run it directly with composer by using composer cs:fix-filtered {PATHS}
. Example:
composer cs:fix-filtered src/ tests/ config/my-config-file.php
Note: Non exixtent files/directories are ignored.
The output is inspired on pretty-quick output:
composer cs:fix-filtered config/ src/Example.php src/non-existent-file.php
Copyright (c) 2021 Nelson Martell
Read the LICENSE
file for details.