Skip to content

Commit c8ceb17

Browse files
committed
SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Some references were not detected
1 parent 248eb0a commit c8ceb17

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Util\Tokens;
7+
use function array_key_exists;
78
use function array_merge;
89
use function array_reverse;
910
use function array_values;
@@ -326,7 +327,6 @@ private static function isReferencedName(File $phpcsFile, int $startPointer): bo
326327

327328
$skipTokenCodes = [
328329
T_FUNCTION,
329-
T_AS,
330330
T_DOUBLE_COLON,
331331
T_OBJECT_OPERATOR,
332332
T_NULLSAFE_OBJECT_OPERATOR,
@@ -389,6 +389,11 @@ private static function isReferencedName(File $phpcsFile, int $startPointer): bo
389389
return false;
390390
}
391391

392+
if ($previousToken['code'] === T_AS && !array_key_exists('nested_parenthesis', $previousToken)) {
393+
// "as" in "use" statement
394+
return false;
395+
}
396+
392397
$endPointer = self::getReferencedNameEndPointer($phpcsFile, $startPointer);
393398
$referencedName = self::getReferenceName($phpcsFile, $startPointer, $endPointer);
394399

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public function testGetAllReferencedNames(): void
8282
['object', true, false],
8383
['DateTime', false, false],
8484
['DateTimeImmutable', false, false],
85+
['\ForeachIterator1', false, false],
86+
['\ForeachValue1', false, false],
87+
['\ForeachIterator2', false, false],
88+
['\ForeachKey', false, false],
89+
['\ForeachValue2', false, false],
8590
];
8691

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

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,11 @@ public function generateRoute($router): string
221221
echo "something{$a(
222222
[DateTimeImmutable::class, 'format']
223223
)}";
224+
225+
foreach (\ForeachIterator1::$items as \ForeachValue1::$value) {
226+
227+
}
228+
229+
foreach (\ForeachIterator2::$items as \ForeachKey::$key => \ForeachValue2::$value) {
230+
231+
}

tests/Sniffs/Namespaces/data/fixableReferenceViaFullyQualifiedName.fixed.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use const Boo\FOO;
1212
use function min;
1313
use const PHP_VERSION;
14+
use Foo\BarTwo;
1415

1516
class Bar extends ObjectPrototype implements Iterator
1617
{
@@ -27,6 +28,12 @@ public function bar()
2728
FOO;
2829
min(1, 2);
2930
PHP_VERSION;
31+
foreach (BarTwo::$values as BarTwo::$value) {
32+
echo BarTwo::$value;
33+
}
34+
foreach (BarTwo::$values as BarTwo::$key => BarTwo::$value) {
35+
echo BarTwo::$key . BarTwo::$value;
36+
}
3037
}
3138

3239
public function foo(DoctrineColumn $doctrineColumn): \Nette\ObjectPrototype

tests/Sniffs/Namespaces/data/fixableReferenceViaFullyQualifiedName.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function bar()
1919
\Boo\FOO;
2020
min(1, 2);
2121
PHP_VERSION;
22+
foreach (\Foo\BarTwo::$values as \Foo\BarTwo::$value) {
23+
echo \Foo\BarTwo::$value;
24+
}
25+
foreach (\Foo\BarTwo::$values as \Foo\BarTwo::$key => \Foo\BarTwo::$value) {
26+
echo \Foo\BarTwo::$key . \Foo\BarTwo::$value;
27+
}
2228
}
2329

2430
public function foo(\Doctrine\ORM\Mapping\Column $doctrineColumn): \Nette\ObjectPrototype

0 commit comments

Comments
 (0)