diff --git a/SlevomatCodingStandard/Helpers/NamespaceHelper.php b/SlevomatCodingStandard/Helpers/NamespaceHelper.php index 6f97597f6..708f7d935 100644 --- a/SlevomatCodingStandard/Helpers/NamespaceHelper.php +++ b/SlevomatCodingStandard/Helpers/NamespaceHelper.php @@ -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; @@ -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); diff --git a/tests/Helpers/data/multipleNamespaces.php b/tests/Helpers/data/multipleNamespaces.php index 0cb8c9ba6..44c4b2600 100644 --- a/tests/Helpers/data/multipleNamespaces.php +++ b/tests/Helpers/data/multipleNamespaces.php @@ -7,3 +7,6 @@ namespace Lorem\Ipsum; new Dolor(); + +namespace\functionCallNotADeclaration(); +namespace\CONSTANT_ACCES_NOT_A_DECLARATION; diff --git a/tests/Sniffs/Namespaces/UnusedUsesSniffTest.php b/tests/Sniffs/Namespaces/UnusedUsesSniffTest.php index 60f4762cd..d77fd5ba1 100644 --- a/tests/Sniffs/Namespaces/UnusedUsesSniffTest.php +++ b/tests/Sniffs/Namespaces/UnusedUsesSniffTest.php @@ -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); + } + } diff --git a/tests/Sniffs/Namespaces/data/unusedUsesNamespaceOperatorNoErrors.php b/tests/Sniffs/Namespaces/data/unusedUsesNamespaceOperatorNoErrors.php new file mode 100644 index 000000000..97bd73bdc --- /dev/null +++ b/tests/Sniffs/Namespaces/data/unusedUsesNamespaceOperatorNoErrors.php @@ -0,0 +1,12 @@ +