8000 [VarExporter] Initialize ghosts before calling their `__clone` method… · symfony/symfony@b99f459 · GitHub
[go: up one dir, main page]

Skip to content

Commit b99f459

Browse files
committed
[VarExporter] Initialize ghosts before calling their __clone method if they have skipped properties
1 parent e8882a3 commit b99f459

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/Symfony/Component/VarExporter/LazyGhostTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ public function __clone(): void
332332
}
333333

334334
if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['clone']) {
335+
if ($this->lazyObjectState->skippedProperties) {
336+
$this->initializeLazyObject();
337+
}
338+
335339
parent::__clone();
336340
}
337341
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost;
4+
5+
class MagicCloneClass
6+
{
7+
public ?int $id;
8+
public bool $cloned;
9+
10+
public function __clone()
11+
{
12+
$this->id = null;
13+
$this->cloned = true;
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost;
4+
5+
use Symfony\Component\VarExporter\LazyGhostTrait;
6+
use Symfony\Component\VarExporter\LazyObjectInterface;
7+
8+
class MagicCloneClassProxy extends MagicCloneClass implements LazyObjectInterface
9+
{
10+
use LazyGhostTrait {
11+
__clone as private __doClone;
12+
}
13+
14+
public function __clone()
15+
{
16+
$this->__doClone();
17+
}
18+
}

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildTestClass;
2020
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\LazyClass;
2121
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
22+
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicCloneClassProxy;
2223
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
2324
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass;
2425

@@ -109,6 +110,23 @@ public function testClone()
109110
$this->assertTrue($clone->resetLazyObject());
110111
}
111112

113+
public function testCloneIsInitializedIfNeeded()
114+
{
115+
$instance = MagicCloneClassProxy::createLazyGhost(function (MagicCloneClassProxy $ghost) {
116+
if (1 === $ghost->id) {
117+
$ghost->cloned = false;
118+
} else {
119+
$this->fail('Ghost must be initialized before its __clone method is called.');
120+
}
121+
}, ['id' => true]);
122+
$instance->id = 1;
123+
124+
$clone = clone $instance;
125+
126+
$this->assertNull($clone->id);
127+
$this->assertTrue($clone->cloned);
128+
}
129+
112130
public function testSerialize()
113131
{
114132
$instance = ChildTestClass::createLazyGhost(function (ChildTestClass $ghost) {

0 commit comments

Comments
 (0)
0