Last updated March 22, 2014. Created by das-peter on January 28, 2012.
Edited by klausi, mparker17, arnested, balintk. Log in to edit this page.

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

  • PHP 5.1.2 or higher.
  • Composer or PEAR.
  • Drush (optional).

Install with Composer

First, make sure Composer is installed correctly:

$ which composer

If you get composer not found or similar, follow Composer's installation instructions.

Then install Coder in your global Composer directory in your home directory (~/.composer):

composer global require drupal/coder:\>7

Start using PHPCS, example:

~/.composer/vendor/bin/phpcs --standard=/home/klausi/.composer/vendor/drupal/coder/coder_sniffer/Drupal /path/to/example.module

Done! Of course that command is annoyingly long, so you can setup a shell alias (see below).

Install the Coder module alone

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

It's recommended to install these in a central place, so you don't have to download it for every Drupal site.

You can install the coder module with Drush or manually (but not both).

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 the Coder module to Drush's central store:

    $ drush pm-download coder --destination=$HOME/.drush
  3. Clear drush's internal cache so it sees the coder module:

    $ drush cache-clear drush
  4. Note that Drush stored the Coder module'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 the coder module shortly.

Install coder manually

  1. Download the latest version of the Coder module 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 the Coder module: you'll need it shortly.

Install PHP CodeSniffer globally

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 1.4.1 or higher.

You can install PHP_CodeSniffer through Composer, PEAR or Drush (but not more than one).

Install with Composer

$ composer global require squizlabs/PHP_CodeSniffer:\>=1.4.1

Install 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 PHcomposer global require drupal/coder:\>7P_CodeSniffer-1.4.2

Install 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.4.1 (stable) by Squiz (http://www.squiz.net).

Running checks on a file or folder

At this point, everything is set up and ready to go!

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 the coder module (e.g.: if your username is mparker17 and you use Linux, then it will be at /home/mparker17/.drush/coder)

3. 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.
    If you're using zsh, edit $HOME/.zshrc.
  3. At the bottom of the file, add the alias:

    alias drupalcs='phpcs --standard=$path_to_coder_module/coder_sniffer/Drupal --'

  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.

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

Comments

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.

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.

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
Web Developer @ Innoraft Solutions

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

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

Have you tried the 2.x branch?

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.

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

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

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

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

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

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

try this one :

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

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