8000 [VarDumper] Adds support for ReflectionUnionType to VarDumper · symfony/symfony@60f82a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60f82a0

Browse files
committed
[VarDumper] Adds support for ReflectionUnionType to VarDumper
Fixes a bug with VarDumper when dumping a ReflectionUnionType. > PHP Error: Call to undefined method ReflectionUnionType::isBuiltin() Notes: * One of the existing tests relies on its position in the test file. I had to modify its expected line number. * There is an existing trailing space around line 367 in an expected value. I'm not sure if this was left for BC reasons but it seems like a bug if the dumper is leaving trailing spaces.
1 parent 136e54c commit 60f82a0

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,21 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, bool $
9696
{
9797
$prefix = Caster::PREFIX_VIRTUAL;
9898

99-
$a += [
100-
$prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
101-
$prefix.'allowsNull' => $c->allowsNull(),
102-
$prefix.'isBuiltin' => $c->isBuiltin(),
103-
];
99+
if ($c instanceof \ReflectionUnionType) {
100+
$a[$prefix.'allowsNull'] = $c->allowsNull();
101+
self::addMap($a, $c, [
102+
'types' => 'getTypes',
103+
]);
104+
} elseif ($c instanceof \ReflectionNamedType) {
105+
$a += [
106+
$prefix.'name' => $c->getName(),
107+
$prefix.'allowsNull' => $c->allowsNull(),
108+
$prefix.'isBuiltin' => $c->isBuiltin(),
109+
];
110+
} else {
111+
// As of PHP 8.0 there are only two children of ReflectionType (abstract) so this is unreachable.
112+
$a[$prefix.'allowsNull'] = $c->allowsNull();
113+
}
104114

105115
return $a;
106116
}

src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1818
use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes;
1919
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
20+
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionPropertyFixture;
2021

2122
/**
2223
* @author Nicolas Grekas <p@tchwork.com>
@@ -91,7 +92,7 @@ public function testClosureCaster()
9192
$b: & 123
9293
}
9394
file: "%sReflectionCasterTest.php"
94-
line: "84 to 84"
95+
line: "85 to 85"
9596
}
9697
EOTXT
9798
, $var
@@ -227,6 +228,64 @@ public function testReflectionParameterNullableUnion()
227228
);
228229
}
229230

231+
public function testReflectionPropertyScalar()
232+
{
233+
$var = new \ReflectionProperty(ReflectionPropertyFixture::class, 'a');
234+
$this->assertDumpMatchesFormat(
235+
<<<'EOTXT'
236+
ReflectionProperty {
237+
+name: "a"
238+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionPropertyFixture"
239+
modifiers: "public"
240+
}
241+
EOTXT
242+
, $var
243+
);
244+
}
245+
246+
public function testReflectionNamedType()
247+
{
248+
$var = (new \ReflectionProperty(ReflectionPropertyFixture::class, 'a'))->getType();
249+
$this->assertDumpMatchesFormat(
250+
<<<'EOTXT'
251+
ReflectionNamedType {
252+
name: "int"
253+
allowsNull: false
254+
isBuiltin: true
255+
}
256+
EOTXT
257+
, $var
258+
);
259+
}
260+
261+
/**
262+
* @requires PHP 8
263+
*/
264+
public function testReflectionUnionType()
265+
{
266+
$var = (new \ReflectionProperty(ReflectionPropertyFixture::class, 'b'))->getType();
267+
$this->assertDumpMatchesFormat(
268+
<<<'EOTXT'
269+
ReflectionUnionType {
270+
allowsNull: false
271+
types: array:2 [
272+
0 => ReflectionNamedType {
273+
name: "string"
274+
allowsNull: false
275+
isBuiltin: true
276+
}
277+
1 => ReflectionNamedType {
278+
name: "int"
279+
allowsNull: false
280+
isBuiltin: true
281+
}
282+
]
283+
}
284+
EOTXT
285+
, $var
286+
);
287+
}
288+
230289
public function testReturnType()
231290
{
232291
$f = eval('return function ():int {};');
@@ -314,6 +373,7 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
314373

315374
public function testGenerator()
316375
{
376+
317377
if (\extension_loaded('xdebug')) {
318378
$this->markTestSkipped('xdebug is active');
319379
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
5+
6+
class ReflectionPropertyFixture
7+
{
8+
public int $a;
9+
public int|string $b;
10+
}

0 commit comments

Comments
 (0)
0