Following the PSR12 :
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.
<?php
if (
$expr1
&& $expr2
) {
// if body
} elseif (
$expr3
&& $expr4
) {
// elseif body
}
The current PSR12 ruleset will show the following error code : PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace
The expected behavior is that no error is shown for the example code.
Just a note in case you are trying to use the PSR-12 standard and not just test it: the included PSR-12 standard is not complete (I released it as a preview) and I'm unable to complete it due to the inconsistencies in the PSR-12 spec itself.
Since PSR-12 is now back in draft, I'm not going to spend a lot of time on it until a few things start to change.
But this one is a pretty clear change from PSR-2 and is failing because I just used the PSR-2 sniff to cover a bunch of cases. This needs to be fixed up, probably by refactoring and extending the PSR-2 sniff.
Since 3.4.1 using following code and PSR12 standard phpcs incorrectly reports the error shown below.
<?php
for (
$i = 0;
$i < 10;
$i++
) {
// for body
}
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------------------------------------------------------------------------------------
2 | ERROR | [x] Expected 0 spaces after opening bracket; newline found (PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace)
3 | ERROR | [x] Expected 1 space after first semicolon of FOR loop; newline found
| | (Squiz.ControlStructures.ForLoopDeclaration.SpacingAfterFirst)
4 | ERROR | [x] Expected 1 space after second semicolon of FOR loop; newline found
| | (Squiz.ControlStructures.ForLoopDeclaration.SpacingAfterSecond)
Now that PSR-12 has been ratified, any timeline on when this issue will be fixed?
any timeline on when this issue will be fixed
No, I can't provide a time.
What's your opinion about this solution https://github.com/scheb/PHP_CodeSniffer/commit/73a6ccf6f7e5ff76f4b11e5f2e17848c085bc3bd ? It would allow PSR2 standard to behave as before, but allow newlines within PSR12.
I did not change all the sniffs yet, because I wanted to get some opinions first. The principle would be the same for others such as ForLoopDeclaration.
Besides that, I don't understand how to write a test case for a sniff with certain properties set. Maybe someone be so kind to point me to an example.
Can't see any other test using properties, but i guess you can write directly to $ruleset->ruleset
$config = new Config();
$config->standards = ['PSR2'];
$config->sniffs = ['PSR2.ControlStructures.ControlStructureSpacing'];
$ruleset = new Ruleset($config);
$ruleset->ruleset['PSR2.ControlStructures.ControlStructureSpacing']['properties']['allowNewLineAfterOpeningBrace'] = true;
Or just load the PSR12
$config = new Config();
$config->standards = ['PSR12'];
$config->sniffs = ['PSR2.ControlStructures.ControlStructureSpacing'];
$ruleset = new Ruleset($config);
Besides that, I don't understand how to write a test case for a sniff with certain properties set. Maybe someone be so kind to point me to an example.
@scheb Use // phpcs:set .... in the unit test case file to test with specific property settings. Just search for that in the /Tests/*/*.inc files and you'll see plenty of examples.
I've just noticed this is still open, but the newline issue has already been resolved in the PSR12 standard for 3.5.0 and this should have been closed then.
The solution was to write a new sniff for PSR12.
Most helpful comment
Just a note in case you are trying to use the PSR-12 standard and not just test it: the included PSR-12 standard is not complete (I released it as a preview) and I'm unable to complete it due to the inconsistencies in the PSR-12 spec itself.
Since PSR-12 is now back in draft, I'm not going to spend a lot of time on it until a few things start to change.
But this one is a pretty clear change from PSR-2 and is failing because I just used the PSR-2 sniff to cover a bunch of cases. This needs to be fixed up, probably by refactoring and extending the PSR-2 sniff.