Skip to content

Commit a9af14f

Browse files
committed
UnusedUsesSniff: Fixed false positive
1 parent 7bb28fe commit a9af14f

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use const T_OPEN_PARENTHESIS;
3838
use const T_OPEN_SHORT_ARRAY;
3939
use const T_OPEN_TAG;
40+
use const T_PARAM_NAME;
4041
use const T_TRAIT;
4142
use const T_TYPE_UNION;
4243
use const T_USE;
@@ -207,6 +208,17 @@ private static function getReferenceType(File $phpcsFile, int $nameStartPointer,
207208
return ReferencedName::TYPE_CLASS;
208209
}
209210

211+
if ($tokens[$previousTokenBeforeStartPointer]['code'] === T_COLON) {
212+
$previousTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $previousTokenBeforeStartPointer - 1);
213+
214+
if ($tokens[$previousTokenPointer]['code'] === T_PARAM_NAME) {
215+
return ReferencedName::TYPE_CONSTANT;
216+
}
217+
218+
// Return type hint
219+
return ReferencedName::TYPE_CLASS;
220+
}
221+
210222
if (
211223
in_array($tokens[$previousTokenBeforeStartPointer]['code'], [
212224
T_EXTENDS,
@@ -215,8 +227,6 @@ private static function getReferenceType(File $phpcsFile, int $nameStartPointer,
215227
// Trait
216228
T_USE,
217229
T_NEW,
218-
// Return type hint
219-
T_COLON,
220230
// Nullable type hint
221231
T_NULLABLE,
222232
], true)

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function testGetAllReferencedNames(): void
6363
['\Union\UnionType4', false, false],
6464
['UnionType5', false, false],
6565
['UnionType6', false, false],
66+
['str_pad', true, false],
67+
['STR_PAD_RIGHT', false, true],
6668
];
6769

6870
$names = ReferencedNameHelper::getAllReferencedNames($phpcsFile, 0);

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function DI\string;
1010
use function doSomething;
1111
use function stream_wrapper_restore;
12+
use const STR_PAD_RIGHT;
1213
use const STREAM_URL_STAT_QUIET;
1314

1415
class FooClass extends \ExtendedClass implements \ImplementedInterface, \SecondImplementedInterface, \ThirdImplementedInterface
@@ -161,3 +162,5 @@ public function call2(bool|UnionType6 $union)
161162
}
162163

163164
}
165+
166+
echo str_pad('123', 1, pad_type: STR_PAD_RIGHT);

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function testUsedFunction(): void
8383
public function testUsedConstant(): void
8484
{
8585
self::assertNoSniffError($this->getFileReport(), 15);
86+
self::assertNoSniffError($this->getFileReport(), 26);
8687
}
8788

8889
public function testUsedClassesInImplements(): void

tests/Sniffs/Namespaces/data/unusedUses.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PartialNamespace;
2424
use UnionType;
2525
use UnionType2;
26+
use const STR_PAD_RIGHT;
2627

2728
class TestClass implements FirstInterface, SecondInterface
2829
{
@@ -44,6 +45,8 @@ public function test(S $s): Rasmus
4445
PartialConstant\USED_CONSTANT;
4546
new PartialNamespace\UsedClass();
4647

48+
echo str_pad('123', 1, pad_type: STR_PAD_RIGHT);
49+
4750
return new NewObject();
4851
}
4952

0 commit comments

Comments
 (0)