Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace false positive #1829

Closed
rask opened this issue Jan 4, 2018 · 4 comments

Comments

@rask
Copy link

rask commented Jan 4, 2018

When using the PSR2 ruleset I'm getting an error for the following:

if (
    $cond1
    && $cond2
) {
    ...
}

Error states the following

ERROR | [x] Expected 0 spaces after opening bracket; newline found
    (PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace)

When I alter the code as follows then the error disappears:

if ($cond1
    && $cond2
) {
    ...
}

From my understanding PSR2 does allow a newline after the opening parenthesis if the if conditions are split to multiple lines. Or am I wrong and the sniff is working as expected?

Also the sniff is a bit confusing as it says the newline is next to a bracket instead of the opening parenthesis.

@rask rask changed the title PSR2.ControlStructures.ControlStructureSpacing.SpacingAdte PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace false positive Jan 4, 2018
@rask
Copy link
Author

rask commented Jan 4, 2018

I found #460 now. GitHub search is not fuzzy it seems.

So the problem would be that the fix would not play well together with the fixer tool?

@gsherwood
Copy link
Member

So the problem would be that the fix would not play well together with the fixer tool?

The problem is that PSR-2 does not describe this as valid syntax and nobody from the PHP-FIG has ever said otherwise, or added errata about it. So the only rule to go on is:

Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.

Which I think is pretty clear. You can't have whitespace after the opening parenthesis, so the first condition can't start on the next line.

If you want to read the full discussion, you can do so on the issue you linked, but no additional information has ever been made available, so my position needs to stay the same: redefining "space" in PSR-2 to only mean a space character and not newlines would completely break PSR-2, so I'm not going to change PHPCS in this way.

Hopefully that explains things.

@rask
Copy link
Author

rask commented Jan 5, 2018

I see. Thanks for the clarification. Will probably just disable the sniff on my end to make multiline conditionals more readable (in my opinion). :)

We can take a look at this later on if PSR2 is clarified on this point.

Thanks!

@rask rask closed this as completed Jan 5, 2018
sirbrillig added a commit to Automattic/phpcs-neutron-standard that referenced this issue Dec 11, 2019
sirbrillig added a commit to Automattic/phpcs-neutron-standard that referenced this issue Dec 11, 2019
* Add newlineCount as option on LongFunctionSniff

* Run Prettier on LongFunctionSniff

* Ignore SpacingAfterOpenBrace rule

See squizlabs/PHP_CodeSniffer#1829

* Add new fixture for LongFunctionSniff

* Add tests for maxFunctionLines
@OldStarchy
Copy link

For anyone coming here in 2023+ as I did, PSR-2 has been deprecated in favour of PSR-12 which fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants