Skip to content

[VarExporter] fix contravariance problem with __unserialize() in lazy proxy #57460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Symfony/Component/VarExporter/ProxyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOPHP
use \Symfony\Component\VarExporter\LazyProxyTrait {
__unserialize as private __doUnserialize;
}
EOPHP;

$body .= <<<EOPHP

public function __unserialize(\$data): void
{
\$this->__doUnserialize(\$data);
}

EOPHP;
} else {
$lazyProxyTraitStatement = <<<EOPHP
use \Symfony\Component\VarExporter\LazyProxyTrait;
EOPHP;
}

return <<<EOPHP
{$parent} implements \\{$interfaces}
{
use \Symfony\Component\VarExporter\LazyProxyTrait;
{$lazyProxyTraitStatement}

private const LAZY_OBJECT_PROPERTY_SCOPES = {$propertyScopes};
{$body}}
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,50 @@ class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
$this->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'
Expand All @@ -182,6 +226,7 @@ public function foo(#[\SensitiveParameter, AnotherAttribute] $a): int
{
}
});

$this->assertStringContainsString($expected, ProxyHelper::generateLazyProxy($class));
}

Expand Down
Loading