Formatting - Blank Lines
By Petr on Feb 11, 2010
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).