Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1a11491

Browse filesBrowse files
mndevelfabpot
authored andcommitted
[VarDumper] Adds support for ReflectionUnionType to VarDumper
1 parent bbf786c commit 1a11491
Copy full SHA for 1a11491

File tree

Expand file treeCollapse file tree

5 files changed

+154
-7
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+154
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
+15-6Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,20 @@ 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 \ReflectionNamedType || \PHP_VERSION_ID < 80000) {
100+
$a += [
101+
$prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
102+
$prefix.'allowsNull' => $c->allowsNull(),
103+
$prefix.'isBuiltin' => $c->isBuiltin(),
104+
];
105+
} elseif ($c instanceof \ReflectionUnionType) {
106+
$a[$prefix.'allowsNull'] = $c->allowsNull();
107+
self::addMap($a, $c, [
108+
'types' => 'getTypes',
109+
]);
110+
} else {
111+
$a[$prefix.'allowsNull'] = $c->allowsNull();
112+
}
104113

105114
return $a;
106115
}
@@ -377,7 +386,7 @@ private static function addExtra(array &$a, \Reflector $c)
377386
}
378387
}
379388

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
+102-1Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\VarDumper\Caster\Caster;
1616
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
17+
use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture;
1718
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
1819
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
20+
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
21+
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;
1922

2023
/**
2124
* @author Nicolas Grekas <p@tchwork.com>
@@ -75,7 +78,7 @@ public function testClosureCaster()
7578
$b: & 123
7679
}
7780
file: "%sReflectionCasterTest.php"
78-
line: "68 to 68"
81+
line: "71 to 71"
7982
}
8083
EOTXT
8184
, $var
@@ -211,6 +214,104 @@ public function testReflectionParameterNullableUnion()
211214
);
212215
}
213216

217+
/**
218+
* @requires PHP 7.4
219+
*/
220+
public function testReflectionPropertyScalar()
221+
{
222+
$var = new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a');
223+
$this->assertDumpMatchesFormat(
224+
<<<'EOTXT'
225+
ReflectionProperty {
226+
+name: "a"
227+
+class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture"
228+
modifiers: "public"
229+
}
230+
EOTXT
231+
, $var
232+
);
233+
}
234+
235+
/**
236+
* @requires PHP 7.4
237+
*/
238+
public function testReflectionNamedType()
239+
{
240+
$var = (new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'))->getType();
241+
$this->assertDumpMatchesFormat(
242+
<<<'EOTXT'
243+
ReflectionNamedType {
244+
name: "int"
245+
allowsNull: false
246+
isBuiltin: true
247+
}
248+
EOTXT
249+
, $var
250+
);
251+
}
252+
253+
/**
254+
* @requires PHP 8
255+
*/
256+
public function testReflectionUnionType()
257+
{
258+
$var = (new \ReflectionProperty(ReflectionUnionTypeFixture::class, 'a'))->getType();
259+
$this->assertDumpMatchesFormat(
260+
<<<'EOTXT'
261+
ReflectionUnionType {
262+
allowsNull: false
263+
types: array:2 [
264+
0 => ReflectionNamedType {
265+
name: "string"
266+
allowsNull: false
267+
isBuiltin: true
268+
}
269+
1 => ReflectionNamedType {
270+
name: "int"
271+
allowsNull: false
272+
isBuiltin: true
273+
}
274+
]
275+
}
276+
EOTXT
277+
, $var
278+
);
279+
}
280+
281+
/**
282+
* @requires PHP 8
283+
*/
284+
public function testExtendsReflectionType()
285+
{
286+
$var = new ExtendsReflectionTypeFixture();
287+
$this->assertDumpMatchesFormat(
288+
<<<'EOTXT'
289+
Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture {
290+
allowsNull: false
291+
}
292+
EOTXT
293+
, $var
294+
);
295+
}
296+
297+
/**
298+
* @requires PHP < 8
299+
*/
300+
public function testLegacyExtendsReflectionType()
301+
{
302+
$var = new ExtendsReflectionTypeFixture();
303+
$this->assertDumpMatchesFormat(
304+
<<<'EOTXT'
305+
Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture {
306+
name: "fake"
307+
allowsNull: false
308+
isBuiltin: false
309+
}
310+
EOTXT
311+
, $var
312+
);
313+
}
314+
214315
public function testReturnType()
215316
{
216317
$f = eval('return function ():int {};');
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class ExtendsReflectionTypeFixture extends \ReflectionType
6+
{
7+
public function allowsNull(): bool
8+
{
9+
return false;
10+
}
11+
12+
public function isBuiltin(): bool
13+
{
14+
return false;
15+
}
16+
17+
public function __toString(): string
18+
{
19+
return 'fake';
20+
}
21+
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class ReflectionNamedTypeFixture
6+
{
7+
public int $a;
8+
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class ReflectionUnionTypeFixture
6+
{
7+
public int|string $a;
8+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.