Skip to content

Commit 8f2307e

Browse files
mvorisekondrejmirtes
authored andcommitted
Display parent class name for anonymous class like native php does
1 parent 9a9abe3 commit 8f2307e

File tree

8 files changed

+90
-9
lines changed

8 files changed

+90
-9
lines changed

src/Reflection/BetterReflection/BetterReflectionProvider.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use PHPStan\Type\Generic\TemplateTypeMap;
5252
use PHPStan\Type\Type;
5353
use function array_key_exists;
54+
use function array_key_first;
5455
use function array_map;
5556
use function base64_decode;
5657
use function in_array;
@@ -217,12 +218,23 @@ public function getAnonymousClassReflection(Node\Stmt\Class_ $classNode, Scope $
217218
null,
218219
);
219220

221+
$displayParentName = $reflectionClass->getParentClassName();
222+
if ($displayParentName === null) {
223+
// https://3v4l.org/6FBuP
224+
$classInterfaceNames = $reflectionClass->getInterfaceNames();
225+
if ($classInterfaceNames !== []) {
226+
$displayParentName = $classInterfaceNames[array_key_first($classInterfaceNames)];
227+
} else {
228+
$displayParentName = 'class';
229+
}
230+
}
231+
220232
/** @var int|null $classLineIndex */
221233
$classLineIndex = $classNode->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX);
222234
if ($classLineIndex === null) {
223-
$displayName = sprintf('class@anonymous/%s:%s', $filename, $classNode->getStartLine());
235+
$displayName = sprintf('%s@anonymous/%s:%s', $displayParentName, $filename, $classNode->getStartLine());
224236
} else {
225-
$displayName = sprintf('class@anonymous/%s:%s:%d', $filename, $classNode->getStartLine(), $classLineIndex);
237+
$displayName = sprintf('%s@anonymous/%s:%s:%d', $displayParentName, $filename, $classNode->getStartLine(), $classLineIndex);
226238
}
227239

228240
self::$anonymousClasses[$className] = new ClassReflection(

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ public function testBug6442(): void
541541
{
542542
$errors = $this->runAnalyse(__DIR__ . '/data/bug-6442.php');
543543
$this->assertCount(2, $errors);
544-
$this->assertSame('Dumped type: \'Bug6442\\\A\'', $errors[0]->getMessage());
544+
$this->assertSame('Dumped type: \'Bug6442\\\B\'', $errors[0]->getMessage());
545545
$this->assertSame(9, $errors[0]->getLine());
546-
$this->assertSame('Dumped type: \'Bug6442\\\B\'', $errors[1]->getMessage());
546+
$this->assertSame('Dumped type: \'Bug6442\\\A\'', $errors[1]->getMessage());
547547
$this->assertSame(9, $errors[1]->getLine());
548548
}
549549

tests/PHPStan/Levels/data/stubValidator-0.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
"ignorable": false
2121
},
2222
{
23-
"message": "Method class@anonymous/stubValidator/stubs.php:27::doFoo() has no return type specified.",
23+
"message": "Method ArrayIterator@anonymous/stubValidator/stubs.php:27::doFoo() has no return type specified.",
2424
"line": 30,
2525
"ignorable": false
2626
},
2727
{
28-
"message": "Parameter $foo of method class@anonymous/stubValidator/stubs.php:27::doFoo() has invalid type StubValidator\\Foooooooo.",
28+
"message": "Parameter $foo of method ArrayIterator@anonymous/stubValidator/stubs.php:27::doFoo() has invalid type StubValidator\\Foooooooo.",
2929
"line": 30,
3030
"ignorable": false
3131
}

tests/PHPStan/Reflection/AnonymousClassReflectionTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,55 @@ public function testReflection(): void
101101
]),
102102
9,
103103
],
104+
[
105+
implode("\n", [
106+
'name: AnonymousClass1d622e3ff3a656e68d55eafbd25eaef1',
107+
'display name: AnonymousClassReflectionTest\A@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:17:1',
108+
]),
109+
17,
110+
],
111+
[
112+
implode("\n", [
113+
'name: AnonymousClass6e1acc8e948827c8d0439a2225fdbdd0',
114+
'display name: AnonymousClassReflectionTest\A@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:17:2',
115+
]),
116+
17,
117+
],
118+
[
119+
implode("\n", [
120+
'name: AnonymousClass2a49db3d44479dddd8beaea4ea8131fb',
121+
'display name: AnonymousClassReflectionTest\A@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:19:1',
122+
]),
123+
19,
124+
],
125+
[
126+
implode("\n", [
127+
'name: AnonymousClass337463cf86ee25e526f445630960b336',
128+
'display name: AnonymousClassReflectionTest\A@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:19:2',
129+
]),
130+
19,
131+
],
132+
[
133+
implode("\n", [
134+
'name: AnonymousClassda3e79cc45f826d60295f848abab37e7',
135+
'display name: AnonymousClassReflectionTest\U@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:29',
136+
]),
137+
29,
138+
],
139+
[
140+
implode("\n", [
141+
'name: AnonymousClassc06612bf3776bbe5e50870a8c3151186',
142+
'display name: AnonymousClassReflectionTest\U@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:31',
143+
]),
144+
31,
145+
],
146+
[
147+
implode("\n", [
148+
'name: AnonymousClassbee6eba8c721d73d649fcc9d361f5902',
149+
'display name: AnonymousClassReflectionTest\V@anonymous/tests/PHPStan/Reflection/data/anonymous-classes.php:33',
150+
]),
151+
33,
152+
],
104153
]);
105154
}
106155

tests/PHPStan/Reflection/data/anonymous-classes.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,23 @@ public function __construct(object $object)
1111
{
1212
}
1313
};
14+
15+
class A {}
16+
17+
new class extends A {}; new class extends A {};
18+
19+
new class (new class extends A {}) extends A {
20+
public function __construct(object $object)
21+
{
22+
}
23+
};
24+
25+
interface U {}
26+
27+
interface V {}
28+
29+
new class implements U {};
30+
31+
new class implements U, V {};
32+
33+
new class implements V, U {};

tests/PHPStan/Rules/Classes/RequireImplementsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testRule(): void
5757
137,
5858
],
5959
[
60-
'Trait IncompatibleRequireImplements\ValidPsalmTrait requires using class to implement IncompatibleRequireImplements\RequiredInterface2, but class@anonymous/tests/PHPStan/Rules/PhpDoc/data/incompatible-require-implements.php:164 does not.',
60+
'Trait IncompatibleRequireImplements\ValidPsalmTrait requires using class to implement IncompatibleRequireImplements\RequiredInterface2, but IncompatibleRequireImplements\RequiredInterface@anonymous/tests/PHPStan/Rules/PhpDoc/data/incompatible-require-implements.php:164 does not.',
6161
164,
6262
],
6363
[

tests/PHPStan/Rules/Methods/MissingMethodImplementationRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testRule(): void
2929
24,
3030
],
3131
[
32-
'Non-abstract class class@anonymous/tests/PHPStan/Rules/Methods/data/missing-method-impl.php:41 contains abstract method doFoo() from interface MissingMethodImpl\Foo.',
32+
'Non-abstract class MissingMethodImpl\Foo@anonymous/tests/PHPStan/Rules/Methods/data/missing-method-impl.php:41 contains abstract method doFoo() from interface MissingMethodImpl\Foo.',
3333
41,
3434
],
3535
]);

tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function testRedeclaredReadonlyProperties(): void
307307
70,
308308
],
309309
[
310-
'Readonly property class@anonymous/tests/PHPStan/Rules/Properties/data/redeclare-readonly-property.php:117::$myProp is already assigned.',
310+
'Readonly property RedeclareReadonlyProperty\A@anonymous/tests/PHPStan/Rules/Properties/data/redeclare-readonly-property.php:117::$myProp is already assigned.',
311311
121,
312312
],
313313
[

0 commit comments

Comments
 (0)