Skip to content

Commit

Permalink
Merge pull request #587 from PHPCSStandards/feature/553-squiz-selfmem…
Browse files Browse the repository at this point in the history
…berreference-fix-false-negative-namespace-keyword-as-operator

Squiz/SelfMemberReference: bug fix - false negative with namespace keyword as operator
  • Loading branch information
jrfnl authored Aug 9, 2024
2 parents 638cfb6 + d6a6cc9 commit c76678a
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 c76678a

Please sign in to comment.