From 783371a64c67c1168c8dabaea0c5ccb381b35ca2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 16 Jul 2024 01:57:18 +0200 Subject: [PATCH] Squiz/SelfMemberReference: handle namespace declaration ending on PHP close tag This change prevents an edge-case false negative for a namespaced self reference, when the namespace declaration would be ended by a PHP close tag instead of the expected `T_SEMICOLON` or `T_OPEN_CURLY_BRACE`. Includes unit test. Related to 552 --- .../Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php | 2 +- .../Tests/Classes/SelfMemberReferenceUnitTest.inc | 11 +++++++++++ .../Classes/SelfMemberReferenceUnitTest.inc.fixed | 11 +++++++++++ .../Tests/Classes/SelfMemberReferenceUnitTest.php | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php b/src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php index ff1cd830ea..4c54aa4618 100644 --- a/src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php +++ b/src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php @@ -225,7 +225,7 @@ protected function getNamespaceOfScope(File $phpcsFile, $stackPtr) $namespaceDeclaration = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr); if ($namespaceDeclaration !== false) { - $endOfNamespaceDeclaration = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $namespaceDeclaration); + $endOfNamespaceDeclaration = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_TAG], $namespaceDeclaration); $namespace = $this->getDeclarationNameWithNamespace( $phpcsFile->getTokens(), ($endOfNamespaceDeclaration - 1) diff --git a/src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc b/src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc index 0a0729a0a9..f2c4713a66 100644 --- a/src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc +++ b/src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc @@ -172,3 +172,14 @@ namespace Foo /*comment*/ \ Bah { } } } + +namespace EndsIn\CloseTag ?> + + 2, 162 => 1, 171 => 1, + 183 => 1, ]; }//end getErrorList()