diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index a3e6c8a5dc05..dca547c9f009 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -231,7 +231,18 @@ 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); + $keys = 'not available'; + if (is_array($objectOrArray)) { + $keys = '"'.print_r(array_keys($objectOrArray), true).'"'; + } elseif ($objectOrArray instanceof \Traversable) { + $list = array(); + foreach ($objectOrArray as $key => &$value) { + $list[] = $key; + } + $keys = '"'.print_r($list, true).'"'; + } + throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are %s', $property, $keys)); } $objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null; 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]'); + } }