Skip to content

Commit

Permalink
PSR12/ControlStructureSpacing: allow for comments between conditions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Dec 1, 2019
1 parent 0676055 commit 7aec50f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function process(File $phpcsFile, $stackPtr)
for ($i = $parenOpener; $i < $parenCloser; $i++) {
if ($tokens[$i]['column'] !== 1
|| $tokens[($i + 1)]['line'] > $tokens[$i]['line']
|| isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true
) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ EOD
) {
return;
}

if (
isset($foo) === true
&& $bar > $foo
/*
* A multi-line comment.
*/
&& $foo === true
) {
break;
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ EOD
) {
return;
}

if (
isset($foo) === true
&& $bar > $foo
/*
* A multi-line comment.
*/
&& $foo === true
) {
break;
}

0 comments on commit 7aec50f

Please sign in to comment.