Last updated October 25, 2016. Created on January 28, 2012.
Edited by capysara, mikeker, keopx, jenlampton. 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.

Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether code meets Drupal coding standards or not.

You can install Coder Sniffer through Composer which will automatically install PHPCS and dependencies for you. 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

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

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

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.

subhojit777’s picture

After following all the above steps correctly when I run drupalcs abc.module, it gives following error:

ERROR: the "drupalcs/Drupal/ruleset.xml" coding standard is not installed. The installed coding standards are Zend, PHPCS, Squiz, Drupal, PEAR and MySource

When I changed the drupalcs alias to this:

alias drupalcs='phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme'

its working fine

Subhojit Paul

ain’s picture

Current documentation claims that the symlink should be created to /path/to/coder/coder_sniffer/Drupal although there is no coder_sniffer subfolder in Coder package(s).

annazhong’s picture

there is no coder_sniffer subfolder in Coder package(s).

Danny_Joris’s picture

Have you tried the 2.x branch?

generalconsensus’s picture

Perhaps this is one of the PHP_CodeSniffer issues for folks with sites that aren't Drupal 7. The drupalcs project is defunct and the coder project doesn't provide an option for integration with the PHP_CodeSniffer at the moment. Would be nice to have that in the documentation on the coder page.

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

Kristina Katalinic’s picture

I keep getting following error when using with sublime text2 and using Command+b

[Errno 2] No such file or directory
[cmd: [u'phpcs', u'--report=emacs', u'--standard=Drupal', u'--extensions=php,module,inc,install,test,profile,theme', u'/sites/commons3/profiles/commons/commons.profile']]
[dir: /sites/commons3/profiles/commons]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Marino/bin]

What am I doing wrong

Brisbane Web Design, Development and SEO consulting services.

nicobot’s picture

With newer versions of pear, it's needed to upgrade pear channels.
Run: pear channel-update

If you receive this kind of error:
ERROR: failed to mkdir C:\php\pear\tests\PHP_CodeSniffer\CodeSniffer\Core\File

Check the pear configuration: pear config-show
And make sure the test directory is correct.

In my case:

C:\xampp\php\pear>pear config-set test_dir c:\xampp\php\tests
config-set succeeded

b.ravanbakhsh’s picture

Also you can type pear upgrade php_codesniffer in windows command.
In my case was :D:\xampp\php>
D:\xampp\php>pear upgrade php_codesniffer

xjm’s picture

If you use MAMP and find yourself stuck on step 1 because pear doesn't work, see @justafish's article:

xjm’s picture

When I run:

brew tap homebrew/dupes josegonzalez/php
brew install drupal-code-sniffer

The tab is installed fine, but then I get:
Error: No available formula for drupal-code-sniffer

rjbautista’s picture

try this one :

brew tap homebrew/dupes josegonzalez/homebrew-php
brew install drupal-code-sniffer

quietone’s picture

I'd appreciate instructions for Debian.

I've tried all the above, plus "apt-get install php-codesniffer", without success. All I get are stack traces, like this one.

PHP Stack trace:
PHP   1. {main}() /usr/bin/phpcs:0
PHP   2. PHP_CodeSniffer_CLI->process() /usr/bin/phpcs:37
PHP   3. PHP_CodeSniffer->process() /usr/share/php/PHP/CodeSniffer/CLI.php:545
PHP   4. PHP_CodeSniffer->processFile() /usr/share/php/PHP/CodeSniffer.php:496
PHP   5. PHP_CodeSniffer->_processFile() /usr/share/php/PHP/CodeSniffer.php:1202
PHP   6. PHP_CodeSniffer_File->start() /usr/share/php/PHP/CodeSniffer.php:1322
PHP   7. Drupal_Sniffs_Commenting_FunctionCommentSniff->process() /usr/share/php/PHP/CodeSniffer/File.php:470
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'

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

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.


Nikhil Mohan’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: