Drupal Coding Standards, with Mamp, Code Sniffer and Php Storm
UPD Sept 2017: The following original article should be considered outdated and obsolete, due the preference to be given to the use of Composer over Pear in PHP Code sniffer implementation. It is thus adviced to better follow the instructions described here: How to use PHP Code Sniffer in PhpStorm for Drupal development from https://justdrupal.com
The following passages are a synthesis of the best practices I found on the web (related links mentioned at this bottom).
This outcome is my preferred workflow, that I would suggest for setting up Php Storm IDE for being properly assisted in writing code compliant with Drupal Coding Standards,
both for Drupal 7 and Drupal 8.
Path examples are given for a typical MAMP installation. They might vary on other Stack installations, but the concepts remain valid.
Installing Php Code Sniffer (Phpcs) with Pear
If you are using MAMP (that works both for Mac and Windows now), PEAR should be (it is) bundled in each PHP provided packages.
First of all, you might ensure that PEAR is installed on your machine.
From the terminal running
$ which pear
will tell you where pear is installed on your machine.
On a current MAMP installation this will be in
(phpX.X.X being the specific php version used/selected by MAMP).
It will simplify things for you if you create a symlink to the executable.
$ sudo ln -s /Applications/MAMP/bin/php/phpX.X.X/bin/pear /usr/local/bin/pear
$ pear config-show
will show the full pear configuration on your screen)
PEAR will install bin files like phpcs into a configurable location. You can see this location by running
$ pear config-show | grep bin_dir
You might want to just set it to /Applications/MAMP/bin or somewhere local on your Mac, like /usr/local/bin, or even better in the php_bin MAMP specific folder/version: /Applications/MAMP/bin/php/phpX.X.X/bin
Before you do this, it is worth uninstalling (and properly reinstall) PHP_CodeSniffer:
$ pear uninstall PHP_CodeSniffer
$ pear config-set php_bin /Applications/MAMP/bin/php/phpX.X.X/bin/php
$ pear config-set bin_dir /Applications/MAMP/bin/php/phpX.X.X/bin
$ pear install PHP_CodeSniffer
(check that the paths are valid first and that you don't have friendlier symlink versions)
You need the php_bin to be correct because PEAR will change the #! line of all bin files (like phpcs) on install to point to your PHP executable.
The bin_dir is important to ensure bin files are placed into a directory that is in your path.
CodeSniffer will be easier to use if you add a symlink for this executable as well.
$ sudo ln -s /Applications/MAMP/bin/php/phpX.X.X/bin/phpcs /usr/local/bin/phpcs
You can test your installation by typing
$ phpcs --version
Customizing for Drupal: Coder and PHP Code Sniffer Integration
The next step is to make the Drupal standards available to codesniffer (phpcs).
The Standards can be accessed extracting them from the Drupal Coder module (Drupal 7 or Drupal 8 version, depending on your needs).
Unpack the downloaded archive and find the coder_sniffer/Drupal subdirectory inside. You need to move the Drupal directory contents into the default Standards supplied by CodeSniffer.
To find this directory navigate to the directory identified by running
$ pear config-get php_dir
then navigate down to PHP/CodeSniffer/Standards.
On the standard MAMP setup this is /Applications/MAMP/bin/php/phpX.X.X/lib/php/PHP/CodeSniffer/Standards
Setting up Drupal Code Sniffer
To do this you need to:
- Download the Coder module
Note: download the 8.x branch, even if you intend to use it on Drupal 7.
You can download it in any 'normal' folder, but not in a Drupal project.
$ cd /folder/where/i/want/coder
$ drush dl coder
It should download the latest version which is 8.x - if it doesn't then add --select to the drush command and choose the 8.x branch.
- Add Drupal standards to PHP Code Sniffer
Tell phpcs to use the Drupal standards from the downloaded Coder module:
$ sudo phpcs --config-set installed_paths /folder/where/i/want/coder/coder/coder_sniffer
At this point you have PHP Code Sniffer set up with Drupal coding standards.
You can use it from command line by running:
$ phpcs --standard=Drupal file/to/check
or add it to PhpStorm.
Set up PHP Storm with Code Sniffer and Drupal Coding Standards
Finally you are able to set up PHP Storm with Code Sniffer to give extra warnings about Drupal coding standards violations.
To do this you need to copy/move the Drupal directory contents, present in the Coder module folder into the folder:
There should be ruleset.xml in this directory and some other subdirectories.
Once done this (and verified proper execution privileges on that ... CodeSniffer/Standards/Drupal folder and its contents),
simply open the Php Storm preferences, search for Sniffer -> Inspection -> PHP Code Sniffer Validation
In the Coding Standard Select, Update and choose Drupal …
For further and complete details on this, you can refer to the specific Jetbrains documentation section on Drupal Development using PhpStorm