Skip to content

Commit 74b296b

Browse files
c01lkukulich
authored andcommitted
Fix class detection in double-quoted strings and heredoc
1 parent d82a671 commit 74b296b

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
use const T_HEREDOC;
3636
use const T_IMPLEMENTS;
3737
use const T_INSTANCEOF;
38+
use const T_NAME_FULLY_QUALIFIED;
39+
use const T_NAME_QUALIFIED;
40+
use const T_NAME_RELATIVE;
3841
use const T_NAMESPACE;
3942
use const T_NEW;
4043
use const T_NS_SEPARATOR;
@@ -51,6 +54,7 @@
5154
use const T_TYPE_UNION;
5255
use const T_USE;
5356
use const T_VARIABLE;
57+
use const T_WHITESPACE;
5458

5559
/**
5660
* @internal
@@ -494,6 +498,31 @@ private static function getReferencedNamesFromString(string $content): array
494498
}
495499

496500
$referencedNames[] = $referencedName;
501+
} elseif (is_array($token) && $token[0] === T_NEW) {
502+
$referencedName = '';
503+
$tmpPosition = $position + 1;
504+
while (true) {
505+
if (!is_array($subTokens[$tmpPosition])) {
506+
break;
507+
}
508+
if ($subTokens[$tmpPosition][0] === T_WHITESPACE) {
509+
$tmpPosition++;
510+
continue;
511+
}
512+
if (!in_array(
513+
$subTokens[$tmpPosition][0],
514+
[T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED, T_NAME_RELATIVE],
515+
true
516+
)) {
517+
break;
518+
}
519+
520+
$referencedName .= $subTokens[$tmpPosition][1];
521+
$tmpPosition++;
522+
}
523+
if ($referencedName !== '') {
524+
$referencedNames[] = $referencedName;
525+
}
497526
}
498527
}
499528

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public function testGetAllReferencedNames(): void
7676
['EnumType', false, false],
7777
['UrlGeneratorInterface', false, false],
7878
['ClassInHeredoc', false, false],
79+
['\Some\OtherClassInHeredoc', false, false],
7980
['ClassInDoubleQuote', false, false],
81+
['\Some\OtherClassInDoubleQuote', false, false],
8082
['object', true, false],
8183
['DateTime', false, false],
8284
['DateTimeImmutable', false, false],

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ public function generateRoute($router): string
196196

197197
<<<XML
198198
<string>Hello world and {$this->wrap(ClassInHeredoc::EXAMPLE)}</string>
199+
{$_(new \Some\OtherClassInHeredoc())}
199200
XML;
200201

201202
"foo {$db->quote(ClassInDoubleQuote::SOME_CONSTANT)}";
202203
"foo $db->quote(FakeClassInDoubleQuote::SOME_CONSTANT)";
204+
"{$_(new \Some\OtherClassInDoubleQuote ?? "")}";
203205

204206
$script .= "// @see \Foo\Bar::func()
205207
\$hasDefaultValue = $hasDefaultValue;

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function testUnusedUse(): void
7070
self::assertNoSniffError($report, 30);
7171
self::assertNoSniffError($report, 31);
7272

73+
// Used class inside a string or heredoc
74+
self::assertNoSniffError($report, 31);
75+
self::assertNoSniffError($report, 32);
76+
7377
self::assertNoSniffError($report, 91);
7478
}
7579

tests/Sniffs/Namespaces/data/unusedUses.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use ClassWithStaticVariable;
3030
use ClassWithConstant;
3131
use function Psl\Type\null;
32+
use InstantiableClass1;
33+
use InstantiableClass2;
3234

3335
class TestClass implements FirstInterface, SecondInterface
3436
{
@@ -60,6 +62,11 @@ enum_type: EnumClass::VALUE(),
6062

6163
echo "test {$wrapper->escape(ClassWithConstant::FOO)}";
6264

65+
echo "{$_(new InstantiableClass1())}";
66+
echo <<<DOC
67+
{$_(new InstantiableClass2())}
68+
DOC;
69+
6370
return new NewObject();
6471
}
6572

0 commit comments

Comments
 (0)