Help getting PSR12 code settings configured - multi-line function call indent

With my Editor -> Code Style -> PHP set from PSR12...


if ($this->currentUser->isImportantPerson() || $this->currentUser->example($campusId ?? $currentCampusId)) {
return true;
}

When I auto-format this file, the line > 120 characters is transformed to:

if ($this->currentUser->isImportantPerson()
|| $this->currentUser->example(
$campusId ?? $currentCampusId
)) {
return true;
}

Which generates a phpcs error:
phpcs: The closing parenthesis of a multi-line control structure must be on the line after the last expression

 

How can I correct this?

10 comments
Comment actions Permalink

Hi there,

My guess is that this happens because of this fragment formatting:

$this->currentUser->example(
$campusId ?? $currentCampusId
)

Based on the above I say: check what you have for  "Code Style | PHP | Wrapping and Braces | Function call arguments" node...

 

Please also see this comment (seems related; the links mentioned there): https://intellij-support.jetbrains.com/hc/en-us/community/posts/4409741793554/comments/4409766939666

0
Comment actions Permalink

Hello,

I found that the specific error above could be fixed by checking `if() statement -> new line after last element`.  

However, it then indents it as such:

if ($this->currentUser->isImportantPerson() || $this->currentUser->example(
$campusId ?? $currentCampusId
)
) {

notice the 2 indents before $campusId.  this causes "phpcs: multi-line function call not indented correctly".

My conclusion here is that while phpstorm says it supports PSR12, it has some bugs/limitations.

0
Comment actions Permalink

notice the 2 indents before $campusId.

That looks like a correct indent if you need to keep every function parameter on a separate line:

$this->currentUser->example(
$campusId ?? $currentCampusId
)

What is the correct indent should be instead?

I think that the fact that it is inside the if statement makes this whole thing more complicated (multi-line if and multi-line function call)...

0
Comment actions Permalink

According to psr12 / phpcs it is only 1 indent.

0
Comment actions Permalink

What is the correct indent should be instead?

Like this (#1)?

if ($this->currentUser->isImportantPerson() || $this->currentUser->example(
$campusId ?? $currentCampusId
)
) {

or like this perhaps (#2)?

if ($this->currentUser->isImportantPerson() || $this->currentUser->example(
$campusId ?? $currentCampusId
)
) {

Or different one?

0
Comment actions Permalink

according to psr12, this is valid:

if ($this->currentUser->isImportantPerson() || $this->currentUser->example(
$campusId ?? $currentCampusId
)) {

(one indent level before $campusId)

0
Comment actions Permalink

So that can be https://youtrack.jetbrains.com/issue/WI-63897 as per link in my first comment.

If it's some another issue -- I do not know which one.

But I could not make the desired result in current 2021.2.3 version.

0
Comment actions Permalink

Jconline

Could you please advise how do you check against PSR12 validity?

I've created a code example based on this, then applied PSR12 style, changed some PHP code style settings & enabled PHP_CodeSniffer with PSR12 and it didn't report anything discussed here: https://i.gyazo.com/484802f0db83c86f95c5a850f0d24873.mp4

Note, that I had to set hard wrap to 60 to get the method body to be wrapped.

1
Comment actions Permalink

I am trying to reproduce the wrapping now and I can't get it to wrap / chop down the line if its over 120 characters.  

The psr12 check is phpcs setup with the psr12 coding standard.  We do have some custom rules in there for things like tabs instead of spaces... Once I can reproduce this I can try to dig in to figure out what combination is actually causing this.  Thanks for your help everyone.

0
Comment actions Permalink

Thanks. You can also share the config file you're using, later on.

0

Please sign in to leave a comment.