PHP Code Sniffer

Introduction

PHP Code Sniffer (PHPCS) is a package for syntax checking where you can define the standard you wish your code to have, which makes it easier for your team to follow.

PHP Code Sniffer is a set of two PHP scripts, the main script phpcs tokenizes PHP, Javascript and CSS files to detect violations of the defined standard that is defined by us, and a second script phpcbf to automatically correct coding standard violations

Requirements

PHP Code Sniffer required PHP 5.4.0 or higher, although individual sniffs may have additional requirements.

Installation

  • Composer

If you use Composer, you can install Php Code Sniffer system wide with the following command:

composer global require “squizlabs/php_codesniffer=*”

wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
        wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

To test the downloaded PHAR’s

php phpcs.phar -h
php phpcbf.phar -h

Configuration file phpcs.xml

This is the file where we configurate what kind of coding standard we want to have, whether it be that every function should have a return type, the maximum length of a line, here is where it is configured, the following is a basic phpcs.xml with a few violation checks.

<?xml version="1.0"?>
<ruleset name="Lilly_CodeSniffer_OCPI">
    <description>Generic PSR12, with some additional enforcements.</description>


    <!-- Default arguments: Nice colours, only show errors //-->
    <arg name="colors"/>
    <arg value="np"/>


    <!-- Specify the directories and files that we want to check //-->
    <file>./src</file>


    <!-- Enforce PSR12 by default //-->
    <rule ref="PSR12">
        <!-- Tabs for indentation, spaces for alignment. Because we are not heathens, here. //-->
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    </rule>


    <!-- Are you still trying to indent using spaces? //-->
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>


    <!-- Enforce a type casting standard, must be (int)$var and not (int) $var //-->
    <rule ref="Generic.Formatting.SpaceAfterCast">
        <properties>
            <property name="spacing" value="0"/>
        </properties>
    </rule>


    <!-- Allows us to use our Resource versioning of things like V2_1_1 //-->
    <rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
        <severity>0</severity>
    </rule>


    <rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
        <severity>0</severity>
    </rule>


    <!-- Allows us to align header calls //-->
    <rule ref="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma">
        <severity>0</severity>
    </rule>
 <!--
    PSR12 is a little lax on line length, in that you could have a 10,000 line length if you wanted:


        - There MUST NOT be a hard limit on line length.
        - The soft limit on line length MUST be 120 characters.
        - Lines SHOULD NOT be longer than 80 characters; lines longer than that SHOULD be split into multiple subsequent
          lines of no more than 80 characters each.


    As such, we have a soft limit of 80 (which will show a warning), and a hard limit of 160 (which will produce an
    error). We should ideally be using lines under, or at, 80, but sometimes a line is just a long line and there's
    nothing you can do about it.


    You can always use <code>// phpcs:ignore</code> to tell CodeSniffer that the
    following line should be ignored. Or, if in a large block, you could you use the


    <code>
    // phpcs:disable
    $i_am_a_naughty_thing = "foo";
    continue_doing_naughty_things($i_am_a_naughty_thing);
    // phpcs:enable


    goodThingsAbound();
    </code>
    -->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="80"/>
            <property name="absoluteLineLimit" value="160"/>
        </properties>
    </rule>
</ruleset>



Execution

  • To execute the command to search for violations, you should run the command: php ./phpcs.phar, this searches for violations based on your definition and writes the violations out in the console
  • Some of them are available to be fixed automatically with the php ./phpcbf.phar command, the ones that are left over and not fixed need to be changed manually.