8000 [PropertyAccess] Fix accessing dynamic properties by andreyserdjuk · Pull Request #37622 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Fix accessing dynamic properties #37622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[PropertyAccess] Fix accessing dynamic properties
  • Loading branch information
andreyserdjuk authored and fabpot committed Aug 26, 2020
commit 47bd0180d17cbaa3409de909020901820d89b047
14 changes: 10 additions & 4 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,16 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid

throw $e;
}
} elseif ($object instanceof \stdClass && property_exists($object, $property)) {
$result[self::VALUE] = $object->$property;
if (isset($zval[self::REF])) {
$result[self::REF] = &$object->$property;
} elseif (property_exists($object, $property)) {
try {
$result[self::VALUE] = $object->$property;
if (isset($zval[self::REF])) {
$result[self::REF] = &$object->$property;
}
} catch (\Error $e) {
if (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(sprintf('Can\'t read protected or private property "%s" in class "%s".', $property, $class), 0, $e);
}
}
} elseif (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class TestClassDynamicProperty
{
public function __construct($dynamicProperty)
{
$this->dynamicProperty = $dynamicProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidArgumentLength;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidMethods;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassDynamicProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassIsWritable;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;
Expand Down Expand Up @@ -97,6 +98,29 @@ public function testGetValue($objectOrArray, $path, $value)
$this->assertSame($value, $this->propertyAccessor->getValue($objectOrArray, $path));
}

/**
* Test get dynamic value from object is other than \stdClass instance.
*/
public function testGetDynamicValue()
{
$value = 'dynamicPropertyValue';
$path = 'dynamicProperty';
$object = new TestClassDynamicProperty($value);

$this->assertSame($value, $this->propertyAccessor->getValue($object, $path));
}

/**
* Ensure exact exception with message was thrown on access to non-public property.
*/
public function testGetInaccessibleProperty()
{
$this->expectException('Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException');
$this->expectExceptionMessage(sprintf('Can\'t read protected or private property "%s" in class "%s".', 'protectedProperty', TestClass::class));

$this->propertyAccessor->getValue(new TestClass('Bernhard'), 'protectedProperty');
}

/**
* @dataProvider getPathsWithMissingProperty
*/
Expand Down
0