Monday, February 17, 2014

PHP Code Analysis with NetBeans 7.4

NetBeans has become my IDE of choice for PHP in the last year or so. Version 7.4 shipped with support for code analysis tools with PHP, namely PHPMD and PHP_CodeSniffer. Unfortunately, usage of these tools within the IDE is not well documented, so I wanted to share how they work.


Installing the tools is up to you; NetBeans just uses the scripts you point to. I prefer using composer's global option, but if you prefer PEAR or some other installation method, that's fine too.

Once installed, navigate to "Tools > Options > PHP > Code Analysis" inside of NetBeans and configure the paths to the scripts and the default rules you want to use.


Now here's the part nobody really tells you: how to actually use these tools. From inside of NetBeans, navigate to "Source > Inspect..."

That's pretty much it. Select the scope and and configuration to use and click "Inspect." If you configured everything correctly, the scripts will run and after a few seconds (or minutes, depending on project size) you'll be presented with a nice results view.

Ignore Folders

You may want to instruct the IDE to ignore certain folders when running code analysis. For example, I use Composer to manage dependencies, and I don't want to see results for everything under my vendor directory. To ignore vendors, right-click on the project in the project pane and navigate to "Properties > Ignored Folders" and add the vendors folder under "Extra Folders for Code Analysis."