From 6e1462e0c0a5d768e4037e8df3b1e1dca9fb5e36 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 9 Jul 2012 18:15:56 +0200 Subject: [PATCH 1/2] [Form] Fixed PropertyPath handling of __get() method that returns a constant --- .../Component/Form/Tests/Util/PropertyPathTest.php | 12 ++++++++++++ src/Symfony/Component/Form/Util/PropertyPath.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php index f231c24f45..b1830addd5 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php @@ -213,6 +213,18 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertSame('foobar', $path->getValue($object)); } + /* + * https://github.com/symfony/symfony/pull/4450 + */ + public function testGetValueReadsMagicGetThatReturnsConstant() + { + $path = new PropertyPath('magicProperty'); + + $object = new Magician(); + + $this->assertNull($path->getValue($object)); + } + /** * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException */ diff --git a/src/Symfony/Component/Form/Util/PropertyPath.php b/src/Symfony/Component/Form/Util/PropertyPath.php index f97a5e343e..81b2242bb9 100644 --- a/src/Symfony/Component/Form/Util/PropertyPath.php +++ b/src/Symfony/Component/Form/Util/PropertyPath.php @@ -402,7 +402,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface $result = $objectOrArray->$hasser(); } elseif ($reflClass->hasMethod('__get')) { // needed to support magic method __get - $result =& $objectOrArray->$property; + $result = $objectOrArray->$property; } elseif ($reflClass->hasProperty($property)) { if (!$reflClass->getProperty($property)->isPublic()) { throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "%s()" or "%s()"?', $property, $reflClass->name, $getter, $isser)); From 1345360e3f8f1fb6affb160d48616ebe483417ba Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 9 Jul 2012 18:21:55 +0200 Subject: [PATCH 2/2] [Form] Fixed PropertyPath handling of offsetGet() that returns a constant value --- .../Tests/Util/PropertyPathCollectionTest.php | 39 ++++++++++++++++++ .../Form/Tests/Util/PropertyPathTest.php | 40 ------------------- .../Component/Form/Util/PropertyPath.php | 8 +++- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php index 3798d25245..a52a42911a 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php @@ -96,6 +96,45 @@ abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase { abstract protected function getCollection(array $array); + public function testGetValueReadsArrayAccess() + { + $object = $this->getCollection(array('firstName' => 'Bernhard')); + + $path = new PropertyPath('[firstName]'); + + $this->assertEquals('Bernhard', $path->getValue($object)); + } + + /** + * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException + */ + public function testGetValueThrowsExceptionIfArrayAccessExpected() + { + $path = new PropertyPath('[firstName]'); + + $path->getValue(new \stdClass()); + } + + public function testSetValueUpdatesArrayAccess() + { + $object = $this->getCollection(array()); + + $path = new PropertyPath('[firstName]'); + $path->setValue($object, 'Bernhard'); + + $this->assertEquals('Bernhard', $object['firstName']); + } + + /** + * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException + */ + public function testSetValueThrowsExceptionIfArrayAccessExpected() + { + $path = new PropertyPath('[firstName]'); + + $path->setValue(new \stdClass(), 'Bernhard'); + } + public function testSetValueCallsAdderAndRemoverForCollections() { $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth', 4 => 'fifth')); diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php index b1830addd5..c2ffb4dfc5 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php @@ -123,26 +123,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Bernhard', $path->getValue($object)); } - public function testGetValueReadsArrayAccess() - { - $object = new \ArrayObject(); - $object['firstName'] = 'Bernhard'; - - $path = new PropertyPath('[firstName]'); - - $this->assertEquals('Bernhard', $path->getValue($object)); - } - - /** - * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException - */ - public function testGetValueThrowsExceptionIfArrayAccessExpected() - { - $path = new PropertyPath('[firstName]'); - - $path->getValue(new Author()); - } - /** * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException */ @@ -328,16 +308,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Bernhard', $object->child['index']->firstName); } - public function testSetValueUpdatesArrayAccess() - { - $object = new \ArrayObject(); - - $path = new PropertyPath('[firstName]'); - $path->setValue($object, 'Bernhard'); - - $this->assertEquals('Bernhard', $object['firstName']); - } - public function testSetValueUpdateMagicSet() { $object = new Magician(); @@ -348,16 +318,6 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foobar', $object->__get('magicProperty')); } - /** - * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException - */ - public function testSetValueThrowsExceptionIfArrayAccessExpected() - { - $path = new PropertyPath('[firstName]'); - - $path->setValue(new Author(), 'Bernhard'); - } - public function testSetValueUpdatesSetters() { $object = new Author(); diff --git a/src/Symfony/Component/Form/Util/PropertyPath.php b/src/Symfony/Component/Form/Util/PropertyPath.php index 81b2242bb9..0dff6f97db 100644 --- a/src/Symfony/Component/Form/Util/PropertyPath.php +++ b/src/Symfony/Component/Form/Util/PropertyPath.php @@ -365,6 +365,8 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface */ protected function &readProperty(&$objectOrArray, $property, $isIndex) { + // The local variable (instead of immediate returns) is necessary + // because we want to return by reference! $result = null; if ($isIndex) { @@ -373,7 +375,11 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface } if (isset($objectOrArray[$property])) { - $result =& $objectOrArray[$property]; + if (is_array($objectOrArray)) { + $result =& $objectOrArray[$property]; + } else { + $result = $objectOrArray[$property]; + } } } elseif (is_object($objectOrArray)) { $camelProp = $this->camelize($property);