Skip to content

Commit f69e252

Browse files
committed
SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false positive
1 parent cc04334 commit f69e252

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use function preg_quote;
4141
use function preg_replace;
4242
use function sprintf;
43+
use function strpos;
4344
use function strtolower;
4445
use function substr;
4546
use const T_DECLARE;
@@ -187,8 +188,17 @@ public function process(File $phpcsFile, $openTagPointer): void
187188

188189
$collidingUseStatementUniqueId = UseStatement::getUniqueId($reference->type, $unqualifiedName);
189190

191+
$isPartialUse = false;
192+
foreach ($useStatements as $useStatement) {
193+
$useStatementName = $useStatement->getAlias() ?? $useStatement->getNameAsReferencedInFile();
194+
if (strpos($name, $useStatementName . '\\') === 0) {
195+
$isPartialUse = true;
196+
break;
197+
}
198+
}
199+
190200
$isFullyQualified = NamespaceHelper::isFullyQualifiedName($name)
191-
|| ($namespacePointers === [] && NamespaceHelper::hasNamespace($name));
201+
|| ($namespacePointers === [] && NamespaceHelper::hasNamespace($name) && !$isPartialUse);
192202

193203
$isGlobalFallback = !$isFullyQualified
194204
&& !NamespaceHelper::hasNamespace($name)

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ public function testAllowPartialUses(array $ignoredNames): void
316316
self::assertNoSniffError($report, 7);
317317
}
318318

319+
public function testAllowPartialUsesWithoutNamespace(): void
320+
{
321+
$report = self::checkFile(
322+
__DIR__ . '/data/partialUsesWithoutNamespace.php',
323+
[
324+
'allowPartialUses' => true,
325+
]
326+
);
327+
328+
self::assertNoSniffErrorInFile($report);
329+
}
330+
319331
/**
320332
* @dataProvider dataIgnoredNamesForIrrelevantTests
321333
* @param list<string> $ignoredNames
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.1
2+
3+
use Mockery as m;
4+
use PHPUnit\Framework\TestCase;
5+
use Psr\Log\LoggerInterface;
6+
7+
class MyTestCase extends TestCase
8+
{
9+
private LoggerInterface&m\MockInterface $loggerMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->loggerMock = m::mock(LoggerInterface::class);
14+
}
15+
}

0 commit comments

Comments
 (0)