From ae6bd698d81f8e355ce08127fefbb105e90c520f Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 9 Aug 2024 15:05:40 -0300 Subject: [PATCH 1/2] Squiz/EmbeddedPhp: fix false positive when handling short open tags The content of a PHP long open tag token is ` ``` Resulted in the error (there is just one space after the opening tag and not two, as stated in the error): ``` Expected 1 space after opening PHP tag; 2 found (Squiz.PHP.EmbeddedPhp.SpacingAfterOpen) ``` And the incorrect fix: ``` ``` This commit fixes this problem by changing the sniff code to consider that only long open tags contain a space after the tag in the `content` key of the token array. --- .../Squiz/Sniffs/PHP/EmbeddedPhpSniff.php | 9 ++++--- .../Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc | 4 +++ .../Tests/PHP/EmbeddedPhpUnitTest.1.inc.fixed | 4 +++ .../Tests/PHP/EmbeddedPhpUnitTest.24.inc | 25 +++++++++++++++++++ .../PHP/EmbeddedPhpUnitTest.24.inc.fixed | 25 +++++++++++++++++++ .../Squiz/Tests/PHP/EmbeddedPhpUnitTest.php | 12 +++++++++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.24.inc create mode 100644 src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.24.inc.fixed diff --git a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php index 0be6118e43..184924b3e7 100644 --- a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php @@ -379,8 +379,11 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag) } // Check that there is one, and only one space at the start of the statement. - $leadingSpace = 0; - if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { + $leadingSpace = 0; + $isLongOpenTag = $tokens[$stackPtr]['code'] === T_OPEN_TAG + && stripos($tokens[$stackPtr]['content'], 'addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); if ($fix === true) { - if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { + if ($isLongOpenTag === true) { $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { // Short open tag with too much whitespace. diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc index f28c559894..015468357d 100644 --- a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc @@ -265,6 +265,10 @@ echo 'the PHP tag is correctly indented as an indent less than the previous code echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code'; ?> + + + + + + + + + + + + + + + + + + + + + + + + + + 1, 263 => 1, 264 => 1, + 270 => 1, ]; case 'EmbeddedPhpUnitTest.2.inc': @@ -190,6 +191,17 @@ public function getErrorList($testFile='') 22 => 2, ]; + case 'EmbeddedPhpUnitTest.24.inc': + $shortOpenTagDirective = (bool) ini_get('short_open_tag'); + if ($shortOpenTagDirective === true) { + return [ + 18 => 1, + 20 => 1, + ]; + } else { + return []; + } + default: return []; }//end switch From 9bc7b0b9383a11f863d8a51b47fde1afce3b3c2c Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 13 Aug 2024 13:13:31 -0300 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php | 11 ++++++----- src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php | 3 +-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php index 184924b3e7..63a1cdd09a 100644 --- a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php @@ -380,12 +380,13 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag) // Check that there is one, and only one space at the start of the statement. $leadingSpace = 0; - $isLongOpenTag = $tokens[$stackPtr]['code'] === T_OPEN_TAG - && stripos($tokens[$stackPtr]['content'], ' 1, 20 => 1, ]; - } else { - return []; } + return []; default: return [];