Saturday, August 6, 2016

Using PHP CodeSniffer With WordPress: Installing and Using the WordPress Rules_part1

Prerequisites

This should be a very short list. If you've followed along with the series up to this point, you need to have:
  • a version of PHP (preferably 5.6.10 or later)
  • PHP CodeSniffer
  • Composer
All of this is covered in detail throughout the previous articles in the series, but if you've gotten this far and are comfortable with the command line then this should be a cinch in comparison to what we've done so far.

With all of that said, let's get started.

The WordPress Rules for PHP CodeSniffer

First, locate the WordPress Coding Standards rules on GitHub. They're easy to find.


You can read all about the details of the project from the project page, but the most important thing I'd like to share is as follows:
This project is a collection of PHP_CodeSniffer rules (sniffs) to validate code developed for WordPress. It ensures code quality and adherence to coding conventions, especially the official WordPress Coding Standards.
I'd like to bring your attention to the phrase that this references the "official WordPress Coding Standards." Note that these rules are based on the WordPress Coding Standards. That is, you can't officially reference them.

If you're looking to find a way to look through the rules that WordPress defines, check out this article in the Codex. It's easy to follow, easy to read, but a lot to remember. Thankfully, we have the rule set linked above.

The important thing to note is that even if you aren't familiar with the rules, the CodeSniffer will find the problems with your code and will notify you of what you need to fix. Though you don't have to read the Codex article, it can sometimes help in identifying what's needed based on the errors or warnings the sniffer generates.

1. Install the WordPress Rules
Assuming you've properly installed PHP CodeSniffer, let's add the WordPress rules to the software. For this tutorial, I'm going to do everything via the command line so as to be as platform agnostic as possible. I'll offer a few words regarding IDEs and rules at the end of the series.

Open your Terminal and navigate to where you have your copy of PHP CodeSniffer installed. If you've been following along with this series of tutorials, then you likely recall we have a composer.json file that will pull this in for us. If not, remember to create composer.json in the root of your project and add this to the file:
  1. {
  2.     "require": {
  3.         "squizlabs/php_codesniffer": "2.*"
  4.     }
  5. }
Once done, run $ composer update from your Terminal and you'll have everything you need to get going. To verify the installation, run the following command:
  1. $ vendor/bin/phpcs --version
And you should see something like the following output:
  1. PHP_CodeSniffer version 2.6.0 (stable) by Squiz (http://www.squiz.net)
Perfect. Next, let's install the WordPress rules. Since we're using Composer (and will continue to do so), this is very easy to do.

Run the following command from within the root directory of your project:
  1. composer create-project wp-coding-standards/wpcs:dev-master --no-dev
Note that you may be prompted with the following question:
  1. Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]?
If you know what you're doing, then feel free to go ahead and select 'n'; otherwise, you'll be fine hitting 'y'.

2. Add the Rules to PHP CodeSniffer
Now that PHP CodeSniffer is installed, and the WordPress rules are installed, we need to make sure PHP CodeSniffer is aware of our new ruleset. To do this, we need to enter the following command in the command line.

From the root of your project directory, enter the following command:
  1. $ vendor/bin/phpcs --config-set installed_paths wpcs
To verify that the new rules have been added, we can ask PHP CodeSniffer to report to us the sets of rules that it currently has available. In the Terminal, enter the following command:
  1. $ vendor/bin/phpcs -i
And you should see the following output (or something very similar):
  1. The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
Notice in the line above that we have several sets of rules regarding WordPress. Pretty neat, isn't it? Of course, let's see how this stacks up when we run the rule sets against a plugin like Hello Dolly.
Written by Tom McFarlin

If you found this post interesting, follow and support us.
Suggest for you:

Learning PHP 7: From the Basics to Application Development

The Complete PHP 7 Guide for Web Developers

Learn PHP 7 This Way to Rise Above & Beyond Competion!

PHP MySQL Database Connections

The Complete PHP with MySQL Developer Course (New)


No comments:

Post a Comment