Easily comply with Symfony2 coding style using CodeSniffer and PhpStorm Code Inspection
June 19, 2014Jonathan Beurel2 min read
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.
composer global require "squizlabs/php_codesniffer=1.*"
You can follow the PHP_CodeSniffer documentation to include it in your project dependencies.
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:
phpcscommand, you must open a new shell or execute this command :
UPDATE (27 Sept 2014): The OpenSky Standards I have used seems to be no longer available. I use this repo instead:
Copy, symlink or check out the repo to a folder called Symfony2 inside the phpcs
cd ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards git clone email@example.com:escapestudios/Symfony2-coding-standard.git Symfony2
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
Configure PhpStorm to use phpcs
Project Settings(PHP > Code Sniffer). Use this command to find phpcs path:
which phpcsand put the result on the
PHP Code Sniffer pathfield. You can test your configuration by clicking on the
validatebutton. Then don't forget to click on the
Configure PhpStorm inspection to use Symfony2 coding style
Project Settings(Inspection). Select
PHP > PHP Code Sniffer validationinspection. 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.
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,
PHPCSfor example. And apply just a black background. You can do the same for warning validation.
A Pull Request return for Coding style error? Never!
I usually use the opensky Symfony2 coding style but what is yours?