8000 This commit, adds extended reflectiontype support and removes a very … · symfony/symfony@d3e73af · GitHub
[go: up one dir, main page]

Skip to content

Commit d3e73af

Browse files
committed
This commit, adds extended reflectiontype support and removes a very very small possibility for a bc break in versions < 8
1 parent a0fd31c commit d3e73af

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,20 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNes
9696
{
9797
$prefix = Caster::PREFIX_VIRTUAL;
9898

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) {
99+
100+
if ($c instanceof \ReflectionNamedType || PHP_VERSION_ID < 80000) {
105101
$a += [
106-
$prefix.'name' => $c->getName(),
102+
$prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
107103
$prefix.'allowsNull' => $c->allowsNull(),
108104
$prefix.'isBuiltin' => $c->isBuiltin(),
109105
];
106+
} elseif ($c instanceof \ReflectionUnionType) {
107+
$a[$prefix . 'allowsNull'] = $c->allowsNull();
108+
self::addMap($a, $c, [
109+
'types' => 'getTypes',
110+
]);
110111
} else {
111-
// As of PHP 8.0 there are only two children of ReflectionType (abstract) so this is unreachable.
112+
//Userland may extend reflection type.
112113
$a[$prefix.'allowsNull'] = $c->allowsNull();
113114
}
114115

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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;
1920
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
@@ -77,7 +78,7 @@ public function testClosureCaster()
7778
$b: & 123
7879
}
7980
file: "%sReflectionCasterTest.php"
80-
line: "70 to 70"
81+
line: "71 to 71"
8182
}
8283
EOTXT
8384
, $var
@@ -277,6 +278,40 @@ public function testReflectionUnionType()
277278
);
278279
}
279280

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+
280315
public function testReturnType()
281316
{
282317
$f = eval('return function ():int {};');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
4+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
5+
6+
class ExtendsReflectionTypeFixture extends \ReflectionType
7+
{
8+
public function allowsNull()
9+
{
10+
return false;
11+
}
12+
13+
public function isBuiltin()
14+
{
15+
return true;
16+
}
17+
18+
public function __toString()
19+
{
20+
return 'fake';
21+
}
22+
}

0 commit comments

Comments
 (0)
0