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 this steps to install the Symfony2 standard CodeSniffer using Composer.

  1. Install phpcs:
    composer global require "squizlabs/php_codesniffer=*"

    You can follow the PHP_CodeSniffer documentation to include it in your project dependencies.

  2. Simply add the composer bin directory to your PATH in your ~/.bash_profile (or ~/.bashrc) by adding this line at the end of the file:
    PATH=$PATH:$HOME/.composer/vendor/bin
  3. Before use phpcs command, you must open a new shell or execute this command :
    source ~/.bash_profile
    
  4. UPDATE (27 Sept 2014): The OpenSky Standards I have used seems to be no longer available. I am currently looking for an alternative. You can choose one on this repo or another one (don’t hesitate to share with us) :

    • git@github.com:lapistano/Symfony2-coding-standard.git
    • git@github.com:qarea/QArea-Symfony2-coding-standard.git
    • git@github.com:escapestudios/Symfony2-coding-standard.git
    • git@github.com:FluidTYPO3/FluidTYPO3-CodingStandards.git


  5. Copy, symlink or check out the repo to a folder called Symfony2 inside the phpcs Standards directory:
    cd ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards
    git clone git@github.com:lapistano/Symfony2-coding-standard.git Symfony2
    
  6. Now, your list of installed coding standard should look like this:
    phpcs -i
    The installed coding standards are PSR1, PSR2, PHPCS, Zend, MySource, Squiz, Symfony2 and PEAR
    

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 : Programming — by @ 12 h 02 min

8 Comment »

  1. Dirk Luijk say :
    27 June 2014

    Nice article, thanks.

    15 h 34 min

  2. emilien say :
    17 September 2014

    The github link to opensky is broken

    9 h 37 min

  3. Robert Wildling say :
    17 September 2014

    Hi, thank you very much for this article!

    Unfortunately the OpenSky git repository seems to be no longer available. Which alternative do you recommend to use?

    11 h 15 min

  4. Robert Wildling say :
    17 September 2014

    AGain me – I actually had to install the repository to another location to make it work. The location on my Mac 10.9.4 is:

    Locate pear and get the php_dir:
    pear config-show | grep php_dir

    cd to that php_dir and move down to the folder PHP/CodeSniffer/Standards

    But then again: I use MAMP and changed ma path respectively…

    12 h 05 min

  5. Jonathan Beurel say :
    25 September 2014

    Hi Emilien and Robert.

    I’m currently looking for an alternative to the OpenSky Coding standards : https://twitter.com/JonathanBeurel/status/513987887717965824

    9 h 07 min

  6. Kevin S say :
    29 September 2014

    git clone git@github.com:lapistano/Symfony2-coding-standard.git Symfony2 requires the user to be authenticated,

    with :
    git clone https://github.com/lapistano/Symfony2-coding-standard.git Symfony2 not!

    Thanks for the tips!

    12 h 56 min

  7. Robert Wildling say :
    2 October 2014

    Hi, Jonathan,

    sorry,I read the note only today (no automatic mail information).

    Could you please check again, if your list up there mentioning TYPO3 really goes along with the Symfony2 standard? From what I saw, it absolutely does not.

    Thank you!
    Cheers!
    Robert

    20 h 01 min

  8. Christoph Föhrdes say :
    9 December 2014

    Thanks for the article! In our projects at LeapHub we also include our coding standard using composer, as we want a defined revision of the standard to be bound to every project.
    For this reason we started a fork of the Symfony2 standard of @lapistano as composer package several months ago.

    It has a stable v1.* where its usable with the PHP_CodeSniffer 1.* and the main branch was ported to PHP_CodeSniffer 2.* today. It might also be useful for one of you guys:

    http://packagist.org/packages/leaphub/phpcs-symfony2-standard

    Christoph

    17 h 40 min


Leave an answer