The PHP_CodeSniffer ruleset to check that repositories are following the standards defined by the our team.
We use PSR-1 and PSR-2 with some exceptions/differences (:white_check_mark: are the implemented sniffs):
- Keep the nesting of control structures per method as small as possible
- Add spaces between assignment, control and return statements
- Prefer early exit over nesting conditions
- ✅ Align equals (=) signs
- ✅ Add spaces around a concatenation operator
$foo = 'Hello ' . 'World!';
- ✅ Add spaces around a negation if condition
if ( ! $cond)
- ✅ Add spaces around a return type declaration
function () : void {}
- ✅ Add spaces after a type cast
$foo = (int) '12345';
You have two possibilities to use the Doctrine Coding Standard with PHP_CodeSniffer in a particular project.
You can install the Doctrine Coding Standard as a composer dependency to your particular project. Just add the following block to your project's composer.json
file:
$ php composer require doctrine/coding-standard:~1.0
Then you can use it like:
$ ./vendor/bin/phpcs --standard=Doctrine /path/to/some/file/to/sniff.php
You might also do automatic fixes using phpcbf
:
$ ./vendor/bin/phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php
You can also install the Doctrine Coding Standard globally:
$ composer global require doctrine/coding-standard:~1.0
Then you can use it like:
$ phpcs --standard=Doctrine /path/to/some/file/to/sniff.php
You might also do automatic fixes using phpcbf
:
$ phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php
If you are contributing to the Doctrine Coding Standard and want to test your contribution, you just need to execute PHPCS with the tests folder and ensure it matches the expected report:
$ ./vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log