From 6630f5e06e5c80ae7576f1224c099e96607010f6 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Thu, 20 Jun 2024 09:38:29 +0200 Subject: [PATCH] [VarExporter] generate __doUnserialize() method in ProxyHelper::generateLazyProxy() --- .../Component/VarExporter/ProxyHelper.php | 27 ++++++++++- .../VarExporter/Tests/ProxyHelperTest.php | 45 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/VarExporter/ProxyHelper.php b/src/Symfony/Component/VarExporter/ProxyHelper.php index 8d19d38373ce4..d5a8d7418b807 100644 --- a/src/Symfony/Component/VarExporter/ProxyHelper.php +++ b/src/Symfony/Component/VarExporter/ProxyHelper.php @@ -197,10 +197,35 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf $body = $methods ? "\n".implode("\n\n", $methods)."\n" : ''; $propertyScopes = $class ? self::exportPropertyScopes($class->name) : '[]'; + if ( + $class?->hasMethod('__unserialize') + && !$class->getMethod('__unserialize')->getParameters()[0]->getType() + ) { + // fix contravariance type problem when $class declares a `__unserialize()` method without typehint. + $lazyProxyTraitStatement = <<__doUnserialize(\$data); + } + + EOPHP; + } else { + $lazyProxyTraitStatement = <<assertSame($expected, ProxyHelper::generateLazyProxy(null, [new \ReflectionClass(TestForProxyHelperInterface1::class), new \ReflectionClass(TestForProxyHelperInterface2::class)])); } + /** + * @dataProvider classWithUnserializeMagicMethodProvider + */ + public function testGenerateLazyProxyForClassWithUnserializeMagicMethod(object $obj, string $expected) + { + $this->assertStringContainsString($expected, ProxyHelper::generateLazyProxy(new \ReflectionClass($obj::class))); + } + + public static function classWithUnserializeMagicMethodProvider(): iterable + { + yield 'not type hinted __unserialize method' => [new class() { + public function __unserialize($array) + { + } + }, <<<'EOPHP' + implements \Symfony\Component\VarExporter\LazyObjectInterface + { + use \Symfony\Component\VarExporter\LazyProxyTrait { + __unserialize as private __doUnserialize; + } + + private const LAZY_OBJECT_PROPERTY_SCOPES = []; + + public function __unserialize($data): void + { + $this->__doUnserialize($data); + } + } + EOPHP]; + + yield 'type hinted __unserialize method' => [new class() { + public function __unserialize(array $array) + { + } + }, <<<'EOPHP' + implements \Symfony\Component\VarExporter\LazyObjectInterface + { + use \Symfony\Component\VarExporter\LazyProxyTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = []; + } + EOPHP]; + } + public function testAttributes() { $expected = <<<'EOPHP' @@ -182,6 +226,7 @@ public function foo(#[\SensitiveParameter, AnotherAttribute] $a): int { } }); + $this->assertStringContainsString($expected, ProxyHelper::generateLazyProxy($class)); }