Coding Standards in NetBeans IDE with PHP CodeSniffer
By Gavin Davies
A coding standard is a useful thing to have. It’s tricky because individual developers often have strong feelings about style, but it’s important to settle on one approach. Otherwise, your codebase looks inconsistent, scrappy and unprofessional. An egregious example is a mixture of tabs and spaces for indentation, which can cause all kinds of nasty code layout. Code should read beautifully and poetically.
For the Amaxus Web CMS, the product team decided on the PEAR standard. Like most real-world software projects that don’t take place in a perfect world, we have legacy code that does not conform to the standard. We considered a script-based wholesale auto-formatting, but that would create difficulty merging to older branches and besides it would not fix all the issues. We therefore decided to follow a “Boy Scout Rule” to always leave the codebase cleaner than we found it. Every file I open, I make sure I do at least something to bring it closer to our standards. Eventually, we will start to reject commits that don’t meet the standards.
Most of us use NetBeans: it integrates with Hudson and SVN, runs unit tests, and despite some stability issues, it generally meets our needs. To implement a Boy Scout Rule, it’s really handy if your IDE highlights the non-conforming areas of a file rather than storing a massively finicky and fine-grained rule set like PEAR in your head.
Setting up CodeSniffer
The first job is to make sure you have PHP Codesniffer installed, so from a command prompt:
pear install PHP_CodeSniffer
Test this from the command line:
C:\Users\gavin>phpcs --version
The console should respond with the current version of CodeSniffer installed.
Getting CodeSniffer working in NetBeans
The easiest way to do this is with a NetBeans plugin, available from the downloads page of Benjamin Eberlei’s GitHub repository. Download the .nbm (NetBeansModule) then go to Tools > Plugins and click the “downloaded” tab. Navigate to the .nbm file and select it (Windows users: see the “Getting it to work on Windows” section below).

Now click “Tools”, “Options”, “PHP Tab”, CodeSniffer and select the coding standard you want:

Now, when you are editing a file, open the Tasks snap-in (Window > Tasks) and you should get a list of tasks. You can also right click on the code and select “Show Code Standard Violations”

See http://github.com/beberlei/netbeans-php-enhancements for further information.
Getting it to work on Windows
At the time of writing, this will not work on Windows as it expects phpcs out of the box (issue ticket). If you download the source, and make the following change, it works on Windows. You need the NetBeans Plugin Development plugin installed in your NetBeans to build the project (this can be installed through Tools > Plugins).
/**
*
* @author manu
*/
public class CodeSnifferBinary {
public static final String BINARY = "phpcs.bat"; //NOI18N
...
All I've done is added .bat. It's just a quick and dirty hack but it works. You can also download a compiled version of this Windows .nbm here.
Troubleshooting
PROBLEM: Menu item is greyed out
CAUSE: 'phpcs' hasn't been found in the PATH
Notes
Tested in NetBeans 6.8 on Windows and OSX.