Installing Xdebug for use with Eclipse or Netbeans on Linux
Scrollable Tables with Floating Header using CSS
Development Resource Project
A Simple ISAPI Filter for Authentication on IIS
Getting Set up with Ogre 3D on Ubuntu
Symfony 2 Crash Course

Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux

Friday, 9 April 10, 4:05 pm
The PEAR PHP_CodeSniffer project provides an invaluable tool for enforcing a consistent set of coding standards across a whole PHP project. Thanks to the guys at PHPsrc, it can now be installed as an Eclipse plug-in, to provide annotations about code standard violations right in your editor:



In order to get this set up and running, you first need Eclipse 3.3 or greater. You can download the package with PDT incorporated from the eclipse site. Get any other tools you need, such as subclipse for SVN.

Now you're ready to install the PHP tools from PHPsrc, which is accomplished by adding http://www.phpsrc.org/eclipse/pti/ as a new update site to the Eclipse plug-in finder found at Help > Install New Software...

Once that's complete and you've restarted Eclipse to ensure all the changes are applied, you need to configure the code sniffer plug in. Go to Window > Preferences. Expand the PHP Tools option on the left-hand side, and you should see an option for PHP CodeSniffer.

You need to select the PHP binary to start with. If you don't know the location of yours, type which php at the bash command prompt. Next choose which PEAR library you wish to use - the internal one installed as part of the PHP tools is usually what you want.

Below that is a pane headed CodeSniffer standards, which is where you select which standard set you are following. There are several common standard sets available out of the box, such as Zend, PEAR, and squiz. You can add your own standards definitions by clicking the button labeled 'New..', and you can apply multiple standards at once if desired:



Below that you can set how many spaces Tab characters should be equated to. This is only relevant when you use tabs in your code, and have a maximum line length.

Click OK once done, and you'll be prompted to rebuild your project so that the selected code standards can be applied to all your PHP source files.

For more info about its various options, check out the PEAR manual for codesniffer.

Eclipse PHP Code Formatter

On the subject of coding standards, someone has produced a very cool plug-in (or, apparently, drop-in) for Eclipse that tidies up your PHP source code according to a range of formatting options. The PDT Tools plug-in is a requirement, and as you've already installed that in order to use CodeSniffer, just get the latest version of the pdt_tools.formatterXXX.zip file from the misc directory on this page, and place it in the dropins subfolder of your Eclipse directory.

When you next restart Eclipse, you'll then find a new set of preferences (Window > Preferences) when you expand PHP > Code Style > Formatter:



These rules will be applied to tidy up the currently open source file whenever you choose the Source > Format option from the right-button context menu.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Number Shortage