Installing Coder
Overview
PHP_CodeSniffer is a library that tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It works with Drupal 6, 7, 8, 9, 10 or 11. (After you install Coder, see command-line options for running it here.)
Coder contains "sniffs" for PHP CodeSniffer. These "sniffs" tell PHP CodeSniffer whether the code meets Drupal coding standards or not. Specifically, there are two rulesets, Drupal and DrupalPractice. The former is intended to enforce the general standards and the latter – previously known as DrupalPractice sniffer – is aimed at module developers who want to avoid common mistakes.
Installing and checking Drupal coding standards using PHP CodeSniffer consists of the following steps at a high level.
- Install Coder (will also install dependencies with Composer)
- Run the sniffs
You can install Coder through Composer. This will automatically install phpcs and all of its dependencies.
You can install Coder globally or locally in your composer project.
Quick Start for DDEV
Install:
$ ddev composer require drupal/coder --devCheck if it got installed:
$ ddev exec ./vendor/bin/phpcs --version
PHP_CodeSniffer version 3.11.1 (stable) by Squiz and PHPCSStandards
... and test that it works:
Only some extensions are automatically included, use --extensions parameter to include relevant ones. Possibly add web/ before core/modules/[...].
$ ddev exec ./vendor/bin/phpcs --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml --standard=Drupal,DrupalPractice core/modules/system/system.install -v
[...]
Processing system.install [PHP => 15440 tokens in 1838 lines]... DONE in 545ms (13 errors, 5 warnings)
Before Installation
If you are using Ubuntu, make sure that your $SHELL is /bin/bash
You can check your $SHELL using this command:
echo $SHELLInstall Coder and phpcs with Composer
First, make sure Composer is installed correctly:
which composer
or:
c:\> composer
If you get composer not found or similar, follow the Composer's installation instructions.
Global Coder Install
Install Coder (8.3.x) in your global Composer directory in your home directory. (The directory is given by COMPOSER_HOME configuration option)
composer global require drupal/coder
composer global require slevomat/coding-standardOn many systems, this will install the coder in ~/.config/composer/vendor/drupal/coder or ~/.composer/vendor/drupal/coder .
".config" being present needs to be taken into consideration when following the information below.
On Windows, the path may look like C:/Users/<WindowsUsername>/AppData/Roaming/Composer, which can also be written as %AppData%/Composer. In the code below, simply replace ~/.config/composer/vendor with %AppData%/Composer/vendor.
You can check the installed location by running the following command:
composer global show -P
If the location is not equivalent to ~/.config/composer/vendor/drupal/coder then you will need to update the commands below. The ~ will be replaced with your user's home directory. A common variant is ~/.composer/vendor/drupal/coder.
If you wish to use a different version of coder than the latest stable version, use drupal/coder:8.3.x-dev to install the latest dev version (Drupal 7/8/9).
You can also use a git repository by installing coder as above and then running:
composer global update drupal/coder --prefer-source
This will make a git repository of coder available in ~/.config/composer/vendor/drupal/coder, and you can use that repository to switch between tagged releases, the tip of the development branch, patches for coder, etc.
To make the phpcs and phpcbf commands available globally, add those to your $PATH variable.
Note: You need to close and restart your terminal after you set the PATH, or source it.
For ~/.profile, ~/.bash_profile, ~/.bashrc or ~/.zshrc:
export PATH="$PATH:$HOME/.config/composer/vendor/bin"
In fish shell, use:
set PATH $PATH $HOME/.config/composer/vendor/bin
On Windows, update your ~/.bashrc file to include:
set PATH="%AppData%/Composer/vendor/bin:$PATH"
OR in git bash
set PATH="~/AppData/Roaming/Composer/vendor/bin:$PATH"
On Windows, if phpcs is not recognised as a global command, despite adding %AppData%/Composer/vendor/bin to your path, then you can run it from the folder where it is installed and provide a relative path:
cd %AppData%/Composer/vendor/squizlabs/php_codesniffer/binOR in git bash
cd ~/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/binPoint phpcs to the new standard
phpcs --config-set installed_paths ~/.config/composer/vendor/drupal/coder/coder_sniffer
or
phpcs --config-set installed_paths ~/.config/composer/vendor/drupal/coder/coder_sniffer,~/.config/composer/vendor/sirbrillig/phpcs-variable-analysis,~/.config/composer/vendor/slevomat/coding-standard
In Windows (git bash)
phpcs --config-set installed_paths ~/AppData/Roaming/Composer/vendor/drupal/coder/coder_sniffer*You may need to use sudo.
Verify Registered Standards
You can verify this has worked with phpcs -i, which requires the DrupalPractice and Drupal rules to the installed.
IDE and Editor Configuration
PhpStorm
To enable phpcs, make the following changes in the preference pane at
Preferences, Languages & Frameworks (sometimes) > PHP > Quality Tools > PHP_CodeSniffer:
- Specify the location of phpcs and phpcbf, via the ellipses to the right of "Configuration." Match the paths from the global install above, or a local dependency.
- Set 'Check files with extensions' list to include the file types scoped in the Coder ruleset:
php,module,inc,install,test,profile,theme,css,info,txt,md,yml - Set "Coding standard" to Drupal. If it does not appear on the list, try temporarily setting the ruleset standards path; it should be near the phpcs file; the path should end with something like ..../vendor/drupal/coder/coder_sniffer.
If you want to enable both the Drupal and DrupalPractice standards, or have trouble getting the GUI to provide Drupal as an option, edit the project configuration manually; the GUI only allows for a single selection. Edit .idea/inspectionProfiles/Project_Default.xml, find the CODING_STANDARD option, and change it to:
<option name="CODING_STANDARD" value="Drupal,DrupalPractice" /> -
If errors appear regarding missing Slevomat sniffs, even though phpcs works fine on the command line, uncheck the Installed standards path in this preference pane; it conflicts with multi-path sniffs.
VSCode
Visual Studio Code users can install the phpcs extension by shevaua. In its settings, enter the Executable Path. For example:
/Users/your-username/.config/composer/vendor/bin/phpcsOn Windows:
C:\Users\MyName\AppData\Roaming\Composer\vendor\bin\phpcs.batOther example paths may be found here.
Netbeans
Netbeans users can specify the global or local phpcs and standard in Tools > Options > PHP > Code analysis.
Install coder manually for development
- Clone the latest version of Coder:
git clone --branch 8.3.x http://git.drupal.org/project/coder.git cd coder - Install dependencies:
composer install - Make the phpcs and phpcbf command available in your ~/.profile (use the location where you cloned coder):
export PATH="$PATH:$HOME/workspace/coder/vendor/bin"
You can verify this has worked with:
phpcs -i
phpcbf -i
The above command should include DrupalPractice and Drupal in the installed coding standards list.
Running PHP CodeSniffer
See the Command Line Usage page.
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