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

Skip to content

Commit 2cf92fc

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 2cf92fc

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-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, object $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: 61 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,64 @@ public function testReflectionParameterNullableUnion()
211213
);
212214
}
213215

216+
public function testReflectionPropertyScalar()
217+
{
218+
$var = new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a');
219+
$this->assertDumpMatchesFormat(
220+
<<<'EOTXT'
221+
ReflectionProperty {
222+
+name: "a"
223+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture"
224+
modifiers: "public"
225+
}
226+
EOTXT
227+
, $var
228+
);
229+
}
230+
231+
public function testReflectionNamedType()
232+
{
233+
$var = (new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'))->getType();
234+
$this->assertDumpMatchesFormat(
235+
<<<'EOTXT'
236+
ReflectionNamedType {
237+
name: "int"
238+
allowsNull: false
239+
isBuiltin: true
240+
}
241+
EOTXT
242+
, $var
243+
);
244+
}
245+
246+
/**
247+
* @requires PHP 8
248+
*/
249+
public function testReflectionUnionType()
250+
{
251+
$var = (new \ReflectionProperty(ReflectionUnionTypeFixture::class, 'a'))->getType();
252+
$this->assertDumpMatchesFormat(
253+
<<<'EOTXT'
254+
ReflectionUnionType {
255+
allowsNull: false
256+
types: array:2 [
257+
0 => ReflectionNamedType {
258+
name: "string"
259+
allowsNull: false
260+
isBuiltin: true
261+
}
262+
1 => ReflectionNamedType {
263+
name: "int"
264+
allowsNull: false
265+
isBuiltin: true
266+
}
267+
]
268+
}
269+
EOTXT
270+
, $var
271+
);
272+
}
273+
214274
public function testReturnType()
215275
{
216276
$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