Install coder and php-codesniffer

Install Coder with Drush

First, make sure Drush is installed correctly:

$ which drush

If you get drush not found or similar, follow Drush's installation instructions.
Now install Coder to Drush's central store:

$ drush pm-download coder --destination=$HOME/.drush

Clear drush's internal cache so it sees Coder:

$ drush cache-clear drush

Note that Drush stored Coder's folder inside a hidden .drush folder in your home directory (e.g.: if your username is mparker17 and you use Linux, then Coder will be at /home/mparker17/.drush/coder). You'll need the path to Coder shortly.

Install CodeSniffer with PEAR

First, make sure PEAR is installed correctly:

$ which pear

If you get pear not found or similar, follow PEAR's installation instructions
Now install PHP CodeSniffer:

$ pear install PHP_CodeSniffer-3.0.0

Install CodeSniffer with Drush

If you've installed Coder through Drush, you can get Drush to install PHPCodeSniffer for you.

$ drush install-php-code-sniffer

Running CodeSniffer
Test PHP Codesniffer was installed correctly

To do this, just run:

$ phpcs --version

You should see something like PHP_CodeSniffer version 1.4.1 (stable) by Squiz ( Depending on how you installed it, you may have to give the full path instead of simply phpcs.

Path for php code sniffer.


  1. sudo ln -sv /home/username/.coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal

Running checks on a file or folder
$ phpcs --standard=Drupal /path/to/example.module

See also other usage examples for phpcbf etc. Of course that command is annoyingly long, so you can setup a shell alias (see below).

If you installed Coder with Drush, you can run:

$ drush drupalcs $file_or_folder_to_check

... where $file_or_folder_to_check is a path to a folder containing PHP / CSS / JS code to check against Drupal standards. You can use . to check the current directory.

If that doesn't work, or you didn't install Coder with Drush, you can run:

$ phpcs --standard=$path_to_coder_module/coder_sniffer/Drupal -- $file_or_folder_to_check

... where $path_to_coder_module is the path to Coder (e.g.: if your username is mparker17 and you use Linux, then it will be at /home/mparker17/.drush/coder)

The code has been tested and works