Doctrine Coding Standard
The PHP_CodeSniffer ruleset to check that repositories are following the standards defined by the Doctrine team.
Standards
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
- Abstract exception class names and exception interface names should be suffixed with
Exception
- ✅ Abstract classes should not be prefixed with
Abstract
- ✅ Interfaces should not be suffixed with
Interface
- ✅ Concrete exception class names should not be suffixed with
Exception
- ✅ 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
,@since
and similar annotations that duplicate Git information - ✅ Assignment in condition is not allowed
- ✅ Use parentheses when creating new instances that do not require arguments
$foo = new Foo()
- ✅ Use Null Coalesce Operator
$foo = $bar ?? $baz
- ✅ Use early return
For full reference of enforcements, go through lib/Doctrine/ruleset.xml
where each sniff is briefly described.
Installation
You have two possibilities to use the Doctrine Coding Standard with PHP_CodeSniffer in a particular project.
1. As a composer dependency of your 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:^3.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
2. Global installation
You can also install the Doctrine Coding Standard globally:
$ composer global require doctrine/coding-standard:^3.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
Versioning
This library follows semantic versioning, and additions to the code ruleset are only performed in major releases.
Testing
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