From 25f72e4ccbe92f5e1d1bf888687f8973ba6aac72 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 19 Feb 2025 10:06:19 +0100 Subject: [PATCH] Squiz/EmbeddedPhp: bug fix - prevent fixer conflict The `Squiz.PHP.EmbeddedPhp` sniff expects no blank lines before the PHP close tag. This rule can, however, conflict with sniffs enforcing blank lines after a class or function. The sniff already contains protection against this conflict, but that protection did not take potential trailing comments after the close curly, like `//end class`, into account. Fixed now. Includes test. --- src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php | 5 +++-- src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc | 7 +++++++ .../Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc.fixed | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php index 7d27a8aa12..240b46e55f 100644 --- a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php @@ -292,8 +292,9 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag } // Check for a blank line at the bottom. - if ((isset($tokens[$lastContent]['scope_closer']) === false - || $tokens[$lastContent]['scope_closer'] !== $lastContent) + $lastNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($closingTag - 1), ($stackPtr + 1), true); + if ((isset($tokens[$lastNonEmpty]['scope_closer']) === false + || $tokens[$lastNonEmpty]['scope_closer'] !== $lastNonEmpty) && $tokens[$lastContent]['line'] < ($tokens[$closingTag]['line'] - 1) ) { // Find a token on the blank line to throw the error on. diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc index 983b3f3734..b4f481d4ee 100644 --- a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc @@ -283,6 +283,13 @@ echo $j; echo $j; ?> + + + +