Skip to content

Commit

Permalink
Squiz/SelfMemberReference: handle namespace declaration ending on PHP…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
jrfnl committed Jul 15, 2024
1 parent 131886e commit 57935d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/Standards/Squiz/Tests/Classes/SelfMemberReferenceUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,14 @@ namespace Foo /*comment*/ \ Bah {
}
}
}

namespace EndsIn\CloseTag ?>
<?php

class Baz {
function myFunction()
{
\EndsIn\CloseTag\Whatever::something();
\EndsIn\CloseTag\Baz::something();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,14 @@ namespace Foo /*comment*/ \ Bah {
}
}
}

namespace EndsIn\CloseTag ?>
<?php

class Baz {
function myFunction()
{
\EndsIn\CloseTag\Whatever::something();
self::something();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getErrorList()
143 => 2,
162 => 1,
171 => 1,
183 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 57935d0

Please sign in to comment.