Last updated July 7, 2015. Created on January 28, 2012.
Edited by batigolix, Gold, selwynpolit, gausarts. Log in to edit this page.

Overview

PHP_CodeSniffer is a library that tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. 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.

It is recommended to install Coder in a central place, so you don't have to download it for every Drupal site. You can install Coder with Composer, Drush, or manually. It is not recommended to install it in more than one place.

You can install PHP_CodeSniffer through Composer, PEAR or Drush. It is not recommended to install it more than once.

  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)

Requirements

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 global require drupal/coder

You can also install the old 7.x-2.x version with composer global require drupal/coder:\<8 (not recommended).

Make the phpcs and phpcbf commands globally available:

$ sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin
$ sudo ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin

(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.4.2

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 (http://www.squiz.net). 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 Drupal.org forums, or join #drupal-support in IRC.

Comments

thursday_bw’s picture

There is a sandbox project here: http://drupal.org/sandbox/bevan.wishart/1441638 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

http://drupal.org/project/git_hooks 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 http://drupal.org/project/dcq 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

Regards,
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).
help!

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:
<xampp-root>\php

ronrons’s picture

If you get an error message like

No releases available for package "pear.php.net/PHP_CodeSniffer"
install failed

then execute the following command and try again.

pear clear-cache

Regards,
Rony

Kristina Katalinic’s picture

I keep getting following error when using with sublime text2 and using https://github.com/enzolutions/sublime-drupal 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]
[Finished]

What am I doing wrong

Brisbane Web Design, Development and SEO consulting services.
www.webmar.com.au

nicobot’s picture

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

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

Nico

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:
http://www.lullabot.com/blog/article/installing-php-pear-and-pecl-extens...

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

see https://www.drupal.org/node/2387871
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.

Thanks.

Sarath Kumar M (शरत कुमार एम)