Static code analysis

Hi all, today we will show you how you can check quality of your PHP code in NetBeans 7.4. For this task, we use standard PHP tools PHP_CodeSniffer and PHP Mess Detector.

As always if NetBeans uses any external tool, verify IDE Options that everything is set correctly:

IDE Options

If it is, you are ready to inspect your code (one can select a PHP project or a PHP file):

Inspect Code

Inspect dialog will appear. Here, we can e.g. select only one inspection tool or create custom configuration:

Inspect Dialog

If we are ready and run the inspection (please notice that it can take a long time for big projects), analysis results are shown:

Inspection Results

As you can see, the results are categorized by files and folders so we can easily recognize which area of our project needs our attention first. If we double click any of these results, the given warning(s) is shown to us on the proper line:

Inspection Warning

If anything goes wrong (e.g. inspection is running too long), one can easily verify what each tool does exactly in the Output window:

Inspection Log

If anyone of you has a tip for another PHP code analysis tool, let us know via NetBeans Bugzilla or leave a comment here (please notice that the comments are moderated so they will not appear here immediately).

That's all for today, as always, please test it and report all the issues or enhancements you find in NetBeans Bugzilla (component php, subcomponent Code).


Awesome! It's good to see this making it into NetBeans. I'd love to see the whole PHP community get more standardized.

Posted by Eli McMakin on May 23, 2013 at 12:31 AM CEST #

This is really great.

Posted by guest on May 23, 2013 at 09:43 AM CEST #

Are you able to set the standard per project, as I just a number of different standards

Posted by kangcool on May 24, 2013 at 12:33 PM CEST #

To kangcool: IMHO not, one can only create custom configurations. Feel free to submit an enhancement to NB Bugzilla.


Posted by Tomas Mysik on May 24, 2013 at 12:35 PM CEST #

Very cool. Netbeans could use plugins available at Sonar project (better, delegate to Sonar this analisys, not only for PHP but for Java and other languages what Sonar plugins have support).

Posted by Carlos Romel on May 29, 2013 at 03:27 PM CEST #

This capability will facilitate a quality analysis code. I love the entry

Posted by Badanie EEG on May 29, 2013 at 03:48 PM CEST #

I like how integrates this into "Action Items" that makes it easy to switch between open file tabs and have "context" be right there. Is it possible to have something similar instead of having to do "Inspect" on every open/current file?

Posted by guest on May 30, 2013 at 12:18 AM CEST #

To guest: Please, submit an enhancement to NB Bugzilla (link is in the blog post).


Posted by Tomas Mysik on May 30, 2013 at 06:18 AM CEST #

We currently use Netbeans to develop our systems in PHP with Symfony framework, we will give a try to this code analysis tools.


Posted by gwebtools subnet calculator on June 03, 2013 at 10:13 PM CEST #

The errors occur infrequently, you may want to report it quickly. NB development goes in the right direction. It amazes me that the IDE for Java environment got so many languages, and the product is very mature and stable.

Posted by Projektant wnętrz warszawa on June 05, 2013 at 10:57 PM CEST #

Really great work.

Copy Paste Detection is another candidate. What about using this as a guide for the tools to be integrated:

Posted by taatparya on July 16, 2013 at 08:02 PM CEST #

To taatparya:
Thanks! Please, as written in the blog post, use NetBeans Bugzilla for reporting issues and enhancements, not these comments.

Thanks for undertanding.

Posted by Tomas Mysik on July 16, 2013 at 09:03 PM CEST #

How do you set the default standard ?
on my computer none are available.

Posted by Ben on September 23, 2013 at 04:00 PM CEST #

To Ben:
Please verify that you have installed PHP_CodeSniffer properly (its standards are got via command line).


Posted by Tomas Mysik on September 23, 2013 at 04:49 PM CEST #

It's great that it's possible to inspect the files with CodeSniffer. But it would be much better if I would be able to force NetBeans to use a specific formatting.

I'm using PSR-2, but I'm unable to use valid multi-line method call arguments and method parameters. I have to select "never" in the wrapping options. When I select "if long" it only wraps the words which would be too long. So two parameters in one line, the other two in the second line.

Okay, I could use "Alway", but then I have also two lines when having two short parameters. Not very beautiful. But not valid, because the first argument/parameter have to be placed in a new line, not after the parantheses. I can't find an option for new lines after the parantheses.

And there are some other things, which causes that I can't use ALT + F to format my code so that it's PSR-2 conform.

Posted by Stefan on October 18, 2013 at 04:47 PM CEST #

Is it possible to disable code analysis, or at least certain rules? I don't really want 1000 warning telling me that my methods are longer than 10 lines ...

Posted by guest on October 21, 2013 at 10:36 AM CEST #

To guest: How would one do it from the command line?


Posted by Tomas Mysik on October 21, 2013 at 10:40 AM CEST #

is it possible to inspect a single folder of a project

Posted by guest on November 06, 2013 at 09:49 AM CET #

To guest: Unfortunately not.

Posted by Tomas Mysik on November 06, 2013 at 09:51 AM CET #

I installed codesniffer in xampp using PEAR and running phpcs -i returns 7 standards installed. Unfortunately, netbeans 7.4 still does not pick them up and says no standards available. Codesniffer version is 1.5.0RC4

Posted by guest on November 07, 2013 at 10:29 PM CET #

To guest: It seems that CodeSniffer changed the output format. Please, report it as a new issue and provide output of this command:

phpcs -i


Posted by Tomas Mysik on November 08, 2013 at 07:28 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed

This blogs is written by NetBeans developers who contribute to the PHP support mainly.


« July 2016