in 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. 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.

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email