Setup PHP CodeSniffer along with WordPress Coding Standards [ Windows, XAMPP ]

The other day, I had some problem setting up latest WordPress coding standard for PHP CodeSniffer on a Windows machine. It was throwing some errors/warnings like the below. There were some others, but eventually we had fixed some of those. However the WP coding standards were not getting listed in phpcs. 🙁

Here are the steps to fix the problems and setup PHP CodeSniffer along with WordPress Coding Standards namely WordPress, WordPress-Core, WordPress-Extra and WordPress-VIP. FYI, I am on a Windows 7 box with XAMPP setup for Apache, MySql & PHP.

Image: Flicker

Image: Flicker cc

Install PHP CodeSniffer on Windows through PEAR installer:
Uninstall the old CodeSniffer(if any):

Install the latest CodeSniffer using PEAR:

It failed, that means we need to clear the cache:

Again, try installing the latest version of the PHP_CodeSniffer package:

Update the channel as asked above:

Upgrade pear too. Incase if any things are old.

Do a cache clear again:

See the list of default coding standards in PHP CodeSniffer:

 

Install WordPress coding standard definitions for the CodeSniffer:
Navigate to the coding standards directory:

Fetch the WordPress coding standards into CodeSniffer’s standards directory. (if git command not working on your command prompt, you may use any gui for git clone)

As, all of the individual standards of WordPress namely WordPress, WordPress-Core, WordPress-Extra and WordPress-VIP are in one directory, for phpcs to be able to detect the standards we need to add the path to phpcs’s installed_paths.

Check if all the installed standards are listed now:

 

How to use PHP_CodeSniffer:
You can use command prompt to check your code, or you can also configure the same with different IDEs like NetBeans, PHPStrom etc.

You can write the output of code sniffer to a file:

Integrate PHP CodeSniffer with NetBeans IDE [Steps]

CodeSniffer, an essential development tool that ensures your code remains clean and consistent, should always be with you while writing code to check and fix the messy things. For those people who use NetBeans IDE to write PHP applications, here are the steps to integrate PHP CodeSniffer with NetBeans IDE.

Steps:
  • Open NetBeans IDE. ( Let me tell you, I am using NetBeans IDE 7.3 for this demo )
  • Install the NetBeans plugin ‘phpCS-MD’ by going to Tools -> Plugins screen.
    Click on the “Available Plugins” tab and search for ‘phpCS’ in the search box provided on top-right corner. Click and check the plugin and click on the “Install” button.
  • Once installed, lets configure it.
    Go to “Tools” -> “Options”.
  • Click on the tab “CodeSniffer”.
    Here you have to provide the path to phpcs.bat file present under your PHP installation directory. Do this by clicking the “Change” button there.
  • Click on the “Test Settings” button and if it shows the PHP CodeSniffer version details, then its configured successfully.

Now, click OK and go back to your code for checking against a particular coding standard. If you want to set a different coding standard definition, then setup that standard before hand on CodeSniffer. How to do that ? Follow my previous post to know how to install CodeSniffer and various Coding Standard definitions. Go to the CodeSniffer setting tab on NetBeans and set the Coding Standard definition name there in the input box named “Standard”.

On the code screen, you can see a new button with a single Blue Eye on the tool bar. This is to check any file for code issues. Click on that and you can see a new section comes below your code named “PHP CS/MD” and displays all the problems in the code row-wise. Now fix the messy code. Happy Coding 🙂

Here are the step by step screen shots to make it easier to follow.
subharanjan_codesniffer_neatbeans_1

subharanjan_codesniffer_neatbeans_2

subharanjan_codesniffer_neatbeans_3

subharanjan_codesniffer_neatbeans_4

subharanjan_codesniffer_neatbeans_5