A few weeks ago I had to go through the process of setting up php code sniffer on my new computer, and realised how confusing most of the blog posts out there are and how many loops and posts you have to jump through to get it set up.
I decided to write a quick post with all the commands in one place and small descriptions for most of the commands:
Installing Drupal Coding Sniffer
1. Download php code sniffer (source code: https://github.com/squizlabs/PHP_CodeSniffer)
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
sudo mv phpcs.phar /usr/bin/phpcs
sudo mv phpcbf.phar /usr/bin/phpcbf
sudo chmod a+x /usr/bin/phpc*
Test that it's installed by running
phpcs -hand it should output the code sniffer help.
2. 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.
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.
3. 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 your favourite text editor/IDE.
Adding code sniffer to Sublime
Here are the few steps you need to follow to add it to Sublime Text 2/3:
1. Download the Sublime Build file from the repo: https://github.com/sirkitree/DrupalCodingStandard
wget https://raw.githubusercontent.com/sirkitree/DrupalCodingStandard/master/DrupalCodingStandard.sublime-build -O ~/.config/sublime-text-3/Packages/User/DrupalCodingStandard.sublime-build
If you don't know where your Sublime installation saves its packages then open Sublime, go to Preferences > Browse Packages, and replace the above path with yours.
2. Activate the Drupal Build file by going to Tools > Build System > DrupalCodingStandard in Sublime.
3. Open any Drupal file and hit Ctrl (Cmd) + B to run the sniffer on that file.
Adding code sniffer to PHPStorm
I have recently started using so am slowly getting used to it and setting up features I used to use in Sublime. Adding PHPCS to PHPStorm is simple and only takes a few steps.
Go into the Settings and either search for the keywords 'code sniffer' or go to Languages&Framerworks then Code Sniffer under the PHP section.
In the Development environment I have chosen Local and clicked on the ... next to the drop down. Add your /usr/bin/phpcs path to the phpcs path and click Validate to make sure it picks it up.
Now that you have phpcs added as a code sniffer we need to tell the 'Inspections' to use it.
In the same settings window, either search for 'code sniffer' again, or go directly to Editor > Inspections. Tick the box for PHP Code Sniffer validation under PHP and then choose the 'Coding standard' from the right hand pane. If the only values in the drop down are 'Custom' or you cannot find 'Drupal' in there then hit the little 'refresh' button next to the drop down and it should pull all the coding standards added to your php code sniffer. Then choose 'Drupal' from the drop down and you are good to go.
The code sniffer will start adding errors/warnings inline in Drupal files, or you can run a code inspection manually by going to Code > Inspect Code and choosing either the whole project or the current file.