On this page
- Check Drupal coding standards
- Check Drupal coding standards and ignore composer and node.js directories
- List enabled Drupal coding standards rules
- Check only one rule
- Check Drupal best practices
- Automatically fix coding standards
- Store settings in a phpcs.xml.dist file
- Setting up aliases (optional)
- Verify Registered Standards
PHP CodeSniffer - Command Line Usage
This documentation needs work. See "Help improve this page" in the sidebar.
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,info,txt,md,yml /path/to/drupal/example_module
Or an individual core file:
phpcs --standard=Drupal core/includes/install.core.inc
Check Drupal coding standards and ignore composer and node.js directories
phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml --ignore=node_modules,bower_components,vendor /path/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,info,txt,md,yml --sniffs=Drupal.Classes.UnusedUseStatement /path/to/drupal/example_module
Check Drupal best practices
phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml /path/to/drupal/example_module
Automatically fix coding standards
phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml /path/to/drupal/example_module
Store settings in a phpcs.xml.dist file
In order to save and commit your PHPCS settings to Git you can use a phpcs.xml.dist file in your project. See the phpcs.xml.dist example in Coder's README (we cannot render XML examples here in drupal.org doc pages). Then you can invoke phpcs without any options and it will read phpcs.xml.dist from the current directory. This can also be useful for Continuous Integration setups.
Setting up aliases (optional)
Running that long command every time is annoying. You probably want to set up shell aliases:
- Figure out which shell you're using:
$ echo $SHELL
Take note of the text after the last
/
: this is your shell. Common ones arebash
andzsh
. - If you're using
bash
, edit$HOME/.bashrc
(or$HOME/.bash_profile
, or$HOME/.bash_aliases
).If you're using
zsh
, edit$HOME/.zshrc
. - At the bottom of the file, add the aliases:
alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,info,txt,md,yml'" alias drupalcsp="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,info,txt,md,yml'" alias drupalcbf="phpcbf --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,info,txt,md,yml'"
- Re-run your .bashrc or .zshrc file so it picks up your new alias.
$ source ~/.bashrc
... or...
$ source ~/.bash_profile
... or...
$ source ~/.zshrc
... or, restart your shell/terminal.
- Now you can use the aliases like this:
$ drupalcs sites/all/modules/mymodule $ drupalcsp sites/all/modules/mymodule $ drupalcbf sites/all/modules/mymodule
Verify Registered Standards
You can verify phpcs is set up correctly this way:
$ phpcs -i
Above command should include DrupalPractice and Drupal in the installed coding standards list. (See installer info for more)
For further information about the usage of PHP_CodeSniffer please see its documentation:
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion