Skip to content

Commit e3b5359

Browse files
Majkl578kukulich
authored andcommitted
UnusedUsesSniff: Fix detection of partially referenced functions and constants
1 parent b07c153 commit e3b5359

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $openTagPointer)
4141
$pointer = $referencedName->getStartPointer();
4242
$nameParts = NamespaceHelper::getNameParts($name);
4343
$nameAsReferencedInFile = $nameParts[0];
44+
$nameReferencedWithoutSubNamespace = count($nameParts) === 1;
4445
$normalizedNameAsReferencedInFile = UseStatement::normalizedNameAsReferencedInFile($nameAsReferencedInFile);
4546
if (
4647
!NamespaceHelper::isFullyQualifiedName($name)
4748
&& isset($unusedNames[$normalizedNameAsReferencedInFile])
4849
) {
49-
if (!$referencedName->hasSameUseStatementType($unusedNames[$normalizedNameAsReferencedInFile])) {
50+
if ($nameReferencedWithoutSubNamespace && !$referencedName->hasSameUseStatementType($unusedNames[$normalizedNameAsReferencedInFile])) {
5051
continue;
5152
}
5253
if ($unusedNames[$normalizedNameAsReferencedInFile]->getNameAsReferencedInFile() !== $nameAsReferencedInFile) {

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public function testReturnTypeHint()
8888
$this->assertNoSniffError($this->getFileReport(), 19);
8989
}
9090

91+
public function testPartialuses()
92+
{
93+
$this->assertNoSniffError($this->getFileReport(), 20);
94+
$this->assertNoSniffError($this->getFileReport(), 21);
95+
$this->assertNoSniffError($this->getFileReport(), 22);
96+
}
97+
9198
public function testUsedUseInAnnotationWithDisabledSearchAnnotations()
9299
{
93100
$report = $this->checkFile(__DIR__ . '/data/unusedUsesInAnnotation.php', [

tests/Sniffs/Namespaces/data/unusedUses.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use Lorem\FirstInterface;
1818
use Ipsum\SecondInterface;
1919
use Zetta\Rasmus;
20+
use My\PartialClass;
21+
use My\PartialFunction;
22+
use My\PartialConstant;
2023

2124
class TestClass implements FirstInterface, SecondInterface
2225
{
@@ -33,6 +36,10 @@ public function test(S $s): Rasmus
3336
UsedFunction();
3437
doFoo(USED_CONSTANT);
3538

39+
new PartialClass\UsedClass();
40+
PartialFunction\usedFunction();
41+
PartialConstant\USED_CONSTANT;
42+
3643
return new NewObject();
3744
}
3845

0 commit comments

Comments
 (0)