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, 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 {