Paulund
2017-08-23 #laravel

PHP Code Sniffer And Fixer

In this tutorial we're going to learn how we can install code sniffer and a fixer to help us keep to a coding standard. Coding standards are a set of rules that your code must follow, having consistency in your coding will make it easier to read and handle in the long term. When it comes to coding standards in PHP it's important to follow the rules laid out by PHP-FIG.

The coding standard I use is PSR-2 so I want to make sure that my code sticks to this coding style and there's a number of tools we can use to ensure the code sticks to this standard. In this tutorial were going to investigate these tools and how you can apply these in you code editor.

What Is PSR-2?

The first rule of PSR-2 is that it will use all the rules laid out in PSR-1 with a couple more rules. You must use indents of 4 spaces and not tabs. There must not be a hard limit on line length but a soft limit of 120 characters. The must be a blank line after the namespace decoration. Opening brackets of classes must start on the next line. Opening brackets for methods must start on the next line. Opening brackets for conditions must start on the same line and must have a space before them and not after them. Here's example of these coding standards in a class


<?php
namespace Vendor\Package;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface
{
    public function sampleMethod($a, $b = null)
    {
        if ($a === $b) {
            bar();
        } elseif ($a > $b) {
            $foo->bar($arg1);
        } else {
            BazClass::bar($arg2, $arg3);
        }
    }

    final public static function bar()
    {
        // method body
    }
}

To view the full spec of PSR-2 follow this link for the complete coding standards. ## PHP_CodeSniffer

The tool we use to search for coding standards errors and fix them is called PHP_CodeSniffer, this is a set of two scripts phpcs and phpcbf. phpcs is a script that will read all your PHP code and identify any defects in your code, the tool phpcbf will then go through these coding defects and automatically correct them for you, ensuring you're keeping to the PSR-2. ### Install PHP_CodeSniffer

The easiest way to install PHP_CodeSniffer is with composer. If you want to install it globally you can use the command


composer global require "squizlabs/php_codesniffer=*"

Or you can install it on single projects by adding it to your composer.json file.


{
    "require-dev": {
        "squizlabs/php_codesniffer": "3.*"
    }
}

With PHP CodeSniffer installed in your vendor folder you can now run it on your code by using the following commands.


./vendor/bin/phpcs -h
./vendor/bin/phpcbf -h

This will bring up the help functionality of both scripts. To run the code sniffer on a certain file you can use the command


./vendor/bin/phpcs /path/to/code/myfile.php

Or you can run the code sniffer on an entire directory.


./vendor/bin/phpcs /path/to/code

Code Sniffer Configuration File

If you don't specify a file or directory for code sniffer it will search the current directory for a configuration file phpcs.xml that will explain what files to check and the coding standards you want to use. Below is phpcs.xml file that you can use on your existing Laravel projects.

<?xml version="1.0"?>
<ruleset name="Laravel Standards">

    <!--
       The name attribute of the ruleset tag is displayed
       when running PHP_CodeSniffer with the -v command line
       argument. The description tag below is not displayed anywhere
       except in this file, so it can contain information for
       developers who may change this file in the future.
    -->
    <description>The Laravel Coding Standards</description>

    <!--
    If no files or directories are specified on the command line
    your custom standard can specify what files should be checked
    instead.
    Note that specifying any file or directory path
    on the command line will ignore all file tags.
    -->
    <file>app</file>
    <file>config</file>
    <file>public</file>
    <file>resources</file>
    <file>routes</file>
    <file>tests</file>

    <!--
       You can hard-code ignore patterns directly into your
       custom standard so you don't have to specify the
       patterns on the command line.
    -->
    <exclude-pattern>*/database/*</exclude-pattern>
    <exclude-pattern>*/cache/*</exclude-pattern>
    <exclude-pattern>*/*.js</exclude-pattern>
    <exclude-pattern>*/*.css</exclude-pattern>
    <exclude-pattern>*/*.xml</exclude-pattern>
    <exclude-pattern>*/*.blade.php</exclude-pattern>
    <exclude-pattern>*/autoload.php</exclude-pattern>
    <exclude-pattern>*/storage/*</exclude-pattern>
    <exclude-pattern>*/docs/*</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>
    <exclude-pattern>*/migrations/*</exclude-pattern>

    <!--
       You can hard-code command line values into your custom standard.
       Note that this does not work for the command line values:
       -v[v][v], -l, -d, -sniffs and -standard
       The following tags are equivalent to the command line arguments:
       -p
    -->
    <arg name="report" value="summary"/>
    <arg name="colors"/>
    <arg value="p"/>

    <!--
       You can hard-code custom php.ini settings into your custom standard.
       The following tag sets the memory limit to 64M.
    -->
    <ini name="memory_limit" value="128M"/>

    <!--
       Include all sniffs in the PEAR standard. Note that the
       path to the standard does not have to be specified as the
       PEAR standard exists inside the PHP_CodeSniffer install
       directory.
    -->
    <rule ref="PSR2"/>

</ruleset>

When this is added into your project root folder all you need to do to run it is the following commands.


./vendor/bin/phpcs
./vendor/bin/phpcbf

After you've ran the phpcbf command you'll notice that your PHP files will now follow the PSR-2 coding standard.