The idea is to go step by step through the repo and see how you can build up your custom sniff.
- Project setup - installing the phpcs using Composer
- Set up the sniffs folder and ruleset
- Add the basis of our sniff - showcase using
register
andprocess
methods - Add some tests and util packages like
PhpCSDebug
.- Note - you can use composer scripts to check your code and run tests
- Note - using
composer standards:check -- --standard=PhpCSDebug ./PhpCSExample/Tests/Strings/DisallowHelloWorldUnitTest.inc
will list all the tokens in the test file - this helps a lot when writing sniffs, just to get your bearings
- Update your sniff to catch some basic examples in tests
- Add the first edge case and a way to solve it (only the
echo 'hello', ' world';
part, the other edge case will fail, that's intentional!) - Add the fix for the edge case
- Add documentation for your custom sniff
- You can check the documentation using
vendor/bin/phpcs --standard=PhpCSExample --generator=Text
- You can check the documentation using
- Add a failing edge case - string concatenation - this one you can figure out how to cover in the sniff :)
You can use different reports when running phpcs:
vendor/bin/phpcs PhpCSExample/Tests/Strings/DisallowHelloWorldUnitTest.inc --standard=PhpCSExample --report=code
You can run the phpcs in interactive mode, and fix things on the fly:
vendor/bin/phpcs PhpCSExample/Tests/Strings/DisallowHelloWorldUnitTest.inc --standard=PhpCSExample -a
You can use filters (useful if you use git hooks):
vendor/bin/phpcs PhpCSExample/Tests/Strings/DisallowHelloWorldUnitTest.inc --standard=PhpCSExample --filter=GitModified
vendor/bin/phpcs PhpCSExample/Tests/Strings/DisallowHelloWorldUnitTest.inc --standard=PhpCSExample --filter=GitStaged
You can ignore annotations (// phpcs:ignore
comments) with --ignore-annotations
https://github.com/squizlabs/PHP_CodeSniffer/
https://packagist.org/?query=phpcs&type=phpcodesniffer-standard https://packagist.org/?query=php_codesniffer&type=phpcodesniffer-standard
https://github.com/PHPCSStandards/composer-installer
https://github.com/PHPCSStandards/PHPCSUtils https://github.com/PHPCSStandards/PHPCSDevTools