From 0c71a93e0bfc29e2dd47c785843ffa7a5406cc0c Mon Sep 17 00:00:00 2001 From: Vladimir Pakhomchik Date: Wed, 14 May 2025 12:41:00 +0200 Subject: [PATCH] Fixed lazy-loading ghost objects generation with property hooks with default values. --- .../Component/VarExporter/ProxyHelper.php | 2 +- .../LazyProxy/HookedWithDefaultValue.php | 16 +++++++++++++--- .../VarExporter/Tests/LazyGhostTraitTest.php | 12 +++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/VarExporter/ProxyHelper.php b/src/Symfony/Component/VarExporter/ProxyHelper.php index e3a38b14a139b..862e0332a0ff8 100644 --- a/src/Symfony/Component/VarExporter/ProxyHelper.php +++ b/src/Symfony/Component/VarExporter/ProxyHelper.php @@ -80,7 +80,7 @@ public static function generateLazyGhost(\ReflectionClass $class): string .($p->isProtected() ? 'protected' : 'public') .($p->isProtectedSet() ? ' protected(set)' : '') ." {$type} \${$name}" - .($p->hasDefaultValue() ? ' = '.$p->getDefaultValue() : '') + .($p->hasDefaultValue() ? ' = '.VarExporter::export($p->getDefaultValue()) : '') ." {\n"; foreach ($p->getHooks() as $hook => $method) { diff --git a/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php index 1281109e7228d..7d49049ac449a 100644 --- a/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php +++ b/src/Symfony/Component/VarExporter/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php @@ -4,8 +4,18 @@ class HookedWithDefaultValue { - public int $backedWithDefault = 321 { - get => $this->backedWithDefault; - set => $this->backedWithDefault = $value; + public int $backedIntWithDefault = 321 { + get => $this->backedIntWithDefault; + set => $this->backedIntWithDefault = $value; + } + + public string $backedStringWithDefault = '321' { + get => $this->backedStringWithDefault; + set => $this->backedStringWithDefault = $value; + } + + public bool $backedBoolWithDefault = false { + get => $this->backedBoolWithDefault; + set => $this->backedBoolWithDefault = $value; } } diff --git a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php index 3f7513c270b5f..a80c007b53c87 100644 --- a/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php +++ b/src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php @@ -516,16 +516,22 @@ public function testPropertyHooksWithDefaultValue() $initialized = true; }); - $this->assertSame(321, $object->backedWithDefault); + $this->assertSame(321, $object->backedIntWithDefault); + $this->assertSame('321', $object->backedStringWithDefault); + $this->assertSame(false, $object->backedBoolWithDefault); $this->assertTrue($initialized); $initialized = false; $object = $this->createLazyGhost(HookedWithDefaultValue::class, function ($instance) use (&$initialized) { $initialized = true; }); - $object->backedWithDefault = 654; + $object->backedIntWithDefault = 654; + $object->backedStringWithDefault = '654'; + $object->backedBoolWithDefault = true; $this->assertTrue($initialized); - $this->assertSame(654, $object->backedWithDefault); + $this->assertSame(654, $object->backedIntWithDefault); + $this->assertSame('654', $object->backedStringWithDefault); + $this->assertSame(true, $object->backedBoolWithDefault); } /**