From efeb2dcf70754ef07fda543d312fa47505bf01ed Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 26 Dec 2020 14:58:16 +0100 Subject: [PATCH] [VarDumper] Fix display of nullable union return types. --- .../VarDumper/Caster/ReflectionCaster.php | 2 +- .../Tests/Caster/ReflectionCasterTest.php | 128 ++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index 6b6d8943b6..4634335fe6 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php @@ -183,7 +183,7 @@ class ReflectionCaster if (isset($a[$prefix.'returnType'])) { $v = $a[$prefix.'returnType']; $v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v; - $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']); + $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']); } if (isset($a[$prefix.'class'])) { $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']); diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index a4183e21af..e1214765ef 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -149,6 +149,68 @@ EOTXT ); } + /** + * @requires PHP 8 + */ + public function testReflectionParameterMixed() + { + $f = eval('return function (mixed $a) {};'); + $var = new \ReflectionParameter($f, 0); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionParameter { + +name: "a" + position: 0 + allowsNull: true + typeHint: "mixed" +} +EOTXT + , $var + ); + } + + /** + * @requires PHP 8 + */ + public function testReflectionParameterUnion() + { + $f = eval('return function (int|float $a) {};'); + $var = new \ReflectionParameter($f, 0); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionParameter { + +name: "a" + position: 0 + typeHint: "int|float" +} +EOTXT + , $var + ); + } + + /** + * @requires PHP 8 + */ + public function testReflectionParameterNullableUnion() + { + $f = eval('return function (int|float|null $a) {};'); + $var = new \ReflectionParameter($f, 0); + + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionParameter { + +name: "a" + position: 0 + allowsNull: true + typeHint: "int|float|null" +} +EOTXT + , $var + ); + } + public function testReturnType() { $f = eval('return function ():int {};'); @@ -168,6 +230,72 @@ EOTXT ); } + /** + * @requires PHP 8 + */ + public function testMixedReturnType() + { + $f = eval('return function (): mixed {};'); + $line = __LINE__ - 1; + + $this->assertDumpMatchesFormat( + <<assertDumpMatchesFormat( + <<assertDumpMatchesFormat( + <<