Skip to content

Commit 9245fbe

Browse files
committed
UnusedUsesSniff - fixed multiple implemented interfaces marked as unused
1 parent 412afdd commit 9245fbe

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile
9191
T_DOUBLE_COLON,
9292
], true)
9393
) {
94-
$type = ReferencedName::TYPE_CONSTANT;
94+
if ($tokens[$previousTokenBeforeStartPointer]['code'] === T_COMMA) {
95+
$implementsPointer = TokenHelper::findPreviousExcluding(
96+
$phpcsFile,
97+
array_merge(TokenHelper::$nameTokenCodes, TokenHelper::$ineffectiveTokenCodes),
98+
$previousTokenBeforeStartPointer - 1
99+
);
100+
if ($tokens[$implementsPointer]['code'] !== T_IMPLEMENTS) {
101+
$type = ReferencedName::TYPE_CONSTANT;
102+
}
103+
} else {
104+
$type = ReferencedName::TYPE_CONSTANT;
105+
}
95106
}
96107
}
97108
}

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public function testUsedConstant()
7777
$this->assertNoSniffError($this->getFileReport(), 15);
7878
}
7979

80+
public function testUsedClassesInImplements()
81+
{
82+
$this->assertNoSniffError($this->getFileReport(), 17);
83+
$this->assertNoSniffError($this->getFileReport(), 18);
84+
}
85+
8086
public function testReturnTypehint()
8187
{
8288
if (PHP_VERSION_ID < 70000) {

tests/Sniffs/Namespaces/data/unusedUses.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use const FooBar\UNUSED_CONSTANT;
1515
use const LoremIpsum\USED_CONSTANT;
1616
use X;
17+
use Lorem\FirstInterface;
18+
use Ipsum\SecondInterface;
1719

18-
class TestClass
20+
class TestClass implements FirstInterface, SecondInterface
1921
{
2022

2123
/**

0 commit comments

Comments
 (0)