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

Skip to content

Commit c121d76

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 bbf786c commit c121d76

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,21 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNes
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
}
@@ -377,7 +387,7 @@ private static function addExtra(array &$a, \Reflector $c)
377387
}
378388
}
379389

380-
private static function addMap(array &$a, \Reflector $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
390+
private static function addMap(array &$a, $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
381391
{
382392
foreach ($map as $k => $m) {
383393
if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1717
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1818
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
19+
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
20+
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;
1921

2022
/**
2123
* @author Nicolas Grekas <p@tchwork.com>
@@ -75,7 +77,7 @@ public function testClosureCaster()
7577
$b: & 123
7678
}
7779
file: "%sReflectionCasterTest.php"
78-
line: "68 to 68"
80+
line: "70 to 70"
7981
}
8082
EOTXT
8183
, $var
@@ -211,6 +213,70 @@ public function testReflectionParameterNullableUnion()
211213
);
212214
}
213215

216+
/**
217+
* @requires PHP 7.4
218+
*/
219+
public function testReflectionPropertyScalar()
220+
{
221+
$var = new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a');
222+
$this->assertDumpMatchesFormat(
223+
<<<'EOTXT'
224+
ReflectionProperty {
225+
+name: "a"
226+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture"
227+
modifiers: "public"
228+
}
229+
EOTXT
230+
, $var
231+
);
232+
}
233+
234+
/**
235+
* @requires PHP 7.4
236+
*/
237+
public function testReflectionNamedType()
238+
{
239+
$var = (new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'))->getType();
240+
$this->assertDumpMatchesFormat(
241+
<<<'EOTXT'
242+
ReflectionNamedType {
243+
name: "int"
244+
allowsNull: false
245+
isBuiltin: true
246+
}
247+
EOTXT
248+
, $var
249+
);
250+
}
251+
252+
/**
253+
* @requires PHP 8
254+
*/
255+
public function testReflectionUnionType()
256+
{
257+
$var = (new \ReflectionProperty(ReflectionUnionTypeFixture::class, 'a'))->getType();
258+
$this->assertDumpMatchesFormat(
259+
<<<'EOTXT'
260+
ReflectionUnionType {
261+
allowsNull: false
262+
types: array:2 [
263+
0 => ReflectionNamedType {
264+
name: "string"
265+
allowsNull: false
266+
isBuiltin: true
267+
}
268+
1 => ReflectionNamedType {
269+
name: "int"
270+
allowsNull: false
271+
isBuiltin: true
272+
}
273+
]
274+
}
275+
EOTXT
276+
, $var
277+
);
278+
}
279+
214280
public function testReturnType()
215281
{
216282
$f = eval('return function ():int {};');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
5+
6+
class ReflectionNamedTypeFixture
7+
{
8+
public int $a;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
5+
6+
class ReflectionUnionTypeFixture
7+
{
8+
public int|string $a;
9+
}

0 commit comments

Comments
 (0)
0