8000 [VarDumper] Adds support for ReflectionUnionType to VarDumper by michaeldnelson · Pull Request #40453 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper] Adds support for ReflectionUnionType to VarDumper #40453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,20 @@ public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNes
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
$prefix.'allowsNull' => $c->allowsNull(),
$prefix.'isBuiltin' => $c->isBuiltin(),
];
if ($c instanceof \ReflectionNamedType || \PHP_VERSION_ID < 80000) {
$a += [
$prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
$prefix.'allowsNull' => $c->allowsNull(),
$prefix.'isBuiltin' => $c->isBuiltin(),
];
} elseif ($c instanceof \ReflectionUnionType) {
$a[$prefix.'allowsNull'] = $c->allowsNull();
self::addMap($a, $c, [
'types' => 'getTypes',
]);
} else {
$a[$prefix.'allowsNull'] = $c->allowsNull();
}

return $a;
}
Expand Down Expand Up @@ -377,7 +386,7 @@ private static function addExtra(array &$a, \Reflector $c)
}
}

private static function addMap(array &$a, \Reflector $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
private static function addMap(array &$a, $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
{
foreach ($map as $k => $m) {
if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {
Expand Down
103 changes: 102 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo;
use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture;
use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture;

/**
* @author Nicolas Grekas <p@tchwork.com>
Expand Down Expand Up @@ -75,7 +78,7 @@ public function testClosureCaster()
$b: & 123
}
file: "%sReflectionCasterTest.php"
line: "68 to 68"
line: "71 to 71"
}
EOTXT
, $var
Expand Down Expand Up @@ -211,6 +214,104 @@ public function testReflectionParameterNullableUnion()
);
}

/**
* @requires PHP 7.4
*/
public function testReflectionPropertyScalar()
{
$var = new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a');
$this->assertDumpMatchesFormat(
<<<'EOTXT'
ReflectionProperty {
+name: "a"
+class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture"
modifiers: "public"
}
EOTXT
, $var
);
}

/**
* @requires PHP 7.4
*/
public function testReflectionNamedType()
{
$var = (new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'))->getType();
$this->assertDumpMatchesFormat(
<<<'EOTXT'
ReflectionNamedType {
name: "int"
allowsNull: false
isBuiltin: true
}
EOTXT
, $var
);
}

/**
* @requires PHP 8
*/
public function testReflectionUnionType()
{
$var = (new \ReflectionProperty(ReflectionUnionTypeFixture::class, 'a'))->getType();
$this->assertDumpMatchesFormat(
<<<'EOTXT'
ReflectionUnionType {
allowsNull: false
types: array:2 [
0 => ReflectionNamedType {
name: "string"
allowsNull: false
isBuiltin: true
}
1 => ReflectionNamedType {
name: "int"
allowsNull: false
isBuiltin: true
}
]
}
EOTXT
, $var
);
}

/**
* @requires PHP 8
*/
public function testExtendsReflectionType()
{
$var = new ExtendsReflectionTypeFixture();
$this->assertDumpMatchesFormat(
<<<'EOTXT'
Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture {
allowsNull: false
}
EOTXT
, $var
);
}

/**
* @requires PHP < 8
*/
public function testLegacyExtendsReflectionType()
{
$var = new ExtendsReflectionTypeFixture();
$this->assertDumpMatchesFormat(
<<<'EOTXT'
Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture {
name: "fake"
allowsNull: false
isBuiltin: false
}
EOTXT
, $var
);
}

public function testReturnType()
{
$f = eval('return function ():int {};');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class ExtendsReflectionTypeFixture extends \ReflectionType
{
public function allowsNull(): bool
{
return false;
}

public function isBuiltin(): bool
{
return false;
}

public function __toString(): string
{
return 'fake';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class ReflectionNamedTypeFixture
{
public int $a;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class ReflectionUnionTypeFixture
{
public int|string $a;
}
0