Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether code meets Drupal coding standards or not.
You can install PHP_CodeSniffer through Composer, PEAR, Drush, or manually. It is recommended to install Coder in a central place, so you don't have to download it for every Drupal site. It is not recommended to install it in more than one place.
- Install Coder
- Install PHP CodeSniffer
- Running CodeSniffer
To check your code with the PHP CodeSniffer rules packaged with Coder, you will need:
- Composer or PEAR.
- Drush (optional).
Install Coder with Composer
First, make sure Composer is installed correctly:
$ which composer
If you get
composer not found or similar, follow Composer's installation instructions.
Install Coder (8.x-2.x) in your global Composer directory in your home directory (~/.composer):
$ COMPOSER_BIN_DIR=/usr/local/bin composer global require drupal/coder
drupal/coder:<8to install the old 7.x-2.x version (not recommended).
drupal/coder:dev-8.x-2.xto install the latest dev version (both Drupal 7 & 8)
phpcbf commands are available globally, alternatively you can add those to your $PATH variable in ~/.profile.
DrupalPractice Standard with PHPCS:
$ phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
Install Coder with Drush
- First, make sure Drush is installed correctly:
$ which drush
If you get
drush not foundor similar, follow Drush's installation instructions.
- Now install Coder to Drush's central store:
$ drush pm-download coder --destination=$HOME/.drush
For Windows, change $HOME to %HOMEPATH%.
- Clear drush's internal cache so it sees Coder:
$ drush cache-clear drush
- Note that Drush stored Coder's folder inside a hidden
.drushfolder in your home directory (e.g.: if your username is
mparker17and you use Linux, then Coder will be at
/home/mparker17/.drush/coder). You'll need the path to Coder shortly.
Install coder manually
- Download the latest version of Coder from the Coder module project page.
- Unpack the zip or tarball to the place you want to store it.
- Note the path where you stored Coder: you'll need it shortly.
Install PHP CodeSniffer
Install CodeSniffer with Composer for Drupal 7
If you installed Coder with Composer, it automatically pulled CodeSniffer for you.
$ composer global require squizlabs/PHP_CodeSniffer:\<2
Install CodeSniffer with Composer for Drupal 8
$ composer global require squizlabs/PHP_CodeSniffer:\>=2
Install CodeSniffer with PEAR
- First, make sure PEAR is installed correctly:
$ which pear
If you get
pear not foundor similar, follow PEAR's installation instructions
- Now install PHP CodeSniffer:
$ pear install PHP_CodeSniffer-1.5.6
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
Test PHP Codesniffer was installed correctly
To do this, just run:
$ phpcs --version
You should see something like
PHP_CodeSniffer version 1.5.6 (stable) by Squiz (http://www.squiz.net). Depending on how you installed it, you may have to give the full path instead of simply
Running checks on a file or folder
$ phpcs --standard=Drupal /path/to/example.module
If you installed Coder with Drush, you can run:
$ drush drupalcs $file_or_folder_to_check
$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
$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
Setting up an alias (optional)
Running that long command every time is annoying. You probably want to set up a shell alias:
- Figure out which shell you're using:
$ echo $SHELL
Take note of the text after the last
/: this is your shell. Common ones are
- If you're using
If you're using
- At the bottom of the file, add the alias:
alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
- Re-run your .bashrc or .zshrc file so it picks up your new alias.
$ source ~/.bashrc
$ source ~/.zshrc
... or, restart your shell / terminal.
- Now you can use the alias like this:
$ drupalcs sites/all/modules/mymodule