PhpStorm formatting, chopped down line indent after context

41 Views Asked by At

Our team will start to use a formatter for PHP. We decided the easiest way was to use an already existing standard and decided for PSR-12.

Half the team uses VSCode and the extension Intelephense for formatting. The other half uses PhpStorm and the built in formatter with the PSR-12 preset.

Some modifications had to be made in PhpStorm to behave the same as the VSCode formatter. This because the VSCode extension requires Premium to change formatting preferences.

However there is one problem remaining.

PhpStorm indents a wrapped line after context, do not know how else to describe it. As can be seen in the snippet below there are 8 spaces before the string in the array and 4 spaces before the closing bracket.

if (1 && [
        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
    ]) {
}

If I remove the first argument (1) the indentation becomes correct

if ([
     'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]) {
}

When formatting with VSCode, this is the result

if (1 && [
     'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]) {
}

I have read through the PhpStorm formatting settings multiple times and been searching the web for similar problems but I am nowhere closer to fixing this.

Am I missing a setting or do you have any other recommendations?

1

There are 1 best solutions below

0
VerteXVaaR On

Following section of PSR-12 states:

Expressions in parentheses MAY be split across multiple lines, where each subsequent line is indented at least once. When doing so, the first condition MUST be on the next line. The closing parenthesis and opening brace MUST be placed together on their own line with one space between them. Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.

Therefore, the correct formatting of your line would be:

<?php

if (
    1
    && [
        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
    ]
) {
    // noop
}
  • Enable Code Style -> PHP -> Wrapping and Braces -> if() statement -> New line before first element
  • Enable Code Style -> PHP -> Wrapping and Braces -> if() statement -> New line after last element
  • Enable Code Style -> PHP -> Wrapping and Braces -> Binary expressions -> Wrap if long (or chop down)