Drupal Code Sniffer — An easy and efficient way to always meet Drupal code standards …

Cheng Code
6 min readMar 23, 2020

--

An overview ;

It is essential to understand what is the purpose of code sniffer before proceeding to the installation process.

A complete and detailed explanation about the work that code sniffer does might be obtained here

In a nutshell , Coder looks for coding standard violations by comparing Drupal code against Drupal coding standards - ( These are basically a set of rules which regulates Drupal codes and make it more standard , comparable and more easy to understand‘ features which are essential for open source software like Drupal )

It can also fix some coding standard violations for you with commands from PHP_CodeSniffer -(A tool which detects code style errors in PHP code )

In simple , it does the irritating part and leaves all the fun to you !!!

PHPCS and PHPCFB are commands from PHP_CodeSniffer

The installation process ;

This tutorial will cover the installation of Drupal Code Sniffer on Atom which is my favourite editor . You are free to choose from a number of other editors including Sublime-text , Visual Studio code and Vim among others.

P.S ; I am currently working on ubuntu 18.04

The installation of Drupal Code Sniffer on Atom might be broken up into the following steps.

  1. Install Composer
  2. Install and Setup Drupal code sniffer
  3. Install Drupal Code Sniffer on Atom

Step ;1 ‘ Install composer

Composer is here required to install coder . You might checkout my previous blog posts or jump onto this quick installation guide in case you dont have composer installed on your system .

As you can see below , the latest version of composer is well installed and working perfectly on my system

Note ; I checked that by typing composer on terminal

Step ; 2 ‘Install and Setup Drupal code sniffer

This is the very core of our installation .While there are a number of different ways of Installing Coder Sniffer , i opted for the manual installation as i found it to be more direct and straightforward.

The steps are as follows;

  • On terminal , go to your home directory and clone the latest version of coder using ;

P.S ; Get the latest version of coder here

git clone --branch 8.x-3.x https://git.drupalcode.org/project/coder.git
  • Once the repo is cloned on your system , you might proceed to installing the required dependencies using composer. To do so run ;
  • P.S ; Make sure you are in the directory where you cloned coder ,mine is called “ coder” as you can see below.
composer install
Done installing all required dependencies
  • Next , you should create a symlink for the phpcs and phpcbf command so as to make the files available in your ~/.profile. This might easily be done by running the following commands;
1.sudo ln -s ~/coder/vendor/bin/phpcs /usr/bin/phpcs
2.sudo ln -s ~/coder/vendor/bin/phpcbf /usr/bin/phpcf
Creating Symlinks
  • Register the Drupal and Drupal Practice Standard with PHPCS
phpcs --config-set installed_paths ~/coder/coder_sniffer
  • That's it , you have successfully installed and set up Drupal Code Sniffer on your system .You might want to verify this by inserting this command in terminal ;

The expected outcome is supposed to be as follows ;

Highlighting these two because they are very important for our installation.

Note ; If you do not see DrupalPractice and Drupal in the installed coding standards list , this means that you have missed out something and the installation has not been appropriately done . You should ensure getting DrupalPractice and Drupal in the installed coding standards list for Drupal Code Sniffer to work.

Step;3 ‘ Install code sniffer on Atom by installing Linter package

Once Drupal code sniffer on your system , you should install it to your editor. The steps for most editors might be obtained here. In this tutorial , we will only see how to get it done on Atom since it is what i use …

  • Firstly install this package (Linter package )either from within Atom or by running the following command:
apm install linter-phpcs

Once installed , you will see the below output on your terminal.

linter-phpcs is installed !!!…yuppy

Step ; 4 ; Check coding standards of a Drupal file using Atom ‘ A brief explanation on how to use Drupal code sniffer

  • Well , open any Drupal project you want . For testing purposes I downloaded Back To Top Drupal module and opened it with Atom

NOTE ; Make sure to copy these codes and save it in your project as phpcs.xml

Save this as phpcs.xml and save into your project directory

Note ; These might also be accessed here

Code to copy ,paste and save as phpcs.xml in your project 
<?xml version="1.0"?> <ruleset name="drupal_project"> <description>Make this project use the Drupal coding standard.</description> <rule ref="Drupal" /> </ruleset>

When you open your project along with the saved phpcs.xml file , it will look as follows in the presence of errors

Drupal code sniffer works on Atom !!!

As you can see , Drupal code sniffer is working perfectly on my editor that is Atom .Once again ensure that the encircled phpcs.xml is well in your project directory as it is above !

  • I also checked the drupal standards from terminal .This might be done by entering the following command ;
1.To check the file for coding standard
phpcs –standard=DrupalPractice </path/to/example.module>
2.To check the complete directory recursively for coding standards
phpcs –standard=DrupalPractice <path to directory>

For the same file i was working on , it gave the same errors at line 6 and 9 as shown below…

The errors
  • P.S ; The very same errors have been pointed out on Atom above.
  • As I correct the errors , the red dots disappear indicating that Drupal standard violations are being eliminated and Drupal Standards are being respected …yurray !!!
  • That's it guys , we have installed Drupal Code Sniffer on Atom and it is working wonderfully as shown above … Congratulations !!!

To Sum Up ;

This task was pretty challenging to me though that it seems actually easy .My main problem was lack of Documentation for installation on Ubuntu since most tutorials focused on Windows and Mac Os . Google proved extremely helpful and thanks to it i could complete the installation of Drupal Code Sniffer on Atom ( my editor )….Thanks Google and Thanks guys for your read … See ya next…

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response