Skip to content

Commit c76678a

Browse files
authored
Merge pull request #587 from PHPCSStandards/feature/553-squiz-selfmemberreference-fix-false-negative-namespace-keyword-as-operator
Squiz/SelfMemberReference: bug fix - false negative with namespace keyword as operator
2 parents 638cfb6 + d6a6cc9 commit c76678a

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)