Skip to content

[VarExporter] Fix forwarding references to proxied classes #50086

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 1 commit into from
Apr 21, 2023
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
14 changes: 2 additions & 12 deletions src/Symfony/Component/Cache/Tests/Traits/RedisProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ public function testRedis5Proxy($class)
if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name)) {
continue;
}
$args = [];
foreach ($method->getParameters() as $param) {
$args[] = ($param->isVariadic() ? '...' : '').'$'.$param->name;
}
$args = implode(', ', $args);
$return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
$methods[] = "\n ".ProxyHelper::exportSignature($method, false)."\n".<<<EOPHP
$methods[] = "\n ".ProxyHelper::exportSignature($method, false, $args)."\n".<<<EOPHP
{
{$return}(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
}
Expand Down Expand Up @@ -76,13 +71,8 @@ public function testRedis6Proxy($class, $stub)
if ('reset' === $method->name || method_exists(LazyProxyTrait::class, $method->name)) {
continue;
}
$args = [];
foreach ($method->getParameters() as $param) {
$args[] = ($param->isVariadic() ? '...' : '').'$'.$param->name;
}
$args = implode(', ', $args);
$return = $method->getReturnType() instanceof \ReflectionNamedType && 'void' === (string) $method->getReturnType() ? '' : 'return ';
$methods[] = "\n ".ProxyHelper::exportSignature($method, false)."\n".<<<EOPHP
$methods[] = "\n ".ProxyHelper::exportSignature($method, false, $args)."\n".<<<EOPHP
{
{$return}(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
}
Expand Down
478 changes: 239 additions & 239 deletions src/Symfony/Component/Cache/Traits/Redis5Proxy.php

Large diffs are not rendered by default.

506 changes: 253 additions & 253 deletions src/Symfony/Component/Cache/Traits/Redis6Proxy.php

Large diffs are not rendered by default.

380 changes: 190 additions & 190 deletions src/Symfony/Component/Cache/Traits/RedisCluster5Proxy.php

Large diffs are not rendered by default.

446 changes: 223 additions & 223 deletions src/Symfony/Component/Cache/Traits/RedisCluster6Proxy.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"psr/log": "^1.1|^2|^3",
"symfony/cache-contracts": "^1.1.7|^2|^3",
"symfony/service-contracts": "^1.1|^2|^3",
"symfony/var-exporter": "^6.2.7"
"symfony/var-exporter": "^6.2.10"
},
"require-dev": {
"cache/integration-tests": "dev-master",
Expand Down
23 changes: 18 additions & 5 deletions src/Symfony/Component/VarExporter/ProxyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf
continue;
}

$signature = self::exportSignature($method);
$parentCall = $method->isAbstract() ? "throw new \BadMethodCallException('Cannot forward abstract method \"{$method->class}::{$method->name}()\".')" : "parent::{$method->name}(...\\func_get_args())";
$signature = self::exportSignature($method, true, $args);
$parentCall = $method->isAbstract() ? "throw new \BadMethodCallException('Cannot forward abstract method \"{$method->class}::{$method->name}()\".')" : "parent::{$method->name}({$args})";

if ($method->isStatic()) {
$body = " $parentCall;";
} elseif (str_ends_with($signature, '): never') || str_ends_with($signature, '): void')) {
$body = <<<EOPHP
if (isset(\$this->lazyObjectState)) {
(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}(...\\func_get_args());
(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
} else {
{$parentCall};
}
Expand All @@ -172,7 +172,7 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf

$body = <<<EOPHP
if (isset(\$this->lazyObjectState)) {
return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}(...\\func_get_args());
return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
}

return {$parentCall};
Expand Down Expand Up @@ -213,15 +213,28 @@ class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
EOPHP;
}

public static function exportSignature(\ReflectionFunctionAbstract $function, bool $withParameterTypes = true): string
public static function exportSignature(\ReflectionFunctionAbstract $function, bool $withParameterTypes = true, string &$args = null): string
{
$hasByRef = false;
$args = '';
$param = null;
$parameters = [];
foreach ($function->getParameters() as $param) {
$parameters[] = ($param->getAttributes(\SensitiveParameter::class) ? '#[\SensitiveParameter] ' : '')
.($withParameterTypes && $param->hasType() ? self::exportType($param).' ' : '')
.($param->isPassedByReference() ? '&' : '')
.($param->isVariadic() ? '...' : '').'$'.$param->name
.($param->isOptional() && !$param->isVariadic() ? ' = '.self::exportDefault($param) : '');
$hasByRef = $hasByRef || $param->isPassedByReference();
$args .= ($param->isVariadic() ? '...$' : '$').$param->name.', ';
}

if (!$param || !$hasByRef) {
$args = '...\func_get_args()';
} elseif ($param->isVariadic()) {
$args = substr($args, 0, -2);
} else {
$args .= sprintf('...\array_slice(\func_get_args(), %d)', \count($parameters));
}

$signature = 'function '.($function->returnsReference() ? '&' : '')
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/VarExporter/Tests/ProxyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
return parent::foo1(...\func_get_args());
}

public function foo4(\Symfony\Component\VarExporter\Tests\Bar|string $b): void
public function foo4(\Symfony\Component\VarExporter\Tests\Bar|string $b, &$d): void
{
if (isset($this->lazyObjectState)) {
($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo4(...\func_get_args());
($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo4($b, $d, ...\array_slice(\func_get_args(), 2));
} else {
parent::foo4(...\func_get_args());
parent::foo4($b, $d, ...\array_slice(\func_get_args(), 2));
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1::foo1()".');
}

public function foo2(?\Symfony\Component\VarExporter\Tests\Bar $b): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
public function foo2(?\Symfony\Component\VarExporter\Tests\Bar $b, ...$d): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
{
if (isset($this->lazyObjectState)) {
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo2(...\func_get_args());
Expand Down Expand Up @@ -196,15 +196,15 @@ public function foo1(): ?Bar
{
}

public function foo2(?Bar $b): ?self
public function foo2(?Bar $b, ...$d): ?self
{
}

public function &foo3(Bar &$b, string &...$c)
{
}

public function foo4(Bar|string $b): void
public function foo4(Bar|string $b, &$d): void
{
}

Expand Down Expand Up @@ -234,7 +234,7 @@ public function foo1(): ?Bar;

interface TestForProxyHelperInterface2
{
public function foo2(?Bar $b): self;
public function foo2(?Bar $b, ...$d): self;

public static function foo3(): string;
}
Expand Down