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

PSR12 standard reports errors for multi-line FOR definitions #2763

Closed
andre-exads opened this issue Dec 12, 2019 · 3 comments
Closed

PSR12 standard reports errors for multi-line FOR definitions #2763

andre-exads opened this issue Dec 12, 2019 · 3 comments
Milestone

Comments

@andre-exads
Copy link

Current PSR-12 configuration on CodeSniffer treat multi-line conditions in a for loop as incorrect, but according to https://www.php-fig.org/psr/psr-12/#54-for it is allowed. Example bellow.

for (
    $i=0;
    $i<5;
    $i++
) {
    // body here
}
@jrfnl
Copy link
Contributor

jrfnl commented Dec 12, 2019

@andre-exads Could you share the ruleset configuration you are using ? Or at the very least, please check that your ruleset does not include both PSR2 as well as PSR12.
The PSR12 ruleset includes all relevant rules from PSR2 automatically and having both rulesets active causes conflicts as the standards are different.

@andre-exads
Copy link
Author

andre-exads commented Dec 12, 2019

I'm not using phpcs file of any kind in my project (when I tried it was an even bigger mess) I only have the following setting on VScode settings.json:

"phpcs.standard": "PSR12",

But if I run it straight from the command line I have the same result:

phpcs --standard=PSR12 /full/path/to/code/file.php 

FILE: /full/path/to/code/file.php
--------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------------
 236 | ERROR | [x] Expected 1 space after first semicolon of FOR loop; newline found
 237 | ERROR | [x] Expected 1 space after second semicolon of FOR loop; newline found
--------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------

Time: 105ms; Memory: 8MB

I'm on Fedora Workstation 31 - php-pear-PHP-CodeSniffer-3.5.3-1.fc31.noarch

@gsherwood gsherwood added this to the 3.5.4 milestone Dec 15, 2019
@gsherwood gsherwood changed the title Control Structure Multiline PSR-12 (for) PSR12 standard report errors for multi-line FOR definitions Jan 6, 2020
@gsherwood gsherwood changed the title PSR12 standard report errors for multi-line FOR definitions PSR12 standard reports errors for multi-line FOR definitions Jan 6, 2020
gsherwood added a commit that referenced this issue Jan 6, 2020
…finitions

This was done by adding a new ignoreNewlines property to Squiz.ControlStructures.ForLoopDeclaration so that the PSR12 standard could use it. In the case of newlines, PSR12 should ignore the statement and let other sniffs enforce the multi-line rules. But when there is no newline, it needs this sniff to enforce a single space.
@gsherwood
Copy link
Member

I've committed a fix for this, which will be in 3.5.4. The solution required a new ignoreNewlines property for the Squiz.ControlStructures.ForLoopDeclaration sniff, which I've documented here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#squizcontrolstructuresforloopdeclaration

Thanks for reporting this.

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

No branches or pull requests

3 participants