From 36ca108664d797a2dc68cc6d621924f2101e6af6 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Mon, 14 Apr 2014 12:19:16 +0200 Subject: [PATCH 1/5] array_keys does not work for ArrayAccess objects --- src/Symfony/Component/PropertyAccess/PropertyAccessor.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index a3e6c8a5dc05..62a5b51f6296 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -231,7 +231,11 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr // Create missing nested arrays on demand if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) { if (!$ignoreInvalidIndices) { - throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true))); + $message = sprintf('Cannot read property "%s".', $property); + if (is_array($objectOrArray)) { + $message .= sprintf(' Available properties are "%s"', print_r(array_keys($objectOrArray), true)); + } + throw new NoSuchIndexException($message); } $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null; From c7b9a4c3d88ce9bd69fc7d03927b306ba31fe148 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Wed, 30 Apr 2014 17:30:36 +0200 Subject: [PATCH 2/5] Add test for NoSuchIndexException --- .../Tests/PropertyAccessorCustomArrayObjectTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCustomArrayObjectTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCustomArrayObjectTest.php index 7340df720fbf..35fccbfa31ba 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCustomArrayObjectTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCustomArrayObjectTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\PropertyAccess\Tests; +use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\PropertyAccess\Tests\Fixtures\CustomArrayObject; class PropertyAccessorCustomArrayObjectTest extends PropertyAccessorCollectionTest @@ -19,4 +20,15 @@ protected function getCollection(array $array) { return new CustomArrayObject($array); } + + /** + * @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException + */ + public function testGetNoSuchIndex() + { + $arrayObject = new CustomArrayObject(array('foo', 'bar')); + $propertyAccessor = new PropertyAccessor(false, true); + + $propertyAccessor->getValue($arrayObject, '[2]'); + } } From b1c20d367776871c0bc30a7894bdf7380db548bd Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Thu, 1 May 2014 08:35:40 +0200 Subject: [PATCH 3/5] List keys for Traversable objects --- src/Symfony/Component/PropertyAccess/PropertyAccessor.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index 62a5b51f6296..33f1c146c98b 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -234,6 +234,12 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr $message = sprintf('Cannot read property "%s".', $property); if (is_array($objectOrArray)) { $message .= sprintf(' Available properties are "%s"', print_r(array_keys($objectOrArray), true)); + } elseif ($objectOrArray instanceof \Traversable) { + $keys = []; + foreach ($objectOrArray as $key => &$value) { + $keys[] = $key; + } + $message .= sprintf(' Available properties are "%s"', print_r($keys, true)); } throw new NoSuchIndexException($message); } From 83911e5af6ce30a2bd337c3d76417c07e0945fe8 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Thu, 1 May 2014 08:48:54 +0200 Subject: [PATCH 4/5] Add message if keys are not available --- .../Component/PropertyAccess/PropertyAccessor.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index 33f1c146c98b..5be0038f76ec 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -232,16 +232,17 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) { if (!$ignoreInvalidIndices) { $message = sprintf('Cannot read property "%s".', $property); + $keys = 'not available'; if (is_array($objectOrArray)) { - $message .= sprintf(' Available properties are "%s"', print_r(array_keys($objectOrArray), true)); + $keys = '"' . print_r(array_keys($objectOrArray), true) . '"'; } elseif ($objectOrArray instanceof \Traversable) { - $keys = []; + $list = []; foreach ($objectOrArray as $key => &$value) { - $keys[] = $key; + $list[] = $key; } - $message .= sprintf(' Available properties are "%s"', print_r($keys, true)); + $keys = '"' . print_r($list, true) . '"'; } - throw new NoSuchIndexException($message); + throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are %s', $property, $keys)); } $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null; From f49418c5bda0b6da6a78978bd1f6b8a3cb4177d2 Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Thu, 1 May 2014 09:59:19 +0200 Subject: [PATCH 5/5] code style --- src/Symfony/Component/PropertyAccess/PropertyAccessor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index 5be0038f76ec..dca547c9f009 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -234,13 +234,13 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr $message = sprintf('Cannot read property "%s".', $property); $keys = 'not available'; if (is_array($objectOrArray)) { - $keys = '"' . print_r(array_keys($objectOrArray), true) . '"'; + $keys = '"'.print_r(array_keys($objectOrArray), true).'"'; } elseif ($objectOrArray instanceof \Traversable) { - $list = []; + $list = array(); foreach ($objectOrArray as $key => &$value) { $list[] = $key; } - $keys = '"' . print_r($list, true) . '"'; + $keys = '"'.print_r($list, true).'"'; } throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are %s', $property, $keys)); }