The PHP_CodeSniffer ruleset to check that repositories are following the standards defined by the Doctrine team.
Doctrine Coding Standard is based on PSR-1 and PSR-2, with some noticeable exceptions/differences/extensions (:white_check_mark: are the implemented sniffs):
- Keep the nesting of control structures per method as small as possible
- Prefer early exit over nesting conditions or using else
- ✅ Align equals (
=) signs in assignments - ✅ Add spaces around a concatenation operator
$foo = 'Hello ' . 'World!'; - ✅ Add spaces between assignment, control and return statements
- ✅ Add spaces after a negation operator
if (! $cond) - ✅ Add spaces around a colon in return type declaration
function () : void {} - ✅ Add spaces after a type cast
$foo = (int) '12345'; - ✅ Use apostrophes for enclosing strings
- ✅ Always use strict comparisons
- ✅ Always add
declare(strict_types=1)at the beginning of a file - ✅ Always add native types where possible
- ✅ Omit phpDoc for parameters/returns with native types, unless adding description
- ✅ Don't use
@author,@sinceand similar annotations that duplicate Git information - ✅ Assignment in condition is not allowed
For full reference of enforcements, go through lib/Doctrine/ruleset.xml where each sniff is briefly described.
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:~2.0.0Then you can use it like:
$ ./vendor/bin/phpcs --standard=Doctrine /path/to/some/file/to/sniff.phpYou might also do automatic fixes using phpcbf:
$ ./vendor/bin/phpcbf --standard=Doctrine /path/to/some/file/to/sniff.phpYou can also install the Doctrine Coding Standard globally:
$ composer global require doctrine/coding-standard:~2.0.0Then you can use it like:
$ phpcs --standard=Doctrine /path/to/some/file/to/sniff.phpYou might also do automatic fixes using phpcbf:
$ phpcbf --standard=Doctrine /path/to/some/file/to/sniff.phpThis library follows semantic versioning, and additions to the code ruleset are only performed in major releases.
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