From f24c678027ed3315ca02f2335105dc3257b981eb Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Fri, 2 Oct 2015 23:43:46 +0200 Subject: [PATCH] Fix PropertyAccessor modifying array in object when array key does not exist --- .../Component/PropertyAccess/PropertyAccessor.php | 4 +++- .../PropertyAccess/Tests/PropertyAccessorTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index f6c0c636d4..76ef8c1d5c 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -118,7 +118,9 @@ class PropertyAccessor implements PropertyAccessorInterface (is_array($objectOrArray) && !array_key_exists($property, $objectOrArray)) ) ) { - $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null; + if ($i + 1 < $propertyPath->getLength()) { + $objectOrArray[$property] = array(); + } } if ($isIndex) { diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php index fec4e14b09..87287048a7 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php @@ -103,6 +103,15 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, 'firstName')); } + public function testGetValueNotModifyObject() + { + $object = new Author(); + $object->firstName = array('Bernhard'); + + $this->assertNull($this->propertyAccessor->getValue($object, 'firstName[1]')); + $this->assertSame(array('Bernhard'), $object->firstName); + } + public function testGetValueIgnoresSingular() { $this->markTestSkipped('This feature is temporarily disabled as of 2.1');