UPD Dec 2016: Adjusted commands to match the current MacOS version.
Many of us can argue that a coding style should not be a thing of a personal preference, and each developer is free to choose whatever style he prefers if the program he’s written is running as expected. In the end, client is not paying for the style of your code, so why bother?
However, when it comes to the coding style, a few important caveats are often overlooked:
- In most cases, you will be writing code meant to be reviewed, modified and understood by other developers and not just you;
- It’s easy to maintain a codebase of 1000 lines, but it’s a completely different thing if it’s 10x> times larger;
- We’re all human. We make mistakes. We forget things. So why not to make the life of your future self simpler;
I can name more reasons why sticking to the coding style guide is important, but the key thing is — the code is not just a set of instructions for the machine to follow, it’s the communication tool. You can’t be effective in communication until you learn how to express yourself in a way that everyone understands.
But we’re also lazy, and learning and maintaining all possible standards can be tedious. Luckily enough it can be automated. Meet Code Sniffer.
PHP Code Sniffer validates your code for consistence with a coding standard and supplements the coding assistance built-in in your favorite IDE. Standards, or a set of rules, can be easily plugged-in and used independently or altogether.
In this short how-to we will install and configure PHP Code Sniffer on a Unix system (Mac OS or any Linux distro), install Drupal coding standards and make the PhpStorm do all the boring work for us, in background and completely automated.
There are several ways to install PHP_CodeSniffer, but I highly recommend to use Composer.
If you don’t have Composer globally installed, just do it like so (no sudo required):
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Next, detect and remove the PHP_CodeSniffer from your system if it was previously installed with PEAR or manually before. Believe me, it makes life so much easier and there will be no need to mess with /usr/share/pear/php/whatever deep folders just to install a standard.
Install the Code Sniffer library:
composer global require squizlabs/php_codesniffer=*
composer global require drupal/coder
PHP_CodeSniffer ships with a few useful standards, but lacks the Drupal coding standards for obvious reason. We’ve got them in
drupal/coder package already, now let’s copy them to the Code Sniffer folder. Note, PhpStorm allows to set a custom folder where the standards are located, but it didn’t work in my case (probably because of using the unstable version), so I’m doing the following. In your case it may not be needed.
cp -r ~/.composer/vendor/drupal/coder/coder_sniffer/Drupal ~/.composer/vendor/drupal/coder/coder_sniffer/DrupalPractice ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards
We’re basically copying the Drupal standards folders from the Coder to the Code Sniffer folder.
If all went well, it’s time to open the PhpStorm (or the compatible IDE).
In PhpStorm open Settings menu and go to Languages & Frameworks >> PHP >> Code Sniffer (quick tip — type «sniffer» in the settings search field).
Click on icon to the right of the Configuration box and the enter the full path (output of the
pwd command) to the PHP_CodeSniffer executable. In my case it was:
Press Validate and check that PhpStorm can recognize the library.
Now it’s time for configuration.
Open Editor >> Inspections settings menu and make sure you set the options as on my screen below:
The code smell, code style and other useful built-in inspections are worth reviewing and adjusting to your needs as well.
The Severity option allows you to choose whether to display the warnings inline as you write code (I recommend this option), or work in a silent mode. In the latter case you will have to invoke the inspection explicitly by going to Code >> Run Inspection by Name main menu item and choosing the «code sniffer» in the autocomplete popup.
The coding standard to choose is Drupal. You may also want to choose Drupal Practice standard in some cases. It originally came from this module http://dgo.to/drupalpractice and provides some useful extra checks to improve the code quality.
Now you’re fully empowered to write better code and be a better developer. Spread the word!