Using PHP_CodeSniffer on NetBeans 7.4

Tags: PHP, NetBeans, PHP_CodeSniffer, phpcs, en-US

In this first post, I’ll share my basic experience in how to use an important tool to assure and verify your code quality.

Special thanks to Tomas Mysik and Adam McAuley for their efforts on Bugzilla and the NetBeans PHP Mailing List, which helped me a lot on how to use these combined tools, and then create this tutorial.

Requirements

Note: I’m assuming you already have an HTTP Server (like Apache) installed and ready for action.

1) PHP_CodeSniffer

It’s a powerfull tool to verify if your code attends to a specific set of rules, contained on a standard. It can also be used thought prompt or command line.

To install this tool, we’ll use Composer, the modern package manager for PHP tools and projects. I’ll consider that you already have Composer installed and ready for action, so fire up your terminal (DOS on Windows) and execute the command below:

composer global require "squizlabs/php_codesniffer=*"

Tip: the directive global will provide the installed tool for all environments. Just remember to check that you have the ~/.composer/vendor/bin/ in your PATH, on Windows by checking the environment variables.

To finally check, run phpcs -i, where the directive -i asks for PHP_CodeSniffer to show alll the standards installed on your machine. You should see this message as a confirmation, as the same command on a Linux environment:

phpcs -i

2) Setting things up on NetBeans

Once you’ve installed PHP CodeSniffer successfully, you’ll need to inform the path of your phpcs file (in Windows, phpcs.bat) to allow NetBeans to use PHP CodeSniffer.

Go to Tools > Options > PHP > Code Sniffer. Look for the Code Sniffer field, and then click on the Search… button at the right of the field. This will make NetBeans find a list of installed versions of phpcs file on your computer. Select the phpcs file (located at your PEAR root’s installation folder), and then CodeSniffer is configured and ready for action.

Tip: As soon the phpcs file is correctly configured on NetBeans, you’ll see a command line in an output window on NetBeans performing phpcs -i, listing all the standards, as mentioned above.

netbeans-detail

3) How to use it on NetBeans

Simple and easy. On the top menu, go for Source > Inspect…. The following window show you the basic settings before perform the inspection:

netbeans-inspect

  • Scope suggests where you’d like to perform the inspection: in a project, all your projects, or even in a single file.

  • Configuration > All Analyzers allow you to select which tool you would like to use in the inspection: CodeSniffer, Mess Detector, both, or a customized profile.

Once you have selected all your preferences, click on the Inspect button to start the inspection. It may take some time, so I suggest you to open a window and have some fresh air ;-)

Tip: if you want to change the scope before adjust the settings, select a file or a project in the Projects window before you go to Source > Inspect… .

NetBeans will then output a list of matchings according to which standards you’ve used to perform the inspection, as the image above (a dog icons indicates a CodeSniffer match, and a garbage can points to a MessDetector match). Then, apply all your methods to fix these matchings to assure that your code is 100% according to the standards, as you wish.

netbeans-result


Post anterior:
Próximo post:

Davi Marcondes Moreira avatar
Sobre Davi Marcondes Moreira
Desenvolvedor de software, palestrante, evangelista de trabalho remoto e home office, amante de MTB/XCO e entusiasta de café. Ele/Dele. Leia mais