Last updated 20 February 2017. Created on 17 May 2012.
Edited by sachinsuryavanshi, xjm, lolandese, klausi. Log in to edit this page.

First make sure that you installed Coder and PHPCS correctly by following the installation instructions.

Check Drupal coding standards

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /file/to/drupal/example_module

List enabled Drupal coding standards rules

phpcs --standard=Drupal -e

Check only one rule

phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --sniffs=Drupal.Classes.UnusedUseStatement /file/to/drupal/example_module

Check Drupal best practices

phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /file/to/drupal/example_module

Automatically fix coding standards

phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /file/to/drupal/example_module

Setting up aliases (optional)

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

  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 aliases:
    alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
    alias drupalcsp="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
    alias drupalcbf="phpcbf --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
  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 aliases like this:
    $ drupalcs sites/all/modules/mymodule
    $ drupalcsp sites/all/modules/mymodule
    $ drupalcbf sites/all/modules/mymodule
    

For further information about the usage of PHP_CodeSniffer please see its documentation:

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