The steps below cover how to set up PHP CodeSniffer in PhpStorm with WordPress Coding Standards rulesets. This can be very helpful for identifying violations of the WP coding standards as you write code, so you can fix them on-the-spot. This guide assumes you’re on Mac OS X or Linux, are using Vagrant for local development, have Composer installed and are comfortable using the command line. A video walkthrough of these steps is also available here.

1. Set up PHP Remote Interpreter

Go to PhpStorm Preferences > Languages & Frameworks > PHP, then click on the […] button.interpreter1
Input these settings:

  • SSH Credentials radio button
  • Your host IP address instead of the one shown (you can get this by running ping example.dev for one of your dev sites on the command line)
  • vagrant for both the User name and Password
  • /usr/bin/php for the PHP interpreter path

Then select the interpreter you just created:
interpreter2

2. Install PHP CodeSniffer

composer global require “squizlabs/php_codesniffer=*”
Then make sure a phpcs alias is accessible in the ~/.composer/vendor/bin/ directory

3. Install WordPress standards rulesets

composer create-project wp-coding-standards/wpcs:dev-master --no-dev
Then make sure the WP standards are accessible in the ~/.composer/wpcs/ directory

4. Add WordPress ruleset path to PHP_CodeSniffer configuration

cd ~/.composer/vendor/bin
phpcs --config-set installed_paths ~/.composer/wpcs

5. Add phpcs to your PATH so you can run it globally

open ~/.bash_profile (or ~/.zshrc if you use ZSH)
Paste in: PATH=$PATH:~/.composer/vendor/bin

Now you should be able to cd into any WP root directory and run phpcs --standard=WordPress wp-load.php. If you see output like this, you’ll know phpcs is working properly:

phpcs

6. Configure PHP CodeSniffer

Go to PhpStorm Preferences > Languages & Frameworks > PHP > Code Sniffer
Select this file: ~/.composer/vendor/bin/phpcs

7. Configure PHP CodeSniffer as a PhpStorm Inspection

Go to PhpStorm Preferences > Editor > Inspections
Go to PHP > PHP Code Sniffer Validation and check the checkbox
Select a WordPress coding standard from the dropdown menu and save it

phpstorm-code-sniffer-inspection

Try it Out

Now try typing something that is valid PHP, but is a violation of the WordPress Coding Standards, such as:

if(true){echo 'I must have forgotten a few spaces';}

PhpStorm should let you know that it’s a violation by giving it a squiggly underline. You can hover over it and get a popup with more info indicating what’s wrong with it. Then you can fix it manually, or use Code > Reformat Code (cmd+option+L on Mac) to automatically reformat the code – just make sure you’ve set the predefined style to WordPress in PhpStorm Preferences > Editor > Code Style > PHP > Set From > Predefined Style > WordPress.

Going Forward

To enable PHP CodeSniffer with the WP Coding Standards for any future projects, you just need to follow steps #1 and #7 above.

Resources

JetBrains PHP CodeSniffer Guide:
https://www.jetbrains.com/phpstorm/help/using-php-code-sniffeThe r-tool.html

PHP CodeSniffer:
https://github.com/squizlabs/PHP_CodeSniffer

WordPress Coding Standards rulesets:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards

Jason Boyle’s guide – provides additional details on several of the steps in this post:
http://jason-boyle.com/phpstorm-php-codesniffer-wordpress-coding-standards/

 

5 comments

  1. Ian Dunn November 22, 2015 at 9:55 am

    Reply

    A few extra things I had to do to get it working:

    * Make sure the Remote Interpreters plugin is enabled in PhpStorm
    * Use sudo for the installation commands
    * Convert smart quotes to regular quotes when copy/pasting the install command for PHP_CodeSniffer
    * Use /home/vagrant/wpcs as the directory for the WP sniffs

  2. Pingback: PhpStorm: PHP CodeSniffer & WordPress Coding Standards – Jason Boyle

  3. Marius Vetrici January 20, 2016 at 6:13 pm

    Reply

    Hello,

    after some digging around, I made it work, but you need to change the instructions on step 6 to:
    Select this file: ~/.composer/wpcs/vendor/squizlabs/php_codesniffer/scripts/phpcs

    The value you provided ~/.composer/vendor/bin/phpcs does not allow PHPStorm to see the WordPress standards in the drop down.

    Thanks for this awesome tutorial!

    • Kellen Mace January 21, 2016 at 1:12 am

      Reply

      Marius,

      I see the WP standards in the dropdown when using ~/.composer/vendor/bin/phpcs as the path, but I’ll leave your comment here for anyone else who has better luck with the path you provided. You’re welcome for the tutorial, and thanks for your comment 🙂

  4. Pingback: WordCamp Day 1 #WCEU - verenahaunschmid

Leave a reply

Your email address will not be published. Required fields are marked *