From 4badc59ffae070c80ce676c007c6fb9efffd6456 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 27 Apr 2025 15:26:02 +0200 Subject: [PATCH 1/3] Remove unneeded use statements --- Tests/LazyProxyTraitTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/LazyProxyTraitTest.php b/Tests/LazyProxyTraitTest.php index 61be742..8a94b71 100644 --- a/Tests/LazyProxyTraitTest.php +++ b/Tests/LazyProxyTraitTest.php @@ -20,8 +20,8 @@ use Symfony\Component\VarExporter\ProxyHelper; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AbstractHooked; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility; -use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass; +use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestClass; From f2b47e60f70cbcc58ee7ef720ab436479dd0cda0 Mon Sep 17 00:00:00 2001 From: Hakayashii Date: Wed, 23 Apr 2025 20:48:59 +0200 Subject: [PATCH 2/3] [VarExporter] Fix: Use correct closure call for property-specific logic in $notByRef --- Internal/Hydrator.php | 2 +- .../LazyProxy/HookedWithDefaultValue.php | 11 +++++++++ Tests/LazyGhostTraitTest.php | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php diff --git a/Internal/Hydrator.php b/Internal/Hydrator.php index d8250d4..158f6ca 100644 --- a/Internal/Hydrator.php +++ b/Internal/Hydrator.php @@ -166,7 +166,7 @@ public static function getSimpleHydrator($class) $object->$name = $value; $object->$name = &$value; } elseif (true !== $noRef) { - $notByRef($object, $value); + $noRef($object, $value); } else { $object->$name = $value; } diff --git a/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php b/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php new file mode 100644 index 0000000..1281109 --- /dev/null +++ b/Tests/Fixtures/LazyProxy/HookedWithDefaultValue.php @@ -0,0 +1,11 @@ + $this->backedWithDefault; + set => $this->backedWithDefault = $value; + } +} diff --git a/Tests/LazyGhostTraitTest.php b/Tests/LazyGhostTraitTest.php index 5b80f6b..3f7513c 100644 --- a/Tests/LazyGhostTraitTest.php +++ b/Tests/LazyGhostTraitTest.php @@ -27,6 +27,7 @@ use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility; use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked; +use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\HookedWithDefaultValue; use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject; class LazyGhostTraitTest extends TestCase @@ -505,6 +506,28 @@ public function testPropertyHooks() $this->assertSame(345, $object->backed); } + /** + * @requires PHP 8.4 + */ + public function testPropertyHooksWithDefaultValue() + { + $initialized = false; + $object = $this->createLazyGhost(HookedWithDefaultValue::class, function ($instance) use (&$initialized) { + $initialized = true; + }); + + $this->assertSame(321, $object->backedWithDefault); + $this->assertTrue($initialized); + + $initialized = false; + $object = $this->createLazyGhost(HookedWithDefaultValue::class, function ($instance) use (&$initialized) { + $initialized = true; + }); + $object->backedWithDefault = 654; + $this->assertTrue($initialized); + $this->assertSame(654, $object->backedWithDefault); + } + /** * @requires PHP 8.4 */ From 717e7544aa99752c54ecba5c0e17459c48317472 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 27 Apr 2025 23:06:26 +0200 Subject: [PATCH 3/3] dump default value for property hooks if present --- ProxyHelper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ProxyHelper.php b/ProxyHelper.php index 538d23f..e3a38b1 100644 --- a/ProxyHelper.php +++ b/ProxyHelper.php @@ -79,7 +79,9 @@ public static function generateLazyGhost(\ReflectionClass $class): string $hooks .= "\n " .($p->isProtected() ? 'protected' : 'public') .($p->isProtectedSet() ? ' protected(set)' : '') - ." {$type} \${$name} {\n"; + ." {$type} \${$name}" + .($p->hasDefaultValue() ? ' = '.$p->getDefaultValue() : '') + ." {\n"; foreach ($p->getHooks() as $hook => $method) { if ('get' === $hook) {