2017 © Pedro Peláez
 

library phpcs-doctrine-annotation-rules

Additional PHP Code Sniffer sniffs focusing on annotations for Doctrine ORM.

image

stesie/phpcs-doctrine-annotation-rules

Additional PHP Code Sniffer sniffs focusing on annotations for Doctrine ORM.

  • Monday, November 27, 2017
  • by stesie
  • Repository
  • 2 Watchers
  • 5 Stars
  • 5,982 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 30 % Grown

The README.md

Doctrine Annotation Coding Standard

Build Status Coverage Status PHPStan, (*1)

Doctrine Annotation Coding Standard for PHP_CodeSniffer provides some additional sniffs centered on DocBlock annotations for Doctrine ORM., (*2)

Sniffs included in this standard

:wrench: = Automatic errors fixing, (*3)

DoctrineAnnotationCodingStandard.Commenting.ImplicitNullableJoinColumn

Applies to DocBlocks of properties that are mapped as either @ORM\ManyToOne or @ORM\OneToOne., (*4)

  • Checks for missing @ORM\JoinColumn annotation
  • If @ORM\JoinColumn exists, checks if nullable is implicitly assumed to be true

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., (*5)

DoctrineAnnotationCodingStandard.Commenting.VarTag :wrench:

Applies to all DocBlocks of Doctrine-mapped properties., (*6)

  • 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., (*7)

Sniff provides the following settings:, (*8)

  • doctrineExtraTypes: list of custom Doctrine types, that are mapped as strings (instead of objects)

Can be configured via ruleset.xml like so:, (*9)

    <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., (*10)

$ 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., (*11)

Using the standard as a whole

Simply mention this (additional) standard in ruleset.xml:, (*12)

<?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:, (*13)

vendor/bin/phpcs --standard=ruleset.xml --extensions=php -sp src tests

Fixing errors automatically

Sniffs in this standard marked by the :wrench: symbol support automatic fixing of coding standard violations. To fix your code automatically, run phpcbf insteand of phpcs:, (*14)

vendor/bin/phpcbf --standard=ruleset.xml --extensions=php -sp src tests

The Versions