drupal / coder
Coder is a library to review Drupal code.
Installs: 41 028 577
Dependents: 475
Suggesters: 0
Security: 0
Stars: 29
Watchers: 4
Forks: 53
Type:phpcodesniffer-standard
pkg:composer/drupal/coder
Requires
- php: >=7.4
- ext-mbstring: *
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.1 || ^1.0.0
- sirbrillig/phpcs-variable-analysis: ^2.13
- slevomat/coding-standard: ^8.24
- squizlabs/php_codesniffer: ^4.0.1
- symfony/yaml: >=3.4.0
Requires (Dev)
- phpstan/phpstan: ^1.7.12
- phpunit/phpunit: ^9.0
- 9.x-dev
- 9.0.0-alpha2
- 9.0.0-alpha1
- 8.3.x-dev
- 8.3.31
- 8.3.30
- 8.3.29
- 8.3.28
- 8.3.27
- 8.3.26
- 8.3.25
- 8.3.24
- 8.3.23
- 8.3.22
- 8.3.21
- 8.3.20
- 8.3.19
- 8.3.18
- 8.3.17
- 8.3.16
- 8.3.15
- 8.3.14
- 8.3.13
- 8.3.12
- 8.3.11
- 8.3.10
- 8.3.9
- 8.3.8
- 8.3.7
- 8.3.6
- 8.3.5
- 8.3.4
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.12
- 8.2.11
- 8.2.10
- 8.2.9
- 8.2.8
- 8.2.7
- 8.2.6
- 8.2.5
- 8.2.4
- 8.2.3
- 8.2.2
- 8.2.1
- 8.2.0
- 8.2.0-rc1
- 8.2.0-beta1
- 8.2.0-alpha2
- 8.2.0-alpha1
- 7.2.5
- 7.2.4
- 7.2.3
- 7.2.2
- dev-8.x-3.x
- dev-8.x-2.x
- dev-7.x-2.x
This package is auto-updated.
Last update: 2025-11-23 17:37:54 UTC
README
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
Note that Javascript support has been removed. To check and fix Javascript files please use ESLint and see the Drupal ESLint documentation.
Local installation in your Drupal project
For collaborating developer teams it makes sense to lock a Coder version with your other development dependencies locally. In your project where you have your composer.json file execute this:
composer require --dev drupal/coder
Then you can run Coder on your custom module code like this:
./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml web/modules/custom
To simplify execution it is highly recommended to use a settings file, see below.
Usage
Check Drupal coding standards
./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml web/modules/custom
Check Drupal best practices
./vendor/bin/phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml web/modules/custom
Automatically fix coding standards
./vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,info,txt,md,yml web/modules/custom
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 like this:
<?xml version="1.0" encoding="UTF-8"?> <ruleset name="example"> <description>PHP CodeSniffer configuration for example development.</description> <!-- Check all files in the custom module directory and below. --> <file>web/modules/custom</file> <arg name="extensions" value="php,module,inc,install,test,profile,theme,info,txt,md,yml"/> <rule ref="Drupal"> <!-- Example how you would disable a rule you are not compliant with yet: <exclude name="Drupal.Commenting.Deprecated"/> --> </rule> <rule ref="DrupalPractice"/> <!-- Example how you would disable an external rule you do not like: <rule ref="PEAR.Functions.ValidDefaultValue.NotAtEnd"> <severity>0</severity> </rule> --> </ruleset>
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. Example:
./vendor/bin/phpcs -p
Working with Editors
Drupal Code Sniffer can be used with various editors.
Editors:
Automated Testing (PHPUnit + PHPCS + PHPStan)
Coder Sniffer comes with a PHPUnit test suite to make sure the sniffs work correctly. Use Composer to install the dependencies:
composer install
Then execute the tests:
./vendor/bin/phpunit
Then execute the coding standards checker on Coder itself:
./vendor/bin/phpcs
Then execute static analysis:
./vendor/bin/phpstan
Contributing
- 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"
Thank you!
Maintainers
- Pieter Frenssen, https://www.drupal.org/u/pfrenssen
- Michael Welford, https://www.drupal.org/u/mikejw
- Klaus Purer, https://www.drupal.org/u/klausi
Credits
Juliette Reinders Folmer and Greg Sherwood, many sniffs are modified copies of their original work on PHPCS.