stesie / phpcs-doctrine-annotation-rules
Additional PHP Code Sniffer sniffs focusing on annotations for Doctrine ORM.
Installs: 50 907
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: ^7.0
- doctrine/annotations: ^1.4
- doctrine/orm: ^2.5
- squizlabs/php_codesniffer: ^3.0
Requires (Dev)
- consistence/coding-standard: ^2.0
- jakub-onderka/php-console-highlighter: ^0.3.2
- jakub-onderka/php-parallel-lint: ^0.9.2
- phpstan/phpstan: ^0.8.0
- phpunit/phpunit: ^6.0.3
- satooshi/php-coveralls: ^1.0
- slevomat/coding-standard: ^3.3
This package is not auto-updated.
Last update: 2024-11-10 04:07:02 UTC
README
Doctrine Annotation Coding Standard for PHP_CodeSniffer provides some additional sniffs centered on DocBlock annotations for Doctrine ORM.
Sniffs included in this standard
🔧 = Automatic errors fixing
DoctrineAnnotationCodingStandard.Commenting.ImplicitNullableJoinColumn
Applies to DocBlocks of properties that are mapped as either @ORM\ManyToOne
or @ORM\OneToOne
.
- Checks for missing
@ORM\JoinColumn
annotation - If
@ORM\JoinColumn
exists, checks ifnullable
is implicitly assumed to betrue
The default value of nullable
of @ORM\JoinColumn
is true
(as opposed to @ORM\Column
),
which many DEVs are unaware of and hence have NULL-able associations where they should not have ones.
This sniff ensures that the nullable-choice is made explicitly.
DoctrineAnnotationCodingStandard.Commenting.VarTag 🔧
Applies to all DocBlocks of Doctrine-mapped properties.
- Checks for missing
@var
tag - Checks the type stated by
@var
against actual type (according to Doctrine mapping)
This sniff supports automatic fixing with phpcbf
.
Sniff provides the following settings:
doctrineExtraTypes
: list of custom Doctrine types, that are mapped as strings (instead of objects)
Can be configured via ruleset.xml
like so:
<rule ref="DoctrineAnnotationCodingStandard.Commenting.VarTag"> <properties> <property name="doctrineExtraTypes" type="array" value="CustomerType,CompanySizeType" /> </properties> </rule>
Installation
The recommended way to install Doctrine Annotation Coding Standard is through Composer.
$ composer require --dev stesie/phpcs-doctrine-annotation-rules
Keep in mind that this is not a full coding standard, it just augments existing ones with extra checks on Doctrine annotations. If unsure, I highly recommend having a look at Slevomat Coding Standard.
Using the standard as a whole
Simply mention this (additional) standard in ruleset.xml
:
<?xml version="1.0"?> <ruleset name="AcmeProject"> <rule ref="vendor/stesie/phpcs-doctrine-annotation-rules/src/DoctrineAnnotationCodingStandard/ruleset.xml" /> <!-- additional standards like slevomat --> </ruleset>
To check your code base for violations, run PHP_CodeSniffer
from the command line:
vendor/bin/phpcs --standard=ruleset.xml --extensions=php -sp src tests
Fixing errors automatically
Sniffs in this standard marked by the 🔧 symbol support automatic fixing of coding standard violations. To fix your code automatically, run phpcbf insteand of phpcs:
vendor/bin/phpcbf --standard=ruleset.xml --extensions=php -sp src tests