From 7aec50f7885ccc77819d3c1c5b9d067eea08dfab Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 1 Dec 2019 14:34:56 +0100 Subject: [PATCH] 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. --- .../ControlStructureSpacingSniff.php | 1 + .../ControlStructureSpacingUnitTest.inc | 11 +++++++++++ .../ControlStructureSpacingUnitTest.inc.fixed | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php index a093e55476..2ee0afd80f 100644 --- a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php @@ -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; } diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc index 61336e72db..2cdb535650 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc @@ -73,3 +73,14 @@ EOD ) { return; } + + if ( + isset($foo) === true + && $bar > $foo + /* + * A multi-line comment. + */ + && $foo === true + ) { + break; + } diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed index cb03105043..7f6fbf9b06 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed @@ -74,3 +74,14 @@ EOD ) { return; } + + if ( + isset($foo) === true + && $bar > $foo + /* + * A multi-line comment. + */ + && $foo === true + ) { + break; + }