Last updated November 26, 2015. Created on January 28, 2012.
Edited by fullerja, robinsonsarah01, kenorb, HongPong. 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. You'll need version 2.1.0 or higher.

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.

  1. Requirements
  2. Install Coder
    1. Install with Composer
    2. Install with Drush
    3. Install manually
  3. Install PHP CodeSniffer
    1. Install with Composer
    2. Install with PEAR
    3. Install with Drush
  4. Running CodeSniffer
    1. Test PHP Codesniffer was installed correctly
    2. Running checks on a file or folder
    3. Setting up an alias (optional)


To check your code with the PHP CodeSniffer rules packaged with Coder, you will need:

  • Composer or PEAR.
  • Drush (optional).

Install Coder

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

Check if phpcs and phpcbf commands are available globally, alternatively you can add those to your $PATH variable in ~/.profile.

Register the Drupal and DrupalPractice Standard with PHPCS:

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

Install Coder with Drush

  1. First, make sure Drush is installed correctly:
    $ which drush

    If you get drush not found or similar, follow Drush's installation instructions.

  2. Now install Coder to Drush's central store:
    $ drush pm-download coder --destination=$HOME/.drush

    For Windows, change $HOME to %HOMEPATH%.

  3. Clear drush's internal cache so it sees Coder:
    $ drush cache-clear drush
  4. 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 coder manually

  1. Download the latest version of Coder from the Coder module project page.
  2. Unpack the zip or tarball to the place you want to store it.
  3. 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

  1. First, make sure PEAR is installed correctly:
    $ which pear

    If you get pear not found or similar, follow PEAR's installation instructions

  2. 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

Running CodeSniffer

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 ( Depending on how you installed it, you may have to give the full path instead of simply phpcs.

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)

Setting up an alias (optional)

Running that long command every time is annoying. You probably want to set up a shell alias:

  1. Figure out which shell you're using:
    $ echo $SHELL

    Take note of the text after the last /: this is your shell. Common ones are bash and zsh.

  2. If you're using bash, edit $HOME/.bashrc (or $HOME/.bash_profile).
    If you're using zsh, edit $HOME/.zshrc.
  3. 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'"
  4. Re-run your .bashrc or .zshrc file so it picks up your new alias.
    $ source ~/.bashrc

    ... or...

    $ source ~/.zshrc

    ... or, restart your shell / terminal.

  5. Now you can use the alias like this:
    $ drupalcs sites/all/modules/mymodule

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/...

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.

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