Skip to content

Commit a827763

Browse files
committed
1 parent a6e850a commit a827763

File tree

3 files changed

+287
-287
lines changed

3 files changed

+287
-287
lines changed

ProxyHelper.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,20 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf
221221
if ('get' === $hook) {
222222
$ref = ($method->returnsReference() ? '&' : '');
223223
$hooks .= <<<EOPHP
224-
{$ref}get {
225-
return \$this->lazyObjectState->realInstance->{$p->name};
226-
}
224+
{$ref}get {
225+
return \$this->lazyObjectState->realInstance->{$p->name};
226+
}
227227
228-
EOPHP;
228+
EOPHP;
229229
} elseif ('set' === $hook) {
230230
$parameters = self::exportParameters($method, true);
231231
$arg = '$'.$method->getParameters()[0]->name;
232232
$hooks .= <<<EOPHP
233-
set({$parameters}) {
234-
\$this->lazyObjectState->realInstance->{$p->name} = {$arg};
235-
}
233+
set({$parameters}) {
234+
\$this->lazyObjectState->realInstance->{$p->name} = {$arg};
235+
}
236236
237-
EOPHP;
237+
EOPHP;
238238
} else {
239239
throw new LogicException(\sprintf('Cannot generate lazy proxy: hook "%s::%s()" is not supported.', $class->name, $method->name));
240240
}
@@ -277,8 +277,8 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf
277277
$body = " throw new \BadMethodCallException('Cannot forward abstract method \"{$method->class}::{$method->name}()\".');";
278278
} elseif (str_ends_with($signature, '): never') || str_ends_with($signature, '): void')) {
279279
$body = <<<EOPHP
280-
\$this->lazyObjectState->realInstance->{$method->name}({$args});
281-
EOPHP;
280+
\$this->lazyObjectState->realInstance->{$method->name}({$args});
281+
EOPHP;
282282
} else {
283283
$mayReturnThis = false;
284284
foreach (preg_split('/[()|&]++/', self::exportType($method) ?? 'static') as $type) {
@@ -296,19 +296,19 @@ public static function generateLazyProxy(?\ReflectionClass $class, array $interf
296296

297297
if ($method->returnsReference() || !$mayReturnThis) {
298298
$body = <<<EOPHP
299-
return \$this->lazyObjectState->realInstance->{$method->name}({$args});
300-
EOPHP;
299+
return \$this->lazyObjectState->realInstance->{$method->name}({$args});
300+
EOPHP;
301301
} else {
302302
$body = <<<EOPHP
303-
\${0} = \$this->lazyObjectState->realInstance;
304-
\${1} = \${0}->{$method->name}({$args});
305-
306-
return match (true) {
307-
\${1} === \${0} => \$this,
308-
!\${1} instanceof \${0} || !\${0} instanceof \${1} => \${1},
309-
null !== \$this->lazyObjectState->cloneInstance =& \${1} => clone \$this,
310-
};
311-
EOPHP;
303+
\${0} = \$this->lazyObjectState->realInstance;
304+
\${1} = \${0}->{$method->name}({$args});
305+
306+
return match (true) {
307+
\${1} === \${0} => \$this,
308+
!\${1} instanceof \${0} || !\${0} instanceof \${1} => \${1},
309+
null !== \$this->lazyObjectState->cloneInstance =& \${1} => clone \$this,
310+
};
311+
EOPHP;
312312
}
313313
}
314314
$methods[$lcName] = " {$signature}\n {\n{$body}\n }";
@@ -351,14 +351,14 @@ public function __unserialize(\$data): void
351351
if ($lazyProxyTraitStatement) {
352352
$lazyProxyTraitStatement = implode("\n ", $lazyProxyTraitStatement);
353353
$lazyProxyTraitStatement = <<<EOPHP
354-
use \Symfony\Component\VarExporter\Internal\LazyDecoratorTrait {
355-
{$lazyProxyTraitStatement}
356-
}
357-
EOPHP;
354+
use \Symfony\Component\VarExporter\Internal\LazyDecoratorTrait {
355+
{$lazyProxyTraitStatement}
356+
}
357+
EOPHP;
358358
} else {
359359
$lazyProxyTraitStatement = <<<EOPHP
360-
use \Symfony\Component\VarExporter\Internal\LazyDecoratorTrait;
361-
EOPHP;
360+
use \Symfony\Component\VarExporter\Internal\LazyDecoratorTrait;
361+
EOPHP;
362362
}
363363

364364
return <<<EOPHP
@@ -437,12 +437,12 @@ private static function generateLegacyLazyProxy(?\ReflectionClass $class, array
437437
$body = " $parentCall;";
438438
} elseif (str_ends_with($signature, '): never') || str_ends_with($signature, '): void')) {
439439
$body = <<<EOPHP
440-
if (isset(\$this->lazyObjectState)) {
441-
(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
442-
} else {
443-
{$parentCall};
444-
}
445-
EOPHP;
440+
if (isset(\$this->lazyObjectState)) {
441+
(\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
442+
} else {
443+
{$parentCall};
444+
}
445+
EOPHP;
446446
} else {
447447
if (!$methodsHaveToBeProxied && !$method->isAbstract()) {
448448
// Skip proxying methods that might return $this
@@ -459,12 +459,12 @@ private static function generateLegacyLazyProxy(?\ReflectionClass $class, array
459459
}
460460

461461
$body = <<<EOPHP
462-
if (isset(\$this->lazyObjectState)) {
463-
return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
464-
}
462+
if (isset(\$this->lazyObjectState)) {
463+
return (\$this->lazyObjectState->realInstance ??= (\$this->lazyObjectState->initializer)())->{$method->name}({$args});
464+
}
465465
466-
return {$parentCall};
467-
EOPHP;
466+
return {$parentCall};
467+
EOPHP;
468468
}
469469
$methods[$lcName] = " {$signature}\n {\n{$body}\n }";
470470
}
@@ -491,10 +491,10 @@ private static function generateLegacyLazyProxy(?\ReflectionClass $class, array
491491
) {
492492
// fix contravariance type problem when $class declares a `__unserialize()` method without typehint.
493493
$lazyProxyTraitStatement = <<<EOPHP
494-
use \Symfony\Component\VarExporter\LazyProxyTrait {
495-
__unserialize as private __doUnserialize;
496-
}
497-
EOPHP;
494+
use \Symfony\Component\VarExporter\LazyProxyTrait {
495+
__unserialize as private __doUnserialize;
496+
}
497+
EOPHP;
498498

499499
$body .= <<<EOPHP
500500
@@ -506,8 +506,8 @@ public function __unserialize(\$data): void
506506
EOPHP;
507507
} else {
508508
$lazyProxyTraitStatement = <<<EOPHP
509-
use \Symfony\Component\VarExporter\LazyProxyTrait;
510-
EOPHP;
509+
use \Symfony\Component\VarExporter\LazyProxyTrait;
510+
EOPHP;
511511
}
512512

513513
return <<<EOPHP

Tests/LegacyProxyHelperTest.php

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,98 +27,98 @@ class LegacyProxyHelperTest extends ProxyHelperTest
2727
public function testGenerateLazyProxy()
2828
{
2929
$expected = <<<'EOPHP'
30-
extends \Symfony\Component\VarExporter\Tests\TestForProxyHelper implements \Symfony\Component\VarExporter\LazyObjectInterface
31-
{
32-
use \Symfony\Component\VarExporter\LazyProxyTrait;
33-
34-
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
35-
36-
public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
30+
extends \Symfony\Component\VarExporter\Tests\TestForProxyHelper implements \Symfony\Component\VarExporter\LazyObjectInterface
3731
{
38-
if (isset($this->lazyObjectState)) {
39-
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo1(...\func_get_args());
40-
}
32+
use \Symfony\Component\VarExporter\LazyProxyTrait;
4133
42-
return parent::foo1(...\func_get_args());
43-
}
34+
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
4435
45-
public function foo4(\Symfony\Component\VarExporter\Tests\Bar|string $b, &$d): void
46-
{
47-
if (isset($this->lazyObjectState)) {
48-
($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo4($b, $d, ...\array_slice(\func_get_args(), 2));
49-
} else {
50-
parent::foo4($b, $d, ...\array_slice(\func_get_args(), 2));
36+
public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
37+
{
38+
if (isset($this->lazyObjectState)) {
39+
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo1(...\func_get_args());
40+
}
41+
42+
return parent::foo1(...\func_get_args());
5143
}
52-
}
5344
54-
protected function foo7()
55-
{
56-
if (isset($this->lazyObjectState)) {
57-
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo7(...\func_get_args());
45+
public function foo4(\Symfony\Component\VarExporter\Tests\Bar|string $b, &$d): void
46+
{
47+
if (isset($this->lazyObjectState)) {
48+
($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo4($b, $d, ...\array_slice(\func_get_args(), 2));
49+
} else {
50+
parent::foo4($b, $d, ...\array_slice(\func_get_args(), 2));
51+
}
5852
}
5953
60-
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelper::foo7()".');
54+
protected function foo7()
55+
{
56+
if (isset($this->lazyObjectState)) {
57+
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo7(...\func_get_args());
58+
}
59+
60+
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelper::foo7()".');
61+
}
6162
}
62-
}
6363
64-
// Help opcache.preload discover always-needed symbols
65-
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
66-
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
67-
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
64+
// Help opcache.preload discover always-needed symbols
65+
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
66+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
67+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
6868

69-
EOPHP;
69+
EOPHP;
7070

7171
$this->assertSame($expected, ProxyHelper::generateLazyProxy(new \ReflectionClass(TestForProxyHelper::class)));
7272
}
7373

7474
public function testGenerateLazyProxyForInterfaces()
7575
{
7676
$expected = <<<'EOPHP'
77-
implements \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1, \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2, \Symfony\Component\VarExporter\LazyObjectInterface
78-
{
79-
use \Symfony\Component\VarExporter\LazyProxyTrait;
77+
implements \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1, \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2, \Symfony\Component\VarExporter\LazyObjectInterface
78+
{
79+
use \Symfony\Component\VarExporter\LazyProxyTrait;
8080
81-
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
81+
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
8282
83-
public function initializeLazyObject(): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1&\Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
84-
{
85-
if ($state = $this->lazyObjectState ?? null) {
86-
return $state->realInstance ??= ($state->initializer)();
83+
public function initializeLazyObject(): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1&\Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
84+
{
85+
if ($state = $this->lazyObjectState ?? null) {
86+
return $state->realInstance ??= ($state->initializer)();
87+
}
88+
89+
return $this;
8790
}
8891
89-
return $this;
90-
}
92+
public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
93+
{
94+
if (isset($this->lazyObjectState)) {
95+
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo1(...\func_get_args());
96+
}
9197
92-
public function foo1(): ?\Symfony\Component\VarExporter\Tests\Bar
93-
{
94-
if (isset($this->lazyObjectState)) {
95-
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo1(...\func_get_args());
98+
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1::foo1()".');
9699
}
97100
98-
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface1::foo1()".');
99-
}
101+
public function foo2(?\Symfony\Component\VarExporter\Tests\Bar $b, ...$d): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
102+
{
103+
if (isset($this->lazyObjectState)) {
104+
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo2(...\func_get_args());
105+
}
100106
101-
public function foo2(?\Symfony\Component\VarExporter\Tests\Bar $b, ...$d): \Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2
102-
{
103-
if (isset($this->lazyObjectState)) {
104-
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo2(...\func_get_args());
107+
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2::foo2()".');
105108
}
106109
107-
return throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2::foo2()".');
108-
}
109-
110-
public static function foo3(): string
111-
{
112-
throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2::foo3()".');
110+
public static function foo3(): string
111+
{
112+
throw new \BadMethodCallException('Cannot forward abstract method "Symfony\Component\VarExporter\Tests\TestForProxyHelperInterface2::foo3()".');
113+
}
113114
}
114-
}
115115
116-
// Help opcache.preload discover always-needed symbols
117-
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
118-
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
119-
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
116+
// Help opcache.preload discover always-needed symbols
117+
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
118+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
119+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
120120

121-
EOPHP;
121+
EOPHP;
122122

123123
$this->assertSame($expected, ProxyHelper::generateLazyProxy(null, [new \ReflectionClass(TestForProxyHelperInterface1::class), new \ReflectionClass(TestForProxyHelperInterface2::class)]));
124124
}
@@ -130,50 +130,50 @@ public function __unserialize($array): void
130130
{
131131
}
132132
}, <<<'EOPHP'
133-
implements \Symfony\Component\VarExporter\LazyObjectInterface
134-
{
135-
use \Symfony\Component\VarExporter\LazyProxyTrait {
136-
__unserialize as private __doUnserialize;
137-
}
133+
implements \Symfony\Component\VarExporter\LazyObjectInterface
134+
{
135+
use \Symfony\Component\VarExporter\LazyProxyTrait {
136+
__unserialize as private __doUnserialize;
137+
}
138138
139-
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
139+
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
140140
141-
public function __unserialize($data): void
142-
{
143-
$this->__doUnserialize($data);
141+
public function __unserialize($data): void
142+
{
143+
$this->__doUnserialize($data);
144+
}
144145
}
145-
}
146-
EOPHP];
146+
EOPHP];
147147
148148
yield 'type hinted __unserialize method' => [new class {
149149
public function __unserialize(array $array): void
150150
{
151151
}
152152
}, <<<'EOPHP'
153-
implements \Symfony\Component\VarExporter\LazyObjectInterface
154-
{
155-
use \Symfony\Component\VarExporter\LazyProxyTrait;
153+
implements \Symfony\Component\VarExporter\LazyObjectInterface
154+
{
155+
use \Symfony\Component\VarExporter\LazyProxyTrait;
156156
157-
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
158-
}
159-
EOPHP];
157+
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
158+
}
159+
EOPHP];
160160
}
161161
162162
public function testAttributes()
163163
{
164164
$expected = <<<'EOPHP'
165165
166-
public function foo(#[\SensitiveParameter] $a): int
167-
{
168-
if (isset($this->lazyObjectState)) {
169-
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo(...\func_get_args());
170-
}
166+
public function foo(#[\SensitiveParameter] $a): int
167+
{
168+
if (isset($this->lazyObjectState)) {
169+
return ($this->lazyObjectState->realInstance ??= ($this->lazyObjectState->initializer)())->foo(...\func_get_args());
170+
}
171171
172-
return parent::foo(...\func_get_args());
172+
return parent::foo(...\func_get_args());
173+
}
173174
}
174-
}
175175

176-
EOPHP;
176+
EOPHP;
177177

178178
$class = new \ReflectionClass(new class {
179179
#[SomeAttribute]

0 commit comments

Comments
 (0)