From 4262707e5aeee5b67448ba2fb422d2f6c8074445 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 28 Mar 2014 19:21:04 +0100 Subject: [PATCH] [PropertyAccess] Fixed CS and added missing documentation --- .../PropertyAccess/PropertyAccessor.php | 21 +++++++++++-------- .../Tests/PropertyAccessorCollectionTest.php | 2 -- .../Tests/PropertyAccessorTest.php | 1 - 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index b21866bd68..8c86d88303 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -33,7 +33,7 @@ class PropertyAccessor implements PropertyAccessorInterface /** * @var Boolean */ - private $throwExceptionOnInvalidIndex; + private $ignoreInvalidIndices; /** * Should not be used by application code. Use @@ -42,7 +42,7 @@ class PropertyAccessor implements PropertyAccessorInterface public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false) { $this->magicCall = $magicCall; - $this->throwExceptionOnInvalidIndex = $throwExceptionOnInvalidIndex; + $this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex; } /** @@ -56,7 +56,7 @@ class PropertyAccessor implements PropertyAccessorInterface throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface'); } - $propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex); + $propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices); return $propertyValues[count($propertyValues) - 1][self::VALUE]; } @@ -117,7 +117,7 @@ class PropertyAccessor implements PropertyAccessorInterface } try { - $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex); + $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices); return true; } catch (NoSuchIndexException $e) { @@ -186,15 +186,18 @@ class PropertyAccessor implements PropertyAccessorInterface /** * Reads the path from an object up to a given path index. * - * @param object|array $objectOrArray The object or array to read from - * @param PropertyPathInterface $propertyPath The property path to read - * @param integer $lastIndex The index up to which should be read + * @param object|array $objectOrArray The object or array to read from + * @param PropertyPathInterface $propertyPath The property path to read + * @param integer $lastIndex The index up to which should be read + * @param Boolean $ignoreInvalidIndices Whether to ignore invalid indices + * or throw an exception * * @return array The values read in the path. * * @throws UnexpectedTypeException If a value within the path is neither object nor array. + * @throws NoSuchIndexException If a non-existing index is accessed */ - private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $throwExceptionOnInvalidIndex = false) + private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true) { $propertyValues = array(); @@ -209,7 +212,7 @@ class PropertyAccessor implements PropertyAccessorInterface // Create missing nested arrays on demand if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) { - if ($throwExceptionOnInvalidIndex) { + if (!$ignoreInvalidIndices) { throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true))); } diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php index cd51f2601c..18211c822b 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php @@ -11,9 +11,7 @@ namespace Symfony\Component\PropertyAccess\Tests; -use Symfony\Component\PropertyAccess\Exception\ExceptionInterface; use Symfony\Component\PropertyAccess\PropertyAccessor; -use Symfony\Component\PropertyAccess\StringUtil; class PropertyAccessorCollectionTest_Car { diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php index a40c2d9ffa..a6b09fa0ab 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php @@ -13,7 +13,6 @@ namespace Symfony\Component\PropertyAccess\Tests; use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass; -use Symfony\Component\PropertyAccess\Tests\Fixtures\Magician; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;