Skip to content

Commit 98dd8d3

Browse files
committed
Merge branch 'feature/psr2-namespacedeclaration-fix-false-positive' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 72eaae3 + deb125a commit 98dd8d3

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/Standards/PSR2/Sniffs/Namespaces/NamespaceDeclarationSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
class NamespaceDeclarationSniff implements Sniff
1617
{
@@ -41,6 +42,12 @@ public function process(File $phpcsFile, $stackPtr)
4142
{
4243
$tokens = $phpcsFile->getTokens();
4344

45+
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
46+
if ($tokens[$nextNonEmpty]['code'] === T_NS_SEPARATOR) {
47+
// Namespace keyword as operator. Not a declaration.
48+
return;
49+
}
50+
4451
$end = $phpcsFile->findEndOfStatement($stackPtr);
4552
for ($i = ($end + 1); $i < ($phpcsFile->numTokens - 1); $i++) {
4653
if ($tokens[$i]['line'] === $tokens[$end]['line']) {

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ namespace Vendor\
2020

2121
Package;
2222
namespace Vendor\Package;
23+
24+
$call = namespace\function_name();
25+
echo namespace\CONSTANT_NAME;
26+
// Something which is not a blank line.

src/Standards/PSR2/Tests/Namespaces/NamespaceDeclarationUnitTest.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ namespace Vendor\
2222
Package;
2323

2424
namespace Vendor\Package;
25+
26+
$call = namespace\function_name();
27+
echo namespace\CONSTANT_NAME;
28+
// Something which is not a blank line.

0 commit comments

Comments
 (0)