Skip to content

Commit 01b9e8f

Browse files
committed
ReferenceUsedNamesOnlySniff: importing global functions and constants is not necessary in the global namespace
1 parent 2ce505a commit 01b9e8f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $openTagPointer)
213213
$isFullyQualified = NamespaceHelper::isFullyQualifiedName($name);
214214
$isGlobalFallback = !$isFullyQualified
215215
&& !NamespaceHelper::hasNamespace($name)
216+
&& NamespaceHelper::findCurrentNamespaceName($phpcsFile, $startPointer) !== null
216217
&& !array_key_exists(UseStatement::getUniqueId($reference->type, $name), $useStatements);
217218
$isGlobalFunctionFallback = $reference->isFunction && $isGlobalFallback;
218219
$isGlobalConstantFallback = $reference->isConstant && $isGlobalFallback;

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,4 +847,17 @@ public function testSearchingInAnnotations(): void
847847
self::assertAllFixedInFile($report);
848848
}
849849

850+
public function testReferencingGlobalTypesInGlobalNamespace(): void
851+
{
852+
$report = self::checkFile(
853+
__DIR__ . '/data/referencingGlobalTypesInGlobalNamespace.php',
854+
[
855+
'allowFallbackGlobalFunctions' => false,
856+
'allowFallbackGlobalConstants' => false,
857+
]
858+
);
859+
860+
self::assertNoSniffErrorInFile($report);
861+
}
862+
850863
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
PHP_VERSION;
4+
5+
phpversion();
6+
7+
new DateTimeImmutable();

0 commit comments

Comments
 (0)