Skip to content

Commit 7aec50f

Browse files
committed
PSR12/ControlStructureSpacing: allow for comments between conditions
PSR12 does not forbid comments interlaced in the conditions of a (multi-line) control structure. However, the sniff didn't handle those correctly. With the example code added to the unit test case file, the sniff, as it was, would throw an unsolvable error which would also cause fixer conflicts with the sniff itself: ``` 82 | ERROR | [x] Each line in a multi-line control structure must be indented at least once; expected at least 8 spaces, but found 0 | | (PSR12.ControlStructures.ControlStructureSpacing.LineIndent) ``` ``` # phpcbf ./psr12/tests/controlstructures/controlstructurespacingunittest.inc --standard=psr12 --sniffs=psr12.controlstructures.controlstructurespacing PHP_CodeSniffer version 3.5.3 (stable) by Squiz (http://www.squiz.net) PHPCBF RESULT SUMMARY ---------------------------------------------------------------------------------------------------------------------------------------- FILE FIXED REMAINING ---------------------------------------------------------------------------------------------------------------------------------------- /src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc FAILED TO FIX ---------------------------------------------------------------------------------------------------------------------------------------- A TOTAL OF 17 ERRORS WERE FIXED IN 1 FILE ---------------------------------------------------------------------------------------------------------------------------------------- PHPCBF FAILED TO FIX 1 FILE ---------------------------------------------------------------------------------------------------------------------------------------- ``` By ignoring lines where the first token is a comment, this issue will be fixed.
1 parent 0676055 commit 7aec50f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function process(File $phpcsFile, $stackPtr)
9595
for ($i = $parenOpener; $i < $parenCloser; $i++) {
9696
if ($tokens[$i]['column'] !== 1
9797
|| $tokens[($i + 1)]['line'] > $tokens[$i]['line']
98+
|| isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true
9899
) {
99100
continue;
100101
}

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,14 @@ EOD
7373
) {
7474
return;
7575
}
76+
77+
if (
78+
isset($foo) === true
79+
&& $bar > $foo
80+
/*
81+
* A multi-line comment.
82+
*/
83+
&& $foo === true
84+
) {
85+
break;
86+
}

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ EOD
7474
) {
7575
return;
7676
}
77+
78+
if (
79+
isset($foo) === true
80+
&& $bar > $foo
81+
/*
82+
* A multi-line comment.
83+
*/
84+
&& $foo === true
85+
) {
86+
break;
87+
}

0 commit comments

Comments
 (0)