Let’s face it we all start of with the best of intentions, but ensuring your PHP code confirms precisely to a given code standard is hard. It just isn’t what humans are good at. We miss semi-commas, we forget those pesky spaces between parens. What is worse is that all of the fixes to these tiny style “nits” take up precious cognitive cycles that could be better employed thinking about more complex problems.

Machines on the other hand are great at doing coding standards. In fact they love it! They’ll do it all day long and never complain. And they rarely get it wrong.

So with this in mind, let’s configure the robots to automatically fix our PHP code to conform to the WordPress PHP Coding Standards.

For this tutorial, I’ll start out without relying on a given editor. However, later I’ll focus more on Sublime Text. If Sublime isn’t your chosen editor then some quick searching should reveal the precise settings for your prefered choice. The key however, is getting the right software installed.

Step 1: review our chosen tools

PHP CodeSniffer has become the industry tool for PHP code linting and fixing. The tool is comprised of two scripts

  1. phpcs – tokenizes the files to detect violations of a defined coding standard.
  2. phpcbf – automatically corrects coding standard violations in your files.

It is also highly configurable, allowing users to define custom coding standards as Plugins. We need to conform to the WordPress coding standards so we’re going to make use of the WordPress Coding Standards for PHP_CodeSniffer rules.

Step 2: install PHPCS

The next step for us is to install the PHPCS package. I found this was easiest to achieve using composer but the PHPCS git repo lists a number of alternatives.

Assuming you have Composer installed you can simply run:

composer global require "squizlabs/php_codesniffer=*"

You should then add the composer bin directory to you PATH.

export PATH=$HOME/bin:/usr/local/bin:$HOME/.composer/vendor/bin:$PATH

Reload your shell to ensure your PATH is updated.

Step 3: Install WPCS

Next we need to install the WordPress Coding Standards rules for PHPCS. These can be installed in a number of different ways. Again I recommend Composer:

composer create-project wp-coding-standards/wpcs --no-dev

Once complete you should then be able to cd into wpcs/vendor/bin and run phpcs.

For the convenience of using phpcs as a global command, we also add the path to the wpcs/vendor/bin directory to our PATH.

It should now look similar to the below:

export PATH=$HOME/bin:/usr/local/bin:$HOME/wpcs/vendor/bin:$HOME/.composer/vendor/bin:$PATH

Reload your shell to ensure your PATH is updated, then from any directory type phpcs.

Step 4: Manually set the WP Coding Standards for PHPCS

Although it claims this is done automatically I found I had to tell PHPCS about the WPCS settings. To do this run:

phpcs --config-set installed_paths ~/wpcs/vendor/bin/wpcs

Step 5: Editor integration – Sublime Text

You should now be able to run phpcs against any PHP file using the WPCS from the command line. However, it might be easier to have this run automatically on save from within your editor.

I use Sublime Text so I’m going to focus on that.

Using the Sublime Package Installer, add the “Sublime PHPCS” package. Next locate the user settings file for the Sublime PHPCS package and add the following:

{
 "show_debug": true,
 "phpcs_command_on_save": false,
 "phpcs_executable_path": "~/wpcs/vendor/bin/phpcs",
 "phpcs_additional_args": {
 "--standard": "WordPress",
 "-n": ""
 },
 "php_cs_fixer_executable_path": "~/.composer/vendor/bin/php-cs-fixer",
 "php_cs_fixer_on_save": true,
 "php_cs_fixer_additional_args": {
 "--standard": "WordPress",
 },
 "phpcbf_executable_path": "~/wpcs/vendor/bin/phpcbf",
 "phpcbf_on_save": true,
 "phpcbf_additional_args": {
 "--standard": "WordPress",
 "-n": ""
 },
}

Please note: you may need to update the following items to be accurate for your system:

  • phpcs_executable_path
  • php_cs_fixer_executable_path
  • phpcbf_executable_path

You can find the correct paths for your system by running the following commands in your terminal:

  • which phpcs
  • which php-cs-fixer
  • which phpcbf

Finally, restart Sublime Text.

That’s it! You should now have WPCS running using PHPCS on save using Sublime Text.

Leave a Reply

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