Skip to content

Commit

Permalink
SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false…
Browse files Browse the repository at this point in the history
… positive
  • Loading branch information
kukulich committed May 15, 2023
1 parent cc04334 commit f69e252
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function preg_quote;
use function preg_replace;
use function sprintf;
use function strpos;
use function strtolower;
use function substr;
use const T_DECLARE;
Expand Down Expand Up @@ -187,8 +188,17 @@ public function process(File $phpcsFile, $openTagPointer): void

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

$isPartialUse = false;
foreach ($useStatements as $useStatement) {
$useStatementName = $useStatement->getAlias() ?? $useStatement->getNameAsReferencedInFile();
if (strpos($name, $useStatementName . '\\') === 0) {
$isPartialUse = true;
break;
}
}

$isFullyQualified = NamespaceHelper::isFullyQualifiedName($name)
|| ($namespacePointers === [] && NamespaceHelper::hasNamespace($name));
|| ($namespacePointers === [] && NamespaceHelper::hasNamespace($name) && !$isPartialUse);

$isGlobalFallback = !$isFullyQualified
&& !NamespaceHelper::hasNamespace($name)
Expand Down
12 changes: 12 additions & 0 deletions tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ public function testAllowPartialUses(array $ignoredNames): void
self::assertNoSniffError($report, 7);
}

public function testAllowPartialUsesWithoutNamespace(): void
{
$report = self::checkFile(
__DIR__ . '/data/partialUsesWithoutNamespace.php',
[
'allowPartialUses' => true,
]
);

self::assertNoSniffErrorInFile($report);
}

/**
* @dataProvider dataIgnoredNamesForIrrelevantTests
* @param list<string> $ignoredNames
Expand Down
15 changes: 15 additions & 0 deletions tests/Sniffs/Namespaces/data/partialUsesWithoutNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php // lint >= 8.1

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class MyTestCase extends TestCase
{
private LoggerInterface&m\MockInterface $loggerMock;

protected function setUp(): void
{
$this->loggerMock = m::mock(LoggerInterface::class);
}
}

0 comments on commit f69e252

Please sign in to comment.