Installing Coder

Last updated on
12 March 2024

This documentation needs review. See "Help improve this page" in the sidebar.

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 or 10. (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.

  1. Install Coder (will also install dependencies with Composer)
  2. 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.

Before Installation

If you are using Ubuntu, make sure that your $SHELL is /bin/bash
You can check your $SHELL using this command:

echo $SHELL

Install 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

On many systems, this will install the coder in ~/.config/composer/vendor/drupal/coder  or  ~/.composer/vendor/drupal/coder . This 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 ~/.config/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/bin

OR in git bash

cd ~/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/bin

Point 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

The above command should include DrupalPractice and Drupal in the installed coding standards list.

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:

  1. 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.
  2. 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
  3. 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" />

  4. 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 Ioannis Kappas. In its settings, enter the Executable Path. For example:

/Users/your-username/.config/composer/vendor/bin/phpcs

On Windows:

C:\Users\MyName\AppData\Roaming\Composer\vendor\bin\phpcs.bat

Other example paths may be found here.

Netbeans

Netbeans users can specify the global or local phpcs and standard in Tools > Options > PHP > Code analysis.

Execute the install command. (note, you can also install phpmd with HomeBrew).

#Broken as on 03 May 2023# brew install php-code-sniffer

(recommended) Install coder with composer

composer global require drupal/coder
composer global require squizlabs/php_codesniffer

Register the coding standards Drupal and DrupalPractice to phpcs with:

~/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer

Verify if everything went well with:

❯ ~/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, Drupal and DrupalPractice

Install coder manually for development

  1. Clone the latest version of Coder:
    git clone --branch 8.3.x http://git.drupal.org/project/coder.git
    cd coder
    
  2. Install dependencies:
    composer install
    
  3. 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

Page status: Needs review

You can: