Month: January 2018

Setting up the PHP Code Sniffer with WordPress Coding Standards Integration for PHPStorm on Ubuntu 17.10

(aka the longest blog title I’ve probably ever written).

Recently I reinstalled my laptop with Ubuntu 17.10 (Artful Aardvark). I’ve been wanting to upgrade since it was released in October to try out the updated Gnome interface since Ubuntu officially dropped the Unity interface for the latest release. Usually I stick with the LTS releases but the draw to try out the new, shiny thing was too much.

As I have adopted PHPStorm as my development IDE this meant I had to set up the PHP Code Sniffer and WordPress Coding Standards integration again. I remember this being a bit of a pain when I did it on my 16.04 install.

The Jetbrains documentation about WordPress Development using PhpStorm (specifically the section on  the coding standards) was written back in 2015 and the issues I had were mainly due to different paths for newer PHP/PEAR versions, so I thought I would document the updated process here.

Step 1) Install PHP Code Sniffer via the PEAR package

sudo pear install PHP_CodeSniffer

I could probably have done this using either the PEAR package or by using the Composer package but I found that the Jetbrains document refers to the location of the Code Sniffer sniffs using the PEAR method and I already had PEAR installed, so it was easier for me to use this method. At some later stage I might try the Composer method.

Step 2) Determine the location of the PHP Code Sniffer Standards

In Ubuntu 17.10 with a default PHP 7.1 install the Standards are located in the following path:

/usr/share/php/PHP/CodeSniffer/src/Standards

Step 3) Clone the WordPress Coding Standards GitHub repository

In this case I simply cloned this repository to the root of my home directory.

git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git

Step 4) Copy the relevant Coding Standard to the PHP Code Sniffer Standards location determined at Step 2

I copy all the available WordPress Coding Standards, and merely enable WordPress-Extra for my projects. I am thinking about enabling WordPress-VIP for some extra fun, but I’m not sure I’m brave enough to be shouted at by my IDE…

cd ~/WordPress-Coding-Standards
sudo cp -r WordPress /usr/share/php/PHP/CodeSniffer/src/Standards/ 
sudo cp -r WordPress-Core /usr/share/php/PHP/CodeSniffer/src/Standards/ 
sudo cp -r WordPress-Docs /usr/share/php/PHP/CodeSniffer/src/Standards/ 
sudo cp -r WordPress-Extra /usr/share/php/PHP/CodeSniffer/src/Standards/ 
sudo cp -r WordPress-VIP /usr/share/php/PHP/CodeSniffer/src/Standards/

Step 5) Enable PHP Code Sniffer with WordPress Coding Standards in PHPStorm

First step is to enable PHP Code Sniffer in PHPStorm. To do so you’ll need to know where phpcs is installed. Run the following from your command line to find out.

which phpcs

Mine is located at /usr/bin/phpcs.

In PHPStorm, go to your Settings screen (File->Settings or CTRL+ALT+S) and navigate to Languages and Frameworks -> PHP -> Code Sniffer. Next to the Local Configuration click the browse button and enter the path to phpcs in the PHP Code Sniffer (phpcs) path field. Hit Validate to make sure PHPStorm can find it, then click Ok.

Next you will need to enable the WordPress Coding Standards. Back to the Settings screen navigate to Editor -> Inspections, scroll down to PHP and open the tree. Then scroll down to PHP Code Sniffer Validation and tick the box next to it. In the box that appears to the right when you select PHP Code Sniffer Validation hit the little refresh button and then choose your WordPress Coding Standard of preference and click Ok.

Remember to do this last bit for each new (or existing) WordPress project as you can have different Code Sniffer rules set for each project type (for example if you’re working on any non WordPress projects, like Laravel).

Filed under: Development, Freelancing, WordPressTagged with: ,