[Solved] How to install Drupal coding standard checker via command line?

| | 2 min read

Coder is a Drupal module to check coding standard issues in custom modules and themes. It can check any files on coding standard issues according to Drupal specifications. This method uses latest composer and drush to install latest coder. Feel free to skip corresponding parts as you need.

  • Install Composer

    Check if composer is installed,
    which composer
    If this is empty, proceed with below command. Else skip.
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    which composer


    Install Coder

    composer global require drupal/coder
    sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin
    sudo ln -s ~/.composer/vendor/bin/phpcbf /usr/local/bin
    phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
  • Install the Latest Version of Drush

    If you have installed drush with package manager, remove it, as it would be probably old version.
    sudo apt-get remove drush
    Use the following to install latest drush.
    Add the line export PATH="$HOME/.composer/vendor/bin:$PATH" to end of ~/.bashrc
    source ~/.bashrc
    composer global require drush/drush:dev-master
    drush --version
    If it shows drush version, drush is installed correctly.
  • Download Coder Module

    drush pm-download coder --destination=$HOME/.drush
    drush cache-clear drush
  • Install PHP Code Sniffer

    composer global require squizlabs/PHP_CodeSniffer:\>=2
    phpcs --version

Now you can check standards like this.

phpcs --standard=Drupal /path/to/example.module

Make Alias

Its better to use an alias for drupal itself, like drupalcs. Follow the steps below for it.

echo $SHELL
vi ~/.bashrc

Append the following lines to it.

alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"
alias drupalcs="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,js,css,info,txt'"

Restart the terminal or use

source ~/.bashrc

You can now use like this.

drupalcs /path/to/module

Or for a stricter compliance,

drupalcsp /path/to/module

Hope this helps. Happy Coding. Also, please feel free to get in touch with us if any queries.