Magento 2 seems to include a set of PHP code sniffs in the source repository. How can I, as a third party developer, use the phpcs
tool to run these sniffs against my code?
3 Answers
It's the PhpCsFixer configuration file, not the code sniffer rules itself
Instead of using command line options to customize the rule, you can save the project configuration in a .php_cs.dist file in the root directory of your project. The file must return an instance of PhpCsFixer\ConfigInterface which lets you configure the rules, the files and directories that need to be analyzed.
Example of command:
php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no
This fixer can be added to the pre-commit hook as well.
Ruleset for the code sniffer is in the ruleset.xml
-
1For anyone else that comes along -- php-cs-fixer is separate tool from phpcs and phpcbf Oct 4, 2017 at 16:07
You can use a pre-commit hook to check the staged files, there is one in:
dev/tools/Magento/Tools/StaticReview/pre-commit
Instead, if you want to test a specific file you can run the phpcs
executable inside vendor/bin
like this:
path/to/magento/vendor/bin/phpcs filetotest.php
and you can fix eventual errors with:
path/to/magento/vendor/bin/phpcbf filetotest.php
-
1Thank you fro responding -- but it's also unclear how you'd use run this pre-commit script against a PHP file? Oct 3, 2017 at 16:38
-
1
Sounds like you're asking to simply run the command all on your own. Try this. Remove --dry-run
to have it automatically apply the fixes.
php /path/to/php-cs-fixer --dry-run --diff --verbose fix /path/to/your/code --config /path/to/magento/.php_cs