From 41c4628888427967b45f1e967056b5fdd7b922bf Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Mon, 6 May 2024 16:25:16 +0200 Subject: [PATCH] [VarExporter] fix proxy helper when a method returns null --- Caster/ReflectionCaster.php | 4 ++-- Tests/Caster/ReflectionCasterTest.php | 23 ++++++++++++++++++- .../Php82NullStandaloneReturnType.php | 20 ++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Tests/Fixtures/Php82NullStandaloneReturnType.php diff --git a/Caster/ReflectionCaster.php b/Caster/ReflectionCaster.php index 45397e9..ec09da4 100644 --- a/Caster/ReflectionCaster.php +++ b/Caster/ReflectionCaster.php @@ -231,7 +231,7 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra 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'] instanceof \ReflectionNamedType && $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() && !\in_array($v, ['mixed', 'null'], true) ? '?'.$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']); @@ -413,7 +413,7 @@ public static function getSignature(array $a) if (!$type instanceof \ReflectionNamedType) { $signature .= $type.' '; } else { - if ($param->allowsNull() && 'mixed' !== $type->getName()) { + if ($param->allowsNull() && !\in_array($type->getName(), ['mixed', 'null'], true)) { $signature .= '?'; } $signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' '; diff --git a/Tests/Caster/ReflectionCasterTest.php b/Tests/Caster/ReflectionCasterTest.php index eac092f..10fff19 100644 --- a/Tests/Caster/ReflectionCasterTest.php +++ b/Tests/Caster/ReflectionCasterTest.php @@ -18,6 +18,7 @@ use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo; use Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes; use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass; +use Symfony\Component\VarDumper\Tests\Fixtures\Php82NullStandaloneReturnType; use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionIntersectionTypeFixture; use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture; use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture; @@ -95,7 +96,7 @@ public function testClosureCaster() $b: & 123 } file: "%sReflectionCasterTest.php" - line: "88 to 88" + line: "%s" } EOTXT , $var @@ -406,6 +407,26 @@ class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest" ); } + /** + * @requires PHP 8.2 + */ + public function testNullReturnType() + { + $className = Php82NullStandaloneReturnType::class; + + $this->assertDumpMatchesFormat( + <<foo(...) + ); + } + public function testUnionReturnType() { $f = function (): int|float {}; diff --git a/Tests/Fixtures/Php82NullStandaloneReturnType.php b/Tests/Fixtures/Php82NullStandaloneReturnType.php new file mode 100644 index 0000000..05e8385 --- /dev/null +++ b/Tests/Fixtures/Php82NullStandaloneReturnType.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Fixtures; + +class Php82NullStandaloneReturnType +{ + public function foo(null $bar): null + { + return null; + } +}