From dc083fe961f8fe987a2673f02f06407eb8c663c8 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 --- .../VarDumper/Caster/ReflectionCaster.php | 4 ++-- .../Tests/Caster/ReflectionCasterTest.php | 23 ++++++++++++++++++- .../Php82NullStandaloneReturnType.php | 20 ++++++++++++++++ .../Component/VarExporter/ProxyHelper.php | 2 +- .../Php82NullStandaloneReturnType.php | 20 ++++++++++++++++ .../VarExporter/Tests/ProxyHelperTest.php | 12 ++++++++++ 6 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/Php82NullStandaloneReturnType.php create mode 100644 src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/Php82NullStandaloneReturnType.php diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index 45397e965224..ec09da4fef3d 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/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/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index eac092f29f0d..10fff190a37c 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/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/src/Symfony/Component/VarDumper/Tests/Fixtures/Php82NullStandaloneReturnType.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/Php82NullStandaloneReturnType.php new file mode 100644 index 000000000000..05e8385d39b8 --- /dev/null +++ b/src/Symfony/Component/VarDumper/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; + } +} diff --git a/src/Symfony/Component/VarExporter/ProxyHelper.php b/src/Symfony/Component/VarExporter/ProxyHelper.php index 51e0d2afa7df..9105d0c71dad 100644 --- a/src/Symfony/Component/VarExporter/ProxyHelper.php +++ b/src/Symfony/Component/VarExporter/ProxyHelper.php @@ -311,7 +311,7 @@ public static function exportType(\ReflectionFunctionAbstract|\ReflectionPropert return ''; } if (null === $glue) { - return (!$noBuiltin && $type->allowsNull() && 'mixed' !== $name ? '?' : '').$types[0]; + return (!$noBuiltin && $type->allowsNull() && !\in_array($name, ['mixed', 'null'], true) ? '?' : '').$types[0]; } sort($types); diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/Php82NullStandaloneReturnType.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/Php82NullStandaloneReturnType.php new file mode 100644 index 000000000000..f01f573f105b --- /dev/null +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/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\VarExporter\Tests\Fixtures\LazyProxy; + +class Php82NullStandaloneReturnType +{ + public function foo(): null + { + return null; + } +} diff --git a/src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php b/src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php index 806c5c967d24..c10f47d495d8 100644 --- a/src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php +++ b/src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\VarExporter\Exception\LogicException; use Symfony\Component\VarExporter\ProxyHelper; +use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Php82NullStandaloneReturnType; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass; class ProxyHelperTest extends TestCase @@ -188,6 +189,17 @@ public function testCannotGenerateGhostForStringMagicGet() $this->expectException(LogicException::class); ProxyHelper::generateLazyGhost(new \ReflectionClass(StringMagicGetClass::class)); } + + /** + * @requires PHP 8.2 + */ + public function testNullStandaloneReturnType() + { + self::assertStringContainsString( + 'public function foo(): null', + ProxyHelper::generateLazyProxy(new \ReflectionClass(Php82NullStandaloneReturnType::class)) + ); + } } abstract class TestForProxyHelper pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy