From 436d24351edefb19ea417a4709cde6bf328118fe Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 29 Mar 2024 09:30:22 +0100 Subject: [PATCH] PEAR/ScopeClosingBrace: prevent fixer conflict with itself The `PEAR.WhiteSpace.ScopeClosingBrace` sniff could get into a conflict with itself when the scope closer was preceded by a PHP open tag and before that non-empty (i.e. non-indent) inline HTML. In that case, the sniff would not recognize that the close brace was not on a line by itself, as the search for the last content before would disregard the non-empty HTML and would continue searching, which meant that the "last relevant content before" token would point to a token on a previous line. This, in turn, then led to the sniff continuing on to the next error "Closing brace indented incorrectly", where the indent would now be incorrectly determined as `-1`, which in the fixer would lead to the original content and the replacement content being exactly the same, which created a fixer conflict. Fixed now by improving the "last content before" determination. Includes unit test. Related to 152 --- .../WhiteSpace/ScopeClosingBraceSniff.php | 23 +++++++++++-------- .../WhiteSpace/ScopeClosingBraceUnitTest.inc | 6 +++++ .../ScopeClosingBraceUnitTest.inc.fixed | 7 ++++++ .../WhiteSpace/ScopeClosingBraceUnitTest.php | 1 + 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/src/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index dd395324f3..cb8e46d523 100644 --- a/src/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/src/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -93,16 +93,19 @@ public function process(File $phpcsFile, $stackPtr) } // Check that the closing brace is on it's own line. - $lastContent = $phpcsFile->findPrevious( - [ - T_WHITESPACE, - T_INLINE_HTML, - T_OPEN_TAG, - ], - ($scopeEnd - 1), - $scopeStart, - true - ); + for ($lastContent = ($scopeEnd - 1); $lastContent > $scopeStart; $lastContent--) { + if ($tokens[$lastContent]['code'] === T_WHITESPACE || $tokens[$lastContent]['code'] === T_OPEN_TAG) { + continue; + } + + if ($tokens[$lastContent]['code'] === T_INLINE_HTML + && ltrim($tokens[$lastContent]['content']) === '' + ) { + continue; + } + + break; + } if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) { $error = 'Closing brace must be on a line by itself'; diff --git a/src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc b/src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc index 3f90067920..a97aca7603 100644 --- a/src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc +++ b/src/Standards/PEAR/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc @@ -162,3 +162,9 @@ enum Suits {} enum Cards { } + +?> + + +
+
+ + +
+
1, 160 => 1, 164 => 1, + 170 => 1, ]; }//end getErrorList()