Easily comply with Symfony2 coding style using CodeSniffer and PhpStorm Code Inspection

19 June 2014

The goal of this article is to implement Symfony2 coding style in your PhpStorm editor. This will take you 5 minutes to configure your workspace.

Symfony2 CodeSniffer installation

You can follow the Opensky documentation to install the Symfony2 standard CodeSniffer

  1. Install phpcs:

    pear install PHP_CodeSniffer
    

    If you prefer using Composer, you can follow the PHP_CodeSniffer documentation to include it in your project dependencies.

  2. Find your PEAR directory:

    pear config-show | grep php_dir
    
  3. Copy, symlink or check out this repo to a folder called Symfony2 inside the
    phpcs Standards directory:

    cd /path/to/pear/PHP/CodeSniffer/Standards
    git clone git://github.com/opensky/Symfony2-coding-standard.git Symfony2
    
  4. Now, your CodeSniffer Standards directory should look like this:

    /usr/share/php/PHP/CodeSniffer/Standards $ ls -ld */
    drwxr-xr-x 4 root root 4096 juin  15 13:37 Generic/
    drwxr-xr-x 3 root root 4096 juin  15 13:37 MySource/
    drwxr-xr-x 4 root root 4096 juin  15 13:37 PEAR/
    drwxr-xr-x 2 root root 4096 juin  15 13:37 PHPCS/
    drwxr-xr-x 4 root root 4096 juin  15 13:37 PSR1/
    drwxr-xr-x 4 root root 4096 juin  15 13:37 PSR2/
    drwxr-xr-x 4 root root 4096 juin  15 13:37 Squiz/
    drwxr-xr-x 6 root root 4096 juin  15 13:42 Symfony2/
    drwxr-xr-x 4 root root 4096 juin  15 13:37 Zend/
    

PhpStorm configuration

  1. Configure PhpStorm to use phpcs

    Go to Project Settings (PHP > Code Sniffer). Use this command to find phpcs path: which phpcs and put the result on the PHP Code Sniffer path field. You can test your configuration by clicking on the validate button. Then don’t forget to click on the Apply button.
    phpcs configuration

  2. Configure PhpStorm inspection to use Symfony2 coding style

    Go to Project Settings (Inspection). Select PHP > PHP Code Sniffer validation inspection. In the right panel, you can choose your Coding standard. If Symfony2 does not appear in the drop-down list, click on the refresh button.
    inspection configuration

  3. Optional: Change the appearance of the inspection alerts

    Return in the PHP Code Sniffer inspection menu. Click on the button with three dots to Edit severities. In the new window, click on the + button to add a new one. Choose a name, PHPCS for example. And apply just a black background. You can do the same for warning validation.

  4. Enjoy!

    enjoy

A Pull Request return for Coding style error? Never!

I usually use the opensky Symfony2 coding style but what is yours?

Filed under : All,Programming — by @ 12 h 02 min

2 Comment »

  1. Dirk Luijk say :
    27 June 2014

    Nice article, thanks.

    15 h 34 min

  2. Lan say :
    4 July 2014

    Why do you use pear?

    I’m a PHP student and we learn Symfony.

    It’s a little bit stupid to install pear for phpcs, you can use composer!

    Furthermore, Isaw that Symfony will not use or support pear anymore.

    7 h 05 min


Leave an answer