From 3cf105766aab84c6e09b92cdd2a14a103a081233 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 14 Jul 2021 15:30:39 +0200 Subject: [PATCH] [VarDumper] Support for intersection types Signed-off-by: Alexander M. Turek --- .github/patch-types.php | 1 + .../VarDumper/Caster/ReflectionCaster.php | 2 +- .../Tests/Caster/ReflectionCasterTest.php | 51 ++++++++++++++++++- .../ReflectionIntersectionTypeFixture.php | 8 +++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php diff --git a/.github/patch-types.php b/.github/patch-types.php index 95e56b5984..e139c2c73e 100644 --- a/.github/patch-types.php +++ b/.github/patch-types.php @@ -41,6 +41,7 @@ foreach ($loader->getClassMap() as $class => $file) { case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php'): case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'): case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'): + case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php'): continue 2; } diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index 95c1dbf6ff..4a1ae4f6b7 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php @@ -102,7 +102,7 @@ class ReflectionCaster $prefix.'allowsNull' => $c->allowsNull(), $prefix.'isBuiltin' => $c->isBuiltin(), ]; - } elseif ($c instanceof \ReflectionUnionType) { + } elseif ($c instanceof \ReflectionUnionType || $c instanceof \ReflectionIntersectionType) { $a[$prefix.'allowsNull'] = $c->allowsNull(); self::addMap($a, $c, [ 'types' => 'getTypes', diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index f9a8bd9440..e4a8974ff1 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -17,6 +17,7 @@ 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\ReflectionIntersectionTypeFixture; use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture; use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture; @@ -78,7 +79,7 @@ Closure($x) { $b: & 123 } file: "%sReflectionCasterTest.php" - line: "71 to 71" + line: "72 to 72" } EOTXT , $var @@ -228,6 +229,26 @@ EOTXT ); } + /** + * @requires PHP 8.1 + */ + public function testReflectionParameterIntersection() + { + $f = eval('return function (Traversable&Countable $a) {};'); + $var = new \ReflectionParameter($f, 0); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionParameter { + +name: "a" + position: 0 + typeHint: "Traversable&Countable" +} +EOTXT + , $var + ); + } + /** * @requires PHP 7.4 */ @@ -292,6 +313,34 @@ EOTXT ); } + /** + * @requires PHP 8.1 + */ + public function testReflectionIntersectionType() + { + $var = (new \ReflectionProperty(ReflectionIntersectionTypeFixture::class, 'a'))->getType(); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionIntersectionType { + allowsNull: false + types: array:2 [ + 0 => ReflectionNamedType { + name: "Traversable" + allowsNull: false + isBuiltin: false + } + 1 => ReflectionNamedType { + name: "Countable" + allowsNull: false + isBuiltin: false + } + ] +} +EOTXT + , $var + ); + } + /** * @requires PHP 8 */ diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php new file mode 100644 index 0000000000..d9d8d21769 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php @@ -0,0 +1,8 @@ +