Formatting - Blank Lines

Another formatting category that you can find in PHP formatting setting is Blank Lines category. If you don't know how to obtain settings for PHP formatter, look at the first blog from the formatting series. This category define adding and deleting blank lines between different elements in the PHP code.

As you can see on the picture, user can set how many blank lines the formatter should insert before or after an elements. Property defining blank lines around a namespace declaration and use statement are PHP 5.3 specific.

On the picture are displayed all properties that influence the inserting blank lines during formatting and also the default values.  

The algorithm deletes blank lines in groups of use statements and class fields declarations. The picture on the right sight displays an example with fields, where on the left side is code before formatting and on the right sight is the same code after formatting.

There are two groups of fields, the first one contains $field1, $field2, $field3 and $field4 properties. As you can see all the blank lines within the group of fields are removed, except the line before PHP doc comment for $field4 property, where is inserted the number of lines that is set to as Before Field property.

The second group of fields contains  $field5 and $field6 properties.

Similar behavior is for Use. So the properties Before Use, After Use, Before Field and After Field set to number of lines inserted before / after a group of use statements or fields, not the individual occurrences.

If there are two elements subsequently in the code that the first defines one blank line after the element and the second defines one blank line before the element, then there are not inserted two lines between these elements, but only one. Similarly only the bigger number of lines is placed between the elements, if the number of blank lines after the first element is not equal to the number of blank lines before the second element. 

See the picture below. The After Namespace property defines one blank lines after a namespace and Before Use property set to two lines. So two blank lines are created between the namespace declaration and group of use statements. 

From the picture above should be clear the meaning of properties Before Namespace, After Namespace, Before Use and After Use. The meaning of the rest properties is displayed on the picture below. For better understanding every property is set to one. The lines in the formatted code have color of the property that influence the number of blank lines. A few lines has two colors, because both property are counted for the line according the algorithm that is mentioned above. 

As you can see there are properties that define blank lines around a class (Before Class and After Class), blank lines after the open brace after class declaration (After Class Header) and blank lines before the close brace in class body (Before Class End).

The properties Before Field and After Field were already mentioned.

The last properties defines blank lines around a function / method (Before Function and After Function) and also blank lines before the close brace in function / method body (Before Function End).  


This looks really promising.

More rules the team does not have to worry about because the IDE can do the job in place.
Thats a big bonus point for netbeans in my book.

Just love it, thanks a lot !

Posted by Volker Dusch on February 11, 2010 at 02:32 AM CET #

Great! The more configurable NB is, the more we enjoy it

Posted by Pet on February 11, 2010 at 02:33 AM CET #

I really appreciate your efforts and thanks again for a job well done.

However I'm not sure if this is the best (easiest) way to do it. I somehow got the impression you are step-by-step re-implementing functionality to enforce coding styles. Don't get me wrong: I think this is great!

But: wouldn't on-board scripting capability do the same thing, plus it could serve far more purposes? Or simply a button to execute some external program and add the current file path to it? If in addition there was an easy way to distribute and share these scripts and settings, this would be heaven!

If we just had a chance to use our own scripts in NetBeans, we would write and share those ourselves. BTW: Checking a rule via regular expressions is a 3-liner in PHP and a lot less complicated than in Java.

We are PHP-programmers - not Java-programmers. Just don't tell us that we need to write Java to get it up and running. Let us do it in PHP and we will be fine.

I'm also worried about Oracle. So if we just had a really easy way to write our own small extensions using our scripting language of choice, we didn't have to worry so much anymore.
I also think this would mean a big plus for Java developers as well.

Posted by Tom on February 11, 2010 at 02:52 AM CET #

To Tom: There are few reasons why there has to be build in formatter.

- formatting is not used only for formatting action.
- running external script after every action which requires formatting / indentation (pressing enter, inserting an item from code completion window, templates) is too expensive. The editor in such case will be very slow.
- you can not have full control over external process, that is started from java application
- depending on a php script means that you can not use NetBeans on a machine where is not installed PHP runtime.
- running out of the box
- it's not possible to put result of parsing (AST) and lexical analysis of the code to a script. Such script has to do it again.
- our parser is "hacked" that is able to somehow parse broken code (and in the editor in 90% you have broken code).

But you are right, it's possible to do a plugin which will work as you describe. But still there has to be a formatter that will be used by NetBeans internally.

Posted by Petr Pisl on February 11, 2010 at 03:28 AM CET #

The screenshots described here has some extra features which are not available in the version that I am using.
I am using netbeans v6.8 and I am unable to find out extra options for removing blank lines in the code. :-?
please help.

Posted by gaurav on February 11, 2010 at 04:24 AM CET #

To gaurav: These features are not available in NB 6.8. They are available in the development builds, which can be downloaded from (nightly builds : ) or also you can download a continual build from our server:

The formatting setting is pretty new for PHP and I'm sure that it is not bugless. If you try it, please report every bug.

Posted by Petr Pisl on February 11, 2010 at 05:17 AM CET #

This will be even nicer, if these properties can be shared with CodeSniffer:

Posted by Bruce Ingalls on February 11, 2010 at 08:02 AM CET #

To Bruce: I was thinking about integration the code sniffer directly into NB, but time probably will not permit it. You are right, there is space to improve.

Posted by Petr Pisl on February 11, 2010 at 08:28 AM CET #

@Petr you might want to talk to the Java guys about CodeSniffer integration. It outputs Java-CheckStyle compatible XML files.

Maybe the Java folks already implemented support for CheckStyle that could work just as well for CodeSniffer. Or in case they haven't: maybe they would like to see CheckStyle support in NetBeans as a new feature.

Posted by Tom on February 12, 2010 at 02:52 AM CET #

@Bruce you might already know that, but your plugin won't work on Windows (probably also won't work on Mac either). Also you can't select your own coding standard in the options dialog and have to use one of the preinstalled ones.

Posted by Tom on February 12, 2010 at 04:03 AM CET #

If I copy-paste following code and select all and format, why does nb not reformat code so there are no blank lines as specified in format rules?

class Application_Model_GuestbookMapper

protected $_dbTable;

public function setDbTable($dbTable)


if (is_string($dbTable)) {

$dbTable = new $dbTable();


if (!$dbTable instanceof Zend_Db_Table_Abstract) {

throw new Exception('Invalid table data gateway provided');


$this->_dbTable = $dbTable;

return $this;


Posted by Pet on February 14, 2010 at 09:38 AM CET #

To Pet: Which version of NB do you use? These formatting setting is new in NetBeans 6.9 development builds.

Anyway the new lines are corrected only in places, which are defined by the properties. The blank lines in the code (like in if in your example), are not affected.

Posted by Petr Pisl on February 15, 2010 at 08:48 AM CET #

Nice. This will be even nicer, if you can synchronize your work with PHP CodeSniffer. Take a look at

Posted by Bruce Ingalls on February 17, 2010 at 10:20 AM CET #

An Option for blank lines after comment-blocks would be nice, so instead of removing all blank lines here:

\* some comment

\* another comment

and create

\* some comment
\* another comment

it should be possible to create formatting like:

\* some comment

\* another comment

So four additional Options like "After Commentblock" "Before Commentblock" and for single-line-comments "After Single Line Comment" and "Before Single Line Comment" would be nice

Posted by robo47 on February 17, 2010 at 10:34 AM CET #

Sorry about the dual post (Browser cache issue). FWIW, comment preview does not show other comments.

Note that CodeSniffer can read properties, such as ini-style files. This may be enough to contact the PEAR & Netbeans plugin author, to see if there is any interest in synergy.

I'm not a spokesman for the plugin, but some fixes are arriving for issues that you raise.

On one hand, RATS & YASCA
work nicely for multiple languages. However, it may be possible to leverage an existing PMD plugin for PHPMD support; I think PHPMD currently only supports sniffing, anyway.

HTH, Bruce

Posted by Bruce Ingalls on February 17, 2010 at 10:55 AM CET #

To robo47: I can create new options for blank lines before and after php comment and php doc comment. I'm not sure, whether it would be useful for single line comment, because sometimes you can want to keep more line comments together and other separate.

To Bruce: Thanks for the links. I think that the mentioned tools can be integrated, but it's slightly different from formatting.

Posted by Petr Pisl on February 18, 2010 at 06:44 AM CET #

Code formatter is pretty good thing, and I very appreciated its presence in Netbeans.

But due such improvements (which are damn good in general) it produces lots of false changes when your code is under source control, which was saved by NB 6.8.
This leads to big trouble when you looks at diff after Reformat in 6.9 and trying to locate small piece of problematic code between huge changes.
Just imagine, when you work with team and your team is working on few big projects which are already formated according 6.8 scheme. This new scheme 6.9 is practically denied for whole team, and has to be.

Here are some suggestions, which might help with this issue:

a) bring back the code formatter from 6.8 and let the user to decide, which version will use, eq. some checkbox, "Use old scheme?" which will actually "switch" the format engine.

b) add "leave untouched" checkbox next to every option in formatter settings - especially in "Blank Lines" section, so it will be possible to setup new formatter to behave close to old one.

Option A seems to be a quick and elegant solution, but B can bring way better possibilities on beautifying code.

Also option for export formatter settings only will be great also.

Thanks in advance,


Posted by Raazy on June 23, 2010 at 10:42 AM CEST #

@Radek: "Old formatter" didn't exist. The formatter before was basically just indenter (functionality of the indenter was called). Yes, the new formatter have few problems. Please report them separately for the cases.

The option A basically is nothing, because the Old scheme was not there. The option B is doable. Again the best solution would be if you can file a bug for it.

Regarding changes in the source code. It's good practice don't format source code, when there is another change and formatting changes commit separately. So then it's clear that the change is "just" visual.

Posted by Petr Pisl on June 24, 2010 at 03:59 AM CEST #

@Petr Pisl: Good point with separate commits; it sounds rather like an compromise, but it's possible.

Can you tell me, where would be best to file a bug for this so called option B?


Posted by Raazy on June 24, 2010 at 05:42 AM CEST #

@Radek: You can enter an issue from this page: . There is also a link to a document, how to enter a bug etc. You have to create netbeans account, but it's easy:). Thanks.

Posted by Petr Pisl on June 24, 2010 at 06:29 AM CEST #

Sorry for spamming here but I am unable to file a bug.
When I trying to open this
to post a bug I always get 500 Internal Server Error

Is your bugzilla OK?

Posted by Raazy on June 25, 2010 at 08:02 AM CEST #

I mostly like the PHP auto-formatting in Netbeans. Currently using (Build 201007040001).

One issue is whitespace between end of code and a comment. I like tabs for readable comments that are lined up vertically and auto-format happily wrecks that and I don't see an option to stop it.

$code<tab>= something;<tab><tab>// comment etc.
$m< ><tab>= lilbit;< ><tab><tab>// more comments

It seems to add spaces around operators and after semicolons which is okay, but most of my tabs are getting whacked also. I avoid using is on commented blocks but it is so great to do Ctrl-A then Shift-Alt-F I sometimes forget ;)

Thank you for all your efforts

Posted by Mike on July 13, 2010 at 04:13 PM CEST #

Hi Mike,

could you file a new issue for this. I think it can be considered as a bug. The issue you can enter here


Posted by Petr Pisl on July 14, 2010 at 07:34 AM CEST #

in reply to Bruce post...

I'm currently working on a NetBeans plugin, which displays phpMd ( and phpCodeSniffer ( messages in the nb task area. At the moment it only works with nb 6.9.1, but it works ;)

Posted by Jens on November 02, 2010 at 03:46 AM CET #


I was wondering regarding robo47 post on blank lines after comments.
I like using editor-folds. However I would have liked to have a blank line between them after i auto format.

I'm guessing that a blank line after a comment will not be comfortable in other places so any chance for a blank line after editor-fold?

Posted by buenon on August 20, 2012 at 06:29 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

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


« July 2016