Skip to content

Commit

Permalink
Merge pull request #47 from andjelkastosic/handle-uninitialized-prope…
Browse files Browse the repository at this point in the history
…rties

Return null in case when property is not initialized in property acce…
  • Loading branch information
dominikzogg committed Feb 11, 2024
2 parents 9d2fd24 + 498a5c3 commit 7012cbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Accessor/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Accessor/PropertyAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7012cbf

Please sign in to comment.