Formatting Changes in PhpStorm 10

Recent versions of PhpStorm have bundled several interesting formatting changes. Let’s take a look at what formatting changes have been seen in PhpStorm 9 & 10.

Case Statement Formatting

An option for controlling the indentation of the break statement has been added. The standard indentation is for the break statement to appear on the same line as the code:

Format Break Default

Using the Code Style settings under the Editor tab, then navigating to Wrapping and Braces -> ‘switch’ statement, you can uncheck the Indent ‘break’ from ‘case’ option to get the alternative formatting.

formatting break setting

Now, the break statement will appear with the same indentation as the corresponding case statement.

Formatting Break Alternative

Blank Lines Around Code

In PhpStorm 8, the option to set a minimum number of blank lines around fields was introduced. This led to some consternation as it was adding blank lines around properties and methods, which was causing undesired blank lines between the first/last property, class or method. Ensuring a minimum blank line of 1 in PhpStorm 8 would result in the following formatting:

formatting blank v8

In order to allow the Reformat Code tool to reformat the code exactly to the user’s needs, in PhpStorm 9 the Minimum Blank Lines Around options have been changed, and a new option called After Class Header was added.

formatting line settings

Now, under the Minimum Blank Lines options (which can be found under Settings -> Code Style -> PHP -> Blank Lines), the Blank Lines Around option only affects properties, constants or method blank lines that are not adjacent to the start or end of a class. To set the minimum blank lines before and after class definition braces, use the After Class Header option.

Trailing Commas in Arrays

Another much sought-after improvement is the ability to tell the code formatter to add trailing commas when reformatting arrays. You can turn this behavior on or off by using the Add a comma after last element in multiline array option under the Other tab.

formatting array comma

Auto-changing Case on Completion

Another brand new introduction in PhpStorm, the formatter will now change the case of true, false or null to the case you’ve set in the code style setting, whenever you select them using the code completion popup.

formatting case change

To see these options in the code completion popup, make sure you’ve got case sensitivity in code completion turned off, by selecting None in the Case sensitive completion option.

formatting case sensitive

Other Minor Improvements

There have also been some more minor improvements. The formatter now adheres to blank line settings even if you’re using some of the alternative PHP syntaxes. There were also a number of bug-fixes in and around the formatting tool, and some new settings and tweaks were added, like the ability to keep comments next to the return statement in functions/methods.

– Gary and the PhpStorm Team

JetBrains
The Drive to Develop

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in PhpStorm and tagged , , . Bookmark the permalink.
  • Jan

    There seems to still be no setting to enforce blank line before class closing brace. :(

  • Ravan Scafi

    Also, there’s a PSR-2 violation that PhpStorm doesn’t fix:

    “Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.”

    There’s no setting to enforce one argument per line when multiline, you can have multiple arguments on a single line.

  • http://www.eick.it/ Sven E.

    Hey Gary,

    Which theme for the IDE and the code you use?

    • spabby

      Hi Sven, apologies for my late reply – the theme here is Material UI which you can install from the plugins repository. I tweaked it a little because I like my errors to show very visibly and I like my variable colours to be more pronounced.

  • dedicated

    I would also like to know which IDE color scheme is used in screenshots.

    @sveneick:disqus have you found it ?

  • Kamen Minkov

    Any chance of seeing these settings for blank lines in WebStorm as well? Will they be part of the platform or are they PHPStorm-specific?

    • spabby

      Hi Kamen,

      They are PHP specific at the moment.

      • Kamen Minkov

        Ah, okay. Thanks for the reply!