Skip to content

Commit 07d3906

Browse files
committed
Fixed lazy-loading ghost objects generation with property hooks with default values.
1 parent 5955b14 commit 07d3906

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Symfony/Component/VarExporter/ProxyHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function generateLazyGhost(\ReflectionClass $class): string
8686
.($p->isProtected() ? 'protected' : 'public')
8787
.($p->isProtectedSet() ? ' protected(set)' : '')
8888
." {$type} \${$name}"
89-
.($p->hasDefaultValue() ? ' = '.$p->getDefaultValue() : '')
89+
.($p->hasDefaultValue() ? ' = '.VarExporter::export($p->getDefaultValue()) : '')
9090
." {\n";
9191

9292
foreach ($p->getHooks() as $hook => $method) {

src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@
44

55
class HookedWithDefaultValue
66
{
7-
public int $backedWithDefault = 321 {
8-
get => $this->backedWithDefault;
9-
set => $this->backedWithDefault = $value;
7+
public int $backedIntWithDefault = 321 {
8+
get => $this->backedIntWithDefault;
9+
set => $this->backedIntWithDefault = $value;
10+
}
11+
12+
public string $backedStringWithDefault = '321' {
13+
get => $this->backedStringWithDefault;
14+
set => $this->backedStringWithDefault = $value;
15+
}
16+
17+
public bool $backedBoolWithDefault = false {
18+
get => $this->backedBoolWithDefault;
19+
set => $this->backedBoolWithDefault = $value;
1020
}
1121
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,22 @@ public function testPropertyHooksWithDefaultValue()
332332
$initialized = true;
333333
});
334334

335-
$this->assertSame(321, $object->backedWithDefault);
335+
$this->assertSame(321, $object->backedIntWithDefault);
336+
$this->assertSame('321', $object->backedStringWithDefault);
337+
$this->assertSame(false, $object->backedBoolWithDefault);
336338
$this->assertTrue($initialized);
337339

338340
$initialized = false;
339341
$object = $this->createLazyGhost(HookedWithDefaultValue::class, function ($instance) use (&$initialized) {
340342
$initialized = true;
341343
});
342-
$object->backedWithDefault = 654;
344+
$object->backedIntWithDefault = 654;
345+
$object->backedStringWithDefault = '654';
346+
$object->backedBoolWithDefault = true;
343347
$this->assertTrue($initialized);
344-
$this->assertSame(654, $object->backedWithDefault);
348+
$this->assertSame(654, $object->backedIntWithDefault);
349+
$this->assertSame('654', $object->backedStringWithDefault);
350+
$this->assertSame(true, $object->backedBoolWithDefault);
345351
}
346352

347353
/**

0 commit comments

Comments
 (0)