PHP CodeSniffer and Mess Detecter in Netbeans

Code quality and structure becomes extremely significant as the number of developers on a project increases. It can also be helpful when the code is maintained or refactored infrequently. In both cases, it can reduce the time required to get your head back in the code.

Today I found a plugin for Netbeans that uses phpMD and PHP CodeSniffer to examine the code for a project and make recommendations to improve it. The feedback is visible in the tasks panel and will take you right to the place it suggests you change.

Here’s how I installed it on Windows using WAMPServer. I first installed PEAR on WAMPServer. Then I installed a few libraries using the PEAR installer as follows:

C:\wamp2.1e\bin\php\php5.2.11>pear install PHP_CodeSniffer
Unknown remote channel: pear.phpunit.de
Did not download optional dependencies: channel://pear.phpunit.de/PHP_Timer, use --alldeps to download automatically
pear/PHP_CodeSniffer can optionally use package "channel://pear.phpunit.de/PHP_Timer"
downloading PHP_CodeSniffer-1.3.2.tgz ...
Starting to download PHP_CodeSniffer-1.3.2.tgz (328,845 bytes)
....................................................................done: 328,845 bytes
install ok: channel://pear.php.net/PHP_CodeSniffer-1.3.2
 
C:\wamp2.1e\bin\php\php5.2.11>phpcs --version
PHP_CodeSniffer version 1.3.2 (stable) by Squiz Pty Ltd. (http://www.squiz.net)
 
C:\wamp2.1e\bin\php\php5.2.11>pear channel-discover pear.pdepend.org
Adding Channel "pear.pdepend.org" succeeded
Discovery of channel "pear.pdepend.org" succeeded
 
C:\wamp2.1e\bin\php\php5.2.11>pear remote-list -c pdepend
CHANNEL PDEPEND AVAILABLE PACKAGES:
===================================
PACKAGE                            VERSION
PHP_CodeSniffer_Standards_PDepend2 1.0.0
PHP_Depend                         1.0.1
PHP_Depend_Log_Arbit               1.0.0
staticReflection                   1.0.0
 
C:\wamp2.1e\bin\php\php5.2.11>pear install pdepend/PHP_Depend
Did not download optional dependencies: pecl/imagick, use --alldeps to download automatically
pdepend/PHP_Depend can optionally use package "pecl/imagick" (version >= 2.2.0b2)
downloading PHP_Depend-1.0.1.tgz ...
Starting to download PHP_Depend-1.0.1.tgz (181,720 bytes)
......................................done: 181,720 bytes
install ok: channel://pear.pdepend.org/PHP_Depend-1.0.1
 
C:\wamp2.1e\bin\php\php5.2.11>pear channel-discover pear.phpmd.org
Adding Channel "pear.phpmd.org" succeeded
Discovery of channel "pear.phpmd.org" succeeded
 
C:\wamp2.1e\bin\php\php5.2.11>pear remote-list -c phpmd
CHANNEL PHPMD AVAILABLE PACKAGES:
=================================
PACKAGE VERSION
PHP_PMD 1.3.0
 
C:\wamp2.1e\bin\php\php5.2.11>pear install phpmd/PHP_PMD
downloading PHP_PMD-1.3.0.tgz ...
Starting to download PHP_PMD-1.3.0.tgz (45,722 bytes)
.............done: 45,722 bytes
install ok: channel://pear.phpmd.org/PHP_PMD-1.3.0
 
C:\wamp2.1e\bin\php\php5.2.11>

You could also install phpMD and PHP CodeSniffer separately if you wanted to.

I then had to tell the Netbeans plugin where the batch files were. I did that by opening the Tools -> Options window and setting the phpMD and PHP CodeSniffer tabs as shown below.

At this point all of the output in my tasks view showed errors. After restarting Netbeans it all worked like a charm. Here’s what my output looked like.

Conclusion

That little process took me slightly longer than I would like to admit. At the end of it, most of the recommendations were about comment tags, white space and underscores for variables. It turns out I don’t really care much about those items, so I didn’t spend any time going through them.

It did identify a few variable names that it thought were too long, but then I prefer to be descriptive for the same reasons I mentioned at the beginning of this post, so I ignored those suggestions too.

Perhaps I’ll find other projects where this type of automated review gives me more actionable work, but not this time.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

About Daniel Watrous

I'm a Software & Electrical Engineer and online entrepreneur.

No comments yet... Be the first to leave a reply!

Leave a Reply