Coder is a library for automated Drupal code reviews and coding standard fixes. It defines rules for PHP_CodeSniffer
Built-in support for:
- "Drupal": Coding Standards https://www.drupal.org/coding-standards
- "DrupalPractice": Best practices for Drupal module development
Online documentation: https://www.drupal.org/node/1419980
First, make sure Composer is installed correctly:
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 global require drupal/coder
To make the
phpcbf commands available globally, add the Composer
bin path to your
$PATH variable in
Register the Drupal and DrupalPractice Standard with PHPCS:
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
Composer Installer Plugins
The Coder package (>= 8.2.11) now works with Composer Installer Plugins, that find and register standards whenever packages are installed or updated. To use such a plugin within your project, follow these steps.
composer require --dev dealerdirect/phpcodesniffer-composer-installer composer require --dev drupal/coder
Now, you will see Drupal and DrupalPractice listed in the available PHP CodeSniffer standards.
The same can be done for a Composer global installation.
composer global require dealerdirect/phpcodesniffer-composer-installer composer global require drupal/coder
Check Drupal coding standards
phpcs --standard=Drupal /file/to/drupal/example_module
Check Drupal best practices
phpcs --standard=DrupalPractice /file/to/drupal/example_module
Automatically fix coding standards
phpcbf --standard=Drupal /file/to/drupal/example_module
Working with Editors
Drupal Code Sniffer can be used with various editors.
Automated Testing (PHPUnit + PHPCS)
Coder Sniffer comes with a PHPUnit test suite to make sure the sniffs work correctly. Use Composer to install the dependencies:
Then execute the tests:
Then execute the coding standards checker on Coder itself:
- Make sure an issue exists at https://www.drupal.org/project/issues/coder
- Create a pull request against https://github.com/pfrenssen/coder
- Post a link to the pull request to the issue on drupal.org and set the issue to "needs review"
Pieter Frenssen, https://www.drupal.org/u/pfrenssen
Greg Sherwood and Squiz Pty Ltd, many sniffs are modified copies of their original work on PHPCS.