Last updated 31 March 2017. Created on 28 January 2012.
Edited by attheshow, HongPong, sandip27, wturrell. Log in to edit this page.


PHP_CodeSniffer is a library that tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It works with Drupal 6, 7, or 8. (After you install Coder Sniffer, see command line options for running it here.)

Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether code meets Drupal coding standards or not. Specifically there are two rulesets, Drupal and DrupalPractice. The former is intended to enforce the general standards and the latter – previously known as DrupalPractice sniffer – is aimed at module developers who want to avoid common mistakes.

You can install Coder Sniffer through Composer, this will automatically install PHPCS and its dependencies. Alternatively, phpcs can be installed via a package manager such as Homebrew for Mac (the formula is "php-code-sniffer") and you can then download the coding standards separately.

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 and PHPCS with Composer

First, make sure Composer is installed correctly:

$ which composer


c:\> 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. (The directory is given by COMPOSER_HOME configuration option)

$ composer global require drupal/coder

On many systems this will install coder in ~/.composer/vendor/drupal/coder. This location is the location assumed below.

On Windows, the path may look like C:/Users/<WindowsUsername>/AppData/Roaming/Composer, which can also be written as $HOME/AppData/Roaming/Composer. In the code below, simply replace ~/.composer/vendor with $HOME/AppData/Roaming/Composer/vendor.

You can check the installed location by doing:

$ composer global show -P

If the location is not equivalent to ~/.composer/vendor/drupal/coder then you will need to update the commands below. The ~ will be replaced with your user's home directory. A common variant is ~/.config/composer/vendor/drupal/coder.

If you wish to use a different version of coder than the latest stable version, use one of the following instead of drupal/coder in the command above:

  • drupal/coder:dev-8.x-2.x to install the latest dev version (both Drupal 7 & 8)
  • drupal/coder:<8 to install the old 7.x-2.x version (not recommended).

You can also use a git repository by installing coder as above and then running:

$ composer global update drupal/coder --prefer-source

This will make a git repository of coder available in ~/.composer/vendor/drupal/coder, and you can use that repository to switch between tagged releases, the tip of the development branch, patches for coder, etc.

To make the phpcs and phpcbf commands available globally, add those to your $PATH variable in ~/.profile, ~/.bash_profile, ~/.bashrc or ~/.zshrc:

export PATH="$PATH:$HOME/.composer/vendor/bin"

In fish shell, use:

set PATH $PATH $HOME/.composer/vendor/bin

On Windows, update your ~/.bashrc file to include:

export PATH="$HOME/AppData/Roaming/Composer/vendor/bin:$PATH"

Note: You may need to close and restart your terminal after you set the PATH.

Register the Drupal and DrupalPractice Standard with PHPCS:

$ phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer

Above, command should return Config value "installed_paths" updated successfully on console. If it doesn't, ensure you have latest stable version of PHP_CodeSniffer installed. As of 22nd November 2016, it's 2.7.0 (stable).

You can verify this has worked with:

$ phpcs -i

Above command should include DrupalPractice and Drupal in the installed coding standards list.

PhpStorm users can specify the local of phpcs (in Preferences, Languages & Frameworks > PHP > Code Sniffer) and can then configure which coding standard to use (Preferences, Editor > Inspections > PHP Code Sniffer validation > Coding standard - assuming you've successfully registered the standards you should be able to choose them from the dropdown list).

Or you can add symbolic path to support Drupal and DrupalPractice Standard:

$ ln -s ~/.composer/vendor/drupal/coder/coder_sniffer/Drupal ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Drupal

$ ln -s ~/.composer/vendor/drupal/coder/coder_sniffer/DrupalPractice ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/DrupalPractice

If the above directions were successful, you have completed the install of Code Sniffer. If you'd rather install manually, you can use the directions below instead. You can now proceed to Running PHP CodeSniffer below.

Or, install coder manually for development

  1. Clone the latest version of Coder:
    git clone --branch 8.x-2.x
    cd coder
  2. Install dependencies:
    composer install
  3. Make the phpcs and phpcbf command available in your ~/.profile (use the location where you cloned coder):
    export PATH="$PATH:$HOME/workspace/coder/vendor/bin"

Register the Drupal and DrupalPractice Standard with PHPCS:

$ phpcs --config-set installed_paths ~/workspace/coder/coder_sniffer

Above, command should return Config value "installed_paths" updated successfully on console. If it doesn't, ensure you have latest stable version of PHP_CodeSniffer installed. As of 22nd November 2016, it's 2.7.0 (stable).

You can verify this has worked with:

$ phpcs -i

Above command should include DrupalPractice and Drupal in the installed coding standards list.

Running PHP CodeSniffer

See the Command Line Usage page.

Looking for support? Visit the forums, or join #drupal-support in IRC.


thursday_bw’s picture

There is a sandbox project here: which contains two pre-commit hooks.
One of which calls drupalcs, just place both those files in .git/hooks and git will check the code against the drupal coding standards on commit.

fotuzlab’s picture

eugene.ilyin’s picture is deprecated, I continue develop in new project. Now it works like module for drupal and it is more comfortable to use.

You can check drupal code style on your project automatically with this module for team development through git.

rayjames’s picture

Just CODESNIFFER on Windows with XAMPP Install Instructions

1. Open the command line.
2. Go to your xampp root folder, and from there to: <xampp-root>\php\PEAR
3. Enter: pear install PHP_CodeSniffer

If you get this error when trying to install CodeSniffer ->
'pear' is not recognized as an internal or external command
then just back up one folder and try again:

ronrons’s picture

If you get an error message like

No releases available for package ""
install failed

then execute the following command and try again.

pear clear-cache

rwohleb’s picture

At least with phpcs 1.5.4 on OSX, if you attempt to use something along the lines of:
phpcs --standard=~/.composer/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml ~/path/to/custom/code
You will get an error like this:
ERROR: the "~/.composer/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml" coding standard is not installed. The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

If you use the full path with --standard then it works as expected:
phpcs --standard=/Users/username/.composer/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml ~/path/to/custom/code

uberhacker’s picture

Alternatively, you could also do the following:

phpcs --standard=$HOME/.composer/vendor/...

ichionid’s picture

I also added an alias into bashrc, so now I just need to type $drupalphpcs path/to/my/module
alias drupalphpcs='phpcs --standard=~/.composer/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml'

jrockowitz’s picture

I had to run the below command to install PHP_CodeSniffer 2.0.x-dev correctly

composer global require squizlabs/php_codesniffer:2.0.x-dev

If other people have had issues installing Coder 8.x-2.x and the above command helps. I can update the documentation.

Jim Bacon’s picture

With XAMPP on Windows 7, Drupal 7.34, Coder 7.x-2.4, PHP 5.6.4 I received

Fatal error: Access level to Drupal_Sniffs_Functions_DiscouragedFunctionsSniff::$forbiddenFunctions must be public

when using PHP_CodeSniffer 2.2.0 or 2.1.0.

No error when using 1.5.6

julien’s picture

or downgrade:

pear uninstall PHP_CodeSniffer
pear download PHP_CodeSniffer-1.5.6
pear install PHP_CodeSniffer-1.5.6
acorso’s picture

Thank you, this helped.

Todd Zebert’s picture

A little more info for others facing a smilar issue.
If you don't see a message like "Config value "installed_paths" added successfully" then you probably have an incompatible version of phpcs.

Do phpcs --version. I know in my case one system I worked on had "PHP_CodeSniffer version 1.5.0RC2 (beta)" which didn't work. Follow @julien's instructions to download 1.5.6 which works.

IT Sherpa, Drupal and full-stack developer, Consulting CTO. Loves JavaScript.
Organizer for Drupal LA Meetup and Camp

bisw’s picture

I was wondering for hours how to fix this issue and finally got it here. Thank you very much.

tsvenson’s picture

When I run "drush install-php-code-sniffer" on windows, where I use Acquia Dev Desktop 2, I get an error saying pear doesn't exist.

Is there any way I can get around this and get code sniffer working?

sarathkm’s picture

To use phpcs/phpcbf
After installing php code sniffer with composer add the path to C:\Users\xxxx\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\scripts to ENV PATH.
This is my installed location. Your drive may change.


Nikdhil Mdohfan’s picture

Any idea why this happening?

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class code-sniffer_Sniffs_Strings_ConcatenationSpacingSniff does not exist' in /usr/share/php/PHP/CodeSniffer.php:1335
Stack trace:
#0 /usr/share/php/PHP/CodeSniffer.php(1335): ReflectionClass->__construct('code-sniffer_Sn...')
#1 /usr/share/php/PHP/CodeSniffer.php(560): PHP_CodeSniffer->registerSniffs(Array, Array)
#2 /usr/share/php/PHP/CodeSniffer/CLI.php(818): PHP_CodeSniffer->initStandard(Array, Array)
#3 /usr/share/php/PHP/CodeSniffer/CLI.php(95): PHP_CodeSniffer_CLI->process()
#4 /usr/bin/phpcs(25): PHP_CodeSniffer_CLI->runphpcs()
#5 {main}
  thrown in /usr/share/php/PHP/CodeSniffer.php on line 1335
alex.designworks’s picture

Or you can install everything automatically using DCR:

Software Engineer | Integrated Experts |

ikeigenwijs’s picture

appears to be a double post...

prathK’s picture

If you face following error and you have installed PHP_CodeSniffer make sure you uninstall it and then run the composer install for coder again.

It will fix the following error.
PHP Fatal error: Interface 'PHP_CodeSniffer_Sniff' not found in \AppData\Roaming\Composer\vendor\drupal\coder\coder_sniffer\Drupal\Sniffs\Array\ArraySniff.php on line 24

Following is the reference URL from where I found the fix after few hours.
Hope this will save time for someone else struggling with same error.