Skip to content

Commit

Permalink
NamespaceHelper::getAllNamespacesPointers(): bug fix - allow for name…
Browse files Browse the repository at this point in the history
…space tokens used as operator

The `namespace` keyword can be used to declare namespaces, but is also allowed as an operator which resolves to the current namespace: `namespace\callMe();`.

This was not taken into account correctly when gathering all namespace declaration pointers.

Discovered via a false positive received from the `Namespaces/UnusedUses` sniff.

Includes tests for both the `NamespaceHelper` method as well as the `Namespaces/UnusedUses` sniff.
  • Loading branch information
jrfnl authored and kukulich committed Sep 29, 2023
1 parent 5504980 commit 80077da
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
16 changes: 14 additions & 2 deletions SlevomatCodingStandard/Helpers/NamespaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace SlevomatCodingStandard\Helpers;

use PHP_CodeSniffer\Files\File;
use function array_filter;
use function array_reverse;
use function array_slice;
use function array_values;
use function count;
use function defined;
use function explode;
Expand Down Expand Up @@ -35,8 +37,18 @@ class NamespaceHelper
*/
public static function getAllNamespacesPointers(File $phpcsFile): array
{
$lazyValue = static function () use ($phpcsFile): array {
return TokenHelper::findNextAll($phpcsFile, T_NAMESPACE, 0);
$tokens = $phpcsFile->getTokens();
$lazyValue = static function () use ($phpcsFile, $tokens): array {
$all = TokenHelper::findNextAll($phpcsFile, T_NAMESPACE, 0);
$all = array_filter(
$all,
static function ($pointer) use ($phpcsFile, $tokens) {
$next = TokenHelper::findNextEffective($phpcsFile, $pointer + 1);
return $next === null || $tokens[$next]['code'] !== T_NS_SEPARATOR;
}
);

return array_values($all);
};

return SniffLocalCache::getAndSetIfNotCached($phpcsFile, 'namespacePointers', $lazyValue);
Expand Down
3 changes: 3 additions & 0 deletions tests/Helpers/data/multipleNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
namespace Lorem\Ipsum;

new Dolor();

namespace\functionCallNotADeclaration();
namespace\CONSTANT_ACCES_NOT_A_DECLARATION;
6 changes: 6 additions & 0 deletions tests/Sniffs/Namespaces/UnusedUsesSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,10 @@ public function testUsesInAttributes(): void
self::assertNoSniffErrorInFile($report);
}

public function testUnusedUseNamespaceOperatorNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/unusedUsesNamespaceOperatorNoErrors.php');
self::assertNoSniffErrorInFile($report);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Foo;

use Bar;

class MyClass {
function foobar() {
namespace\callMe();
if (method_exists( Bar::class, 'method')) {}
}
}

0 comments on commit 80077da

Please sign in to comment.