Skip to content

Commit

Permalink
Squiz/SelfMemberReference: bug fix - false negative with namespace ke…
Browse files Browse the repository at this point in the history
…yword 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
  • Loading branch information
jrfnl committed Aug 5, 2024
1 parent 638cfb6 commit d6a6cc9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Standards/Squiz/Sniffs/Classes/SelfMemberReferenceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,23 @@ protected function getDeclarationNameWithNamespace(array $tokens, $stackPtr)
*/
protected function getNamespaceOfScope(File $phpcsFile, $stackPtr)
{
$namespace = '\\';
$namespaceDeclaration = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr);
$namespace = '\\';
$tokens = $phpcsFile->getTokens();

while (($namespaceDeclaration = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr)) !== false) {
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($namespaceDeclaration + 1), null, true);
if ($tokens[$nextNonEmpty]['code'] === T_NS_SEPARATOR) {
// Namespace operator. Ignore.
$stackPtr = ($namespaceDeclaration - 1);
continue;
}

if ($namespaceDeclaration !== false) {
$endOfNamespaceDeclaration = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_TAG], $namespaceDeclaration);
$namespace = $this->getDeclarationNameWithNamespace(
$phpcsFile->getTokens(),
($endOfNamespaceDeclaration - 1)
);
break;
}

return $namespace;
Expand Down
14 changes: 14 additions & 0 deletions src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,17 @@ class Baz {
\EndsIn\CloseTag\Baz::something();
}
}

// Issue PHPCSStandards/PHP_CodeSniffer#553.
namespace TestMe;

namespace\functionCall();
namespace\anotherFunctionCall();

class SelfMemberReference
{
public function falseNegative()
{
$testResults[] = \TestMe\SelfMemberReference::test();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,17 @@ class Baz {
self::something();
}
}

// Issue PHPCSStandards/PHP_CodeSniffer#553.
namespace TestMe;

namespace\functionCall();
namespace\anotherFunctionCall();

class SelfMemberReference
{
public function falseNegative()
{
$testResults[] = self::test();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getErrorList()
162 => 1,
171 => 1,
183 => 1,
197 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit d6a6cc9

Please sign in to comment.