Skip to content

Commit

Permalink
[PropertyAccess] Fix accessing public property in Object
Browse files Browse the repository at this point in the history
  • Loading branch information
kevcomparadise authored and nicolas-grekas committed Dec 11, 2021
1 parent 87c9c4e commit 8d01a4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
throw $e;
}
} elseif (PropertyReadInfo::TYPE_PROPERTY === $type) {
if ($access[self::ACCESS_REF] && !isset($object->$name) && !\array_key_exists($name, (array) $object) && (\PHP_VERSION_ID < 70400 || !(new \ReflectionProperty($class, $name))->hasType())) {
if ($access->canBeReference() && !isset($object->$name) && !\array_key_exists($name, (array) $object) && (\PHP_VERSION_ID < 70400 || !(new \ReflectionProperty($class, $name))->hasType())) {
throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not initialized.', $class, $name));
}

Expand All @@ -491,7 +491,7 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid

throw $e;
}
} elseif ($object instanceof \stdClass && property_exists($object, $property)) {
} elseif (property_exists($object, $property) && \array_key_exists($property, (array) $object)) {
$result[self::VALUE] = $object->$property;
if (isset($zval[self::REF])) {
$result[self::REF] = &$object->$property;
Expand Down
11 changes: 10 additions & 1 deletion Tests/PropertyAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ public function testGetPrivateProperty()
$object = new TestPublicPropertyGetterOnObject();

$this->expectException(NoSuchPropertyException::class);
$this->expectExceptionMessageMatches('/.*Neither the property "b" nor one of the methods/');
$this->expectExceptionMessageMatches('/.*Can\'t get a way to read the property "b" in class "Symfony\\\Component\\\PropertyAccess\\\Tests\\\Fixtures\\\TestPublicPropertyGetterOnObject./');
$this->propertyAccessor->getValue($object, 'b');
}

Expand All @@ -977,6 +977,15 @@ public function testGetDynamicPublicProperty()
$this->assertSame($value, $this->propertyAccessor->getValue($object, $path));
}

public function testGetDynamicPublicPropertyWithMagicGetterDisallow()
{
$object = new TestPublicPropertyGetterOnObjectMagicGet();
$propertyAccessor = new PropertyAccessor(PropertyAccessor::DISALLOW_MAGIC_METHODS);

$this->expectException(NoSuchPropertyException::class);
$propertyAccessor->getValue($object, 'c');
}

public function testGetDynamicPublicPropertyWithMagicGetterAllow()
{
$value = 'B';
Expand Down

0 comments on commit 8d01a4f

Please sign in to comment.