8000 Fix PropertyAccessor modifying array in object when array key does no… · symfony/symfony@255747d · GitHub
[go: up one dir, main page]

Skip to content

Commit 255747d

Browse files
committed
Fix PropertyAccessor modifying array in object when array key does not exist
1 parent 513fc31 commit 255747d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
113113
// Create missing nested arrays on demand
114114
if (
115115
$isIndex &&
116+
$i + 1 < $propertyPath->getLength() &&
116117
(
117118
($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property])) ||
118119
(is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))
119120
)
120121
) {
121-
$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
122+
$objectOrArray[$property] = array();
122123
}
123124

124125
if ($isIndex) {

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public function testGetValueReadsProperty()
103103
$this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, 'firstName'));
104104
}
105105

106+
public function testGetValueNotModifyObject()
107+
{
108+
$object = new Author();
109+
$object->firstName = array('Bernhard');
110+
111+
$this->assertNull($this->propertyAccessor->getValue($object, 'firstName[1]'));
112+
$this->assertSame(array('Bernhard'), $object->firstName);
113+
}
114+
106115
public function testGetValueIgnoresSingular()
107116
{
108117
$this->markTestSkipped('This feature is temporarily disabled as of 2.1');

0 commit comments

Comments
 (0)
0