Skip to content

Commit 4262707

Browse files
committed
[PropertyAccess] Fixed CS and added missing documentation
1 parent 6d2af21 commit 4262707

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PropertyAccessor implements PropertyAccessorInterface
3333
/**
3434
* @var Boolean
3535
*/
36-
private $throwExceptionOnInvalidIndex;
36+
private $ignoreInvalidIndices;
3737

3838
/**
3939
* Should not be used by application code. Use
@@ -42,7 +42,7 @@ class PropertyAccessor implements PropertyAccessorInterface
4242
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
4343
{
4444
$this->magicCall = $magicCall;
45-
$this->throwExceptionOnInvalidIndex = $throwExceptionOnInvalidIndex;
45+
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
4646
}
4747

4848
/**
@@ -56,7 +56,7 @@ public function getValue($objectOrArray, $propertyPath)
5656
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
5757
}
5858

59-
$propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex);
59+
$propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
6060

6161
return $propertyValues[count($propertyValues) - 1][self::VALUE];
6262
}
@@ -117,7 +117,7 @@ public function isReadable($objectOrArray, $propertyPath)
117117
}
118118

119119
try {
120-
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex);
120+
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
121121

122122
return true;
123123
} catch (NoSuchIndexException $e) {
@@ -186,15 +186,18 @@ public function isWritable($objectOrArray, $propertyPath, $value)
186186
/**
187187
* Reads the path from an object up to a given path index.
188188
*
189-
* @param object|array $objectOrArray The object or array to read from
190-
* @param PropertyPathInterface $propertyPath The property path to read
191-
* @param integer $lastIndex The index up to which should be read
189+
* @param object|array $objectOrArray The object or array to read from
190+
* @param PropertyPathInterface $propertyPath The property path to read
191+
* @param integer $lastIndex The index up to which should be read
192+
* @param Boolean $ignoreInvalidIndices Whether to ignore invalid indices
193+
* or throw an exception
192194
*
193195
* @return array The values read in the path.
194196
*
195197
* @throws UnexpectedTypeException If a value within the path is neither object nor array.
198+
* @throws NoSuchIndexException If a non-existing index is accessed
196199
*/
197-
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $throwExceptionOnInvalidIndex = false)
200+
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
198201
{
199202
$propertyValues = array();
200203

@@ -209,7 +212,7 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
209212

210213
// Create missing nested arrays on demand
211214
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
212-
if ($throwExceptionOnInvalidIndex) {
215+
if (!$ignoreInvalidIndices) {
213216
throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true)));
214217
}
215218

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

14-
use Symfony\Component\PropertyAccess\Exception\ExceptionInterface;
1514
use Symfony\Component\PropertyAccess\PropertyAccessor;
16-
use Symfony\Component\PropertyAccess\StringUtil;
1715

1816
class PropertyAccessorCollectionTest_Car
1917
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\PropertyAccess\PropertyAccessor;
1515
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
16-
use Symfony\Component\PropertyAccess\Tests\Fixtures\Magician;
1716
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
1817
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;
1918

0 commit comments

Comments
 (0)