Using PHP CodeSniffer is one of those tools that is part of my workflow that I find utterly invaluable. I haven’t always used it, though.

Don’t get me wrong: If you’ve read this blog for any length of time, you know I’m big on Coding Standards. But when it came to the PHP CodeSniffer, there was an extended period I had gone before I installed it.

Why, though?

Simply put, it was because I didn’t know it existed, at least not for WordPress. When I discovered there was a ruleset for WordPress (and WordPress VIP), it became something that I believe should be a necessary tool in every WordPress developer’s toolbox.

Depending on your IDE, you may or may not be able to install a plugin or an extension that will automatically monitor your code while you’re writing.

I made the change to Atom some time ago, which I’ll share more about momentarily, and it has excellent support for incorporating PHP CodeSniffer into your workflow.

Using PHP CodeSniffer with Atom and WordPress

At first, I was planning to write a much longer post about how I’ve swapped IDEs from Coda to Atom, but I figured that was content for another post. Plus, it ended up dragging on longer than I wanted.

Suffice it to say that I’m no longer using Coda and after a bit of a prompting from Ryan Fugate, I opted to give Atom a third look. It’s matured a lot since I last tried it.

Atom

It’s what I’ve been using for the last few months now, and I dig it. I’ve got a whole list of packages I could share (and eventually will).

For now, though, I find that using PHP CodeSniffer with the WordPress ruleset is essential in writing quality WordPress code.

Using PHP CodeSniffer - An Atom Package

So if you’re using Atom, here’s how to set it up. I’m assuming that you’re using MAMP, but it doesn’t matter as long as you know the path to your PHP CodeSniffer installation.

  1. Install PHP CodeSniffer using Pear or Composer.
  2. Install the WordPress ruleset for the PHP CodeSniffer.
  3. Download a copy of the Linter PHPCS package.
  4. In Atom, specify the executable path to the PHPCS binary.
  5. In Atom, define the set of rules you want to apply when it’s evaluating your code.

Here’s a screenshot of what my configuration looks like:

Using PHP CodeSniffer with Atom - My Configuration

My configuration for using PHP CodeSniffer with WordPress and Atom.

Easy enough, right? Only specify the executable to your installation of phpcs then set the rules for which you want to evaluate your code, and you’ll be good to go.

If you’re looking for another developer’s experience with this setup, also check out what Jason Resnick has shared on his blog regarding using PHP CodeSniffer with Atom. His approach varies a little from mine and it’s always good to see how others tackle similar problems.

Category:
Resources
Tags:
,

Join the conversation! 5 Comments

  1. Thanks for the ht, Tom – much appreciated!

    I agree it’s always best to see how others do things. It’s what works for you.

  2. Thank you for this great tutorial :)

    Hope you will write an article on setting xdebug on atom soon

  3. Even easier instructions:

    Run command to install WP Coding Standards rulesets AND PHP Code Sniffer: composer global require “wp-coding-standards/wpcs=*”
    Edit ~/.bash_profile
    Update PATH to include ~/.composer/vendor/bin like this: export PATH=$PATH:~/.composer/vendor/bin
    Run command to include WP coding standard rulesets in PHP Code Sniffer config: phpcs –config-set installed_paths ~/.composer/vendor/wp-coding-standards/wpcs

    If you need to specify the PHP Code Sniffer executable path in your app (PhpStorm, Atom, etc) use: ~/.composer/vendor/bin/phpcs

Leave a Reply

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