Fun with WordPress PHP Coding Standards in Sublime Text - Cobbled Code

Writing WordPress plugins and themes is my kind of fun, really. Adhering to the WordPress PHP Coding Standards while doing that is not only even more fun but also helps “…maintain a consistent style so the code can become clean and easy to read at a glance”.

Here is how I managed to get automated tests against the WordPress coding standards up and running in Sublime Text on a Mac using Homebrew and Git.

Install PHP_CodeSniffer via Homebrew

PHP_Codesniffer contains two PHP scripts. The main phpcs script detects violations of a preset coding standard, phpcbf can automatically correct those violations.

$ brew tap homebrew/homebrew-php
$ brew install php-code-sniffer

After Homebrew is done installing php-code-sniffer it suggests to verify the installation by running phpcs --version. If you get a nice PHP_CodeSniffer version 3.0.2 (stable) by Squiz (http://www.squiz.net) response when running that command you are ready for the next step.

Install the WordPress-specific PHP_CodeSniffer rules

PHP_CodeSniffer comes included with a bunch of coding standards which are really not of much use to us in this context. To be able to check code against the WordPress coding standards, install them by cloning the PHP_CodeSniffer rules somewhere on your machine.

I keep my Sublime Text plugins and other stuff in a synced folder (Example) so that is where I cloned the repo. Any location will do fine though.

$ git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards ~/my/exciting/path/to/wordpress-coding-standards

Let PHP_CodeSniffer know where you cloned the package, so it can actually use the rules:

$ phpcs --config-set installed_paths ~/my/exciting/path/to/wordpress-coding-standards

Install and configure the sublime-phpcs plugin

Now go ahead and install the Phpcs plugin via Package Control. When this is done, tell the Phpcs plugin the path to phpcs by editing its package settings. Run $ which phpcs and Terminal will show you the path. Here are the settings I ended up with:

{
    // The path to the phpcs executable.
    "phpcs_executable_path":  "/usr/local/bin/phpcs",
    // Tell phpcs to use the WordPress standard by default.
    "phpcs_additional_args": {
        "--standard": "WordPress",
    },
    // Run phpcs on each file save.
    "phpcs_execute_on_save": true,
    // Show annoying panel whenever an error is found.
    "phpcs_show_quick_panel": true,
    // PHP_CodeSniffer comes with the PHP Code Beautifier
    // script which can automatically fix errors.
    // Again, tell the plugin which coding standards to use...
    "phpcbf_additional_args": {
        "--standard": "WordPress"
    },
    // ...and where to find the beautifier executable.
    "phpcbf_executable_path": "/usr/local/bin/phpcbf",
}

Pfew. Now, if all went well you can run the sniffer on a .php file via the Command Palette. Unless you are hardcore like that and your code is pristine from the get-go you’ll end up with a list of issues like this:

PHP_CodeSniffer results in Sublime Text Quick Panel

Happy sniffing!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.