Skip to content

Commit d6a6cc9

Browse files
committed
Squiz/SelfMemberReference: bug fix - false negative with namespace keyword as operator
> The `namespace` keyword can both be used for namespace declarations as well as as an operator - "magic keyword" - to resolve to the current namespace. > See: https://www.php.net/manual/en/language.namespaces.nsconstants.php#example-298 > This last case is not correctly taken into account when determining the current namespace, which leads to false negatives. Fixed now. Includes test. Fixes 553
1 parent 638cfb6 commit d6a6cc9

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,23 @@ protected function getDeclarationNameWithNamespace(array $tokens, $stackPtr)
221221
*/
222222
protected function getNamespaceOfScope(File $phpcsFile, $stackPtr)
223223
{
224-
$namespace = '\\';
225-
$namespaceDeclaration = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr);
224+
$namespace = '\\';
225+
$tokens = $phpcsFile->getTokens();
226+
227+
while (($namespaceDeclaration = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr)) !== false) {
228+
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($namespaceDeclaration + 1), null, true);
229+
if ($tokens[$nextNonEmpty]['code'] === T_NS_SEPARATOR) {
230+
// Namespace operator. Ignore.
231+
$stackPtr = ($namespaceDeclaration - 1);
232+
continue;
233+
}
226234

227-
if ($namespaceDeclaration !== false) {
228235
$endOfNamespaceDeclaration = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_TAG], $namespaceDeclaration);
229236
$namespace = $this->getDeclarationNameWithNamespace(
230237
$phpcsFile->getTokens(),
231238
($endOfNamespaceDeclaration - 1)
232239
);
240+
break;
233241
}
234242

235243
return $namespace;

src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,17 @@ class Baz {
183183
\EndsIn\CloseTag\Baz::something();
184184
}
185185
}
186+
187+
// Issue PHPCSStandards/PHP_CodeSniffer#553.
188+
namespace TestMe;
189+
190+
namespace\functionCall();
191+
namespace\anotherFunctionCall();
192+
193+
class SelfMemberReference
194+
{
195+
public function falseNegative()
196+
{
197+
$testResults[] = \TestMe\SelfMemberReference::test();
198+
}
199+
}

src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,17 @@ class Baz {
171171
self::something();
172172
}
173173
}
174+
175+
// Issue PHPCSStandards/PHP_CodeSniffer#553.
176+
namespace TestMe;
177+
178+
namespace\functionCall();
179+
namespace\anotherFunctionCall();
180+
181+
class SelfMemberReference
182+
{
183+
public function falseNegative()
184+
{
185+
$testResults[] = self::test();
186+
}
187+
}

src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getErrorList()
4747
162 => 1,
4848
171 => 1,
4949
183 => 1,
50+
197 => 1,
5051
];
5152

5253
}//end getErrorList()

0 commit comments

Comments
 (0)