PHP CodeSniffer with PHPStorm using WordPress Coding Standards

The WordPress Community loves PHPStorm and bolstered a little bit from our local WordPress meetup (Singapore) whereby Jack Lenox talked about CodeSniffers, Xdebug, WordPress.com VIP and so on, I thought it apt to show how one can get PHP CodeSniffer working on PHPStorm.

If you’re not on PHPStorm, you really should try it. Perhaps the best money I’ve spent on development software. There is a free 30-days trial 🙂

This setup is good for people who develop remotely, but people who do their development work locally can probably get most of the setup tips here too.

Installing PHP-Pear

Pear allows you to install PHP extensions easily!

sudo apt-get install update
sudo apt-get install php-pear

You may get a message to do a channel update for pear, if so feel free to run

sudo pear channel-update pear.php.net

Installing PHP CodeSniffer

Next, using PEAR, install PHP_CodeSniffer

sudo pear install PHP_CodeSniffer

Now you have the base PHP_CodeSniffer alongside the standard tests such as PSR2 and so on installed.

Getting WordPress Coding Standards in

Next, we shall clone WordPress Coding Standards from Github. The repo has instructions for other ways to get this working if you have a different setup than I do.

If you run into the unknown command ‘Git’ issue, or you know you do not have git installed, just run

sudo apt-get install git

I opted to install the WordPress standards alongside the other PHP standards, you can install it elsewhere but it made little sense to me not to keep things organised.

If you’re on Ubuntu 14.04 & above, PHP CodeSniffer should have been installed to the share folder. I ran

cd /usr/share/php/tests/PHP_CodeSniffer/CodeSniffer/Standards

You can now do a

dir

and you’ll see PSR1, Squiz, PSR, Zend for example as the tests that you can run with PHPCS.

With Git installed, we can pull the repo down (wpcs being the folder name you want to use)

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

After the repo has been pulled down, we will need to add the new set of tests. WPCS offers Core, Docs, VIP, Extra tests amongst others. So we will run a command that automatically adds all those for us

sudo phpcs --config-set installed_paths /usr/share/php/tests/PHP_CodeSniffer/CodeSniffer/Standards/wpcs

To double check you can run

sudo phpcs -i

and you’ll see a bunch of new WordPress stuff added in.

Linking PHPStorm to PHP CodeSniffer

Now that you have PHPCodeSniffer installed, we can go ahead and setup PHPStorm’s link to it

Open PHPStorm and go to Default Settings. You can go to Settings if you want your changes to be project based instead of PHPStorm-wide

Inside Settings, go to ‘Language & Frameworks’ and then to ‘Code Sniffer’

codesniffersetting

Setting an Interpreter

You probably do not have any configuration here or a default local one. If you are developing with WAMPP or something, select local and choose the path to PHP.

However, I’m gonna go with the Remote Interpreter. Click the triple dot icon to start the setup

remote-interpreter

You will be asked to select how you want to connect to the remote interpreter. I basically connected to my Development VPS through the standard deployment configuration I already had. If you want to enter credentials instead, simply choose SSH Credentials

Next you will have to look for the PHP Executable. The path for Unbuntu 14.04 is there, simply look for it in your distro’s user/bin folder.

If you get the right file, you’ll see the correct details such as PHP Version, whether it has a debugger appear like in the picture.

Save this by clicking Ok.

path-for-phpcs

Next, you will have to look for the PHPCS file, use the … browse button to look for it visual. Or if you know the path, key it in. The default path for Ubuntu 14.04 is

/usr/bin/phpcs

You can click validate and it’ll show you a tooltip telling you the version number. At point of writing, the latest stable is 2.6.1

You’re almost done! Now you have linked the CodeSniffer, you simply need to tell PHPStorm to use it during Code Inspections

inspection

Still inside Settings, go to Editor -> Inspections

Expand the PHP Tab and look for the PHP Code Sniffer Validation

Select the Coding Standard you want – if you see nothing, click on the refresh button to retrieve it from your remote interpreter.

Click Ok to save. Now you have PHP Code Sniffer working.

Let’s test it out.

Testing the PHP CodeSniffer

If you Inspect Code your file, the results will be displayed in the PHP Dropdown in the Inspection Tab.

inspectionresults

 

As for inline results, check out the screenshot below

phpcs-spacing

Pretty nifty eh?

If you need any help, feel free to post in the comments below