From 5c016619a02c5840dad8ae64e6f283925cbd9134 Mon Sep 17 00:00:00 2001 From: Andjelka Stosic Date: Tue, 6 Feb 2024 12:43:08 +0100 Subject: [PATCH 1/2] Return null in case when property is not initialized in property accessor --- src/Accessor/PropertyAccessor.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Accessor/PropertyAccessor.php b/src/Accessor/PropertyAccessor.php index c5bd6bc..3fe0b17 100644 --- a/src/Accessor/PropertyAccessor.php +++ b/src/Accessor/PropertyAccessor.php @@ -41,6 +41,11 @@ public function getValue(object $object) throw DeserializerLogicException::createMissingProperty($class, $this->property); } + $reflection = new \ReflectionProperty($class, $this->property); + if (!$reflection->isInitialized($object)) { + return null; + } + $getter = \Closure::bind( fn ($property) => $this->{$property}, $object, From 498a5c3d480dcc8be01908a4f928cda8fa021899 Mon Sep 17 00:00:00 2001 From: Andjelka Stosic Date: Fri, 9 Feb 2024 14:23:41 +0100 Subject: [PATCH 2/2] Add unit test --- tests/Unit/Accessor/PropertyAccessorTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Unit/Accessor/PropertyAccessorTest.php b/tests/Unit/Accessor/PropertyAccessorTest.php index c7b7bd7..5901240 100644 --- a/tests/Unit/Accessor/PropertyAccessorTest.php +++ b/tests/Unit/Accessor/PropertyAccessorTest.php @@ -89,6 +89,22 @@ public function setName(string $name): void self::assertSame('Name', $accessor->getValue($object)); } + public function testGetValueHandleUninitializedProperty(): void + { + $object = new class() { + private string $name; + + public function getName(): string + { + return $this->name; + } + }; + + $accessor = new PropertyAccessor('name'); + + self::assertNull($accessor->getValue($object)); + } + public function testGetValueCanAccessPrivatePropertyThroughDoctrineProxyClass(): void { $object = new class() extends AbstractManyModel implements Proxy {