Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5509d17

Browse files
committed
[PropertyAccess] Fix nullsafe operator on array index
1 parent 3835573 commit 5509d17

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private function readPropertiesUntil(array $zval, PropertyPathInterface $propert
301301
if (($zval[self::VALUE] instanceof \ArrayAccess && !$zval[self::VALUE]->offsetExists($property)) ||
302302
(\is_array($zval[self::VALUE]) && !isset($zval[self::VALUE][$property]) && !\array_key_exists($property, $zval[self::VALUE]))
303303
) {
304-
if (!$ignoreInvalidIndices) {
304+
if (!$ignoreInvalidIndices && !$isNullSafe) {
305305
if (!\is_array($zval[self::VALUE])) {
306306
if (!$zval[self::VALUE] instanceof \Traversable) {
307307
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,28 @@ public static function getValidReadPropertyPaths(): iterable
573573
yield [(object) ['foo' => null], 'foo?.bar.baz', null];
574574
yield [(object) ['foo' => (object) ['bar' => null]], 'foo?.bar?.baz', null];
575575
yield [(object) ['foo' => (object) ['bar' => null]], 'foo.bar?.baz', null];
576+
577+
yield from self::getNullSafeIndexPaths();
578+
}
579+
580+
public static function getNullSafeIndexPaths(): iterable
581+
{
576582
yield [(object) ['foo' => ['bar' => null]], 'foo[bar?].baz', null];
577583
yield [[], '[foo?]', null];
578584
yield [['foo' => ['firstName' => 'Bernhard']], '[foo][bar?]', null];
579585
yield [['foo' => ['firstName' => 'Bernhard']], '[foo][bar?][baz?]', null];
580586
}
581587

588+
/**
589+
* @dataProvider getNullSafeIndexPaths
590+
*/
591+
public function testNullSafeIndexWithThrowOnInvalidIndex($objectOrArray, $path, $value)
592+
{
593+
$this->propertyAccessor = new PropertyAccessor(PropertyAccessor::DISALLOW_MAGIC_METHODS, PropertyAccessor::THROW_ON_INVALID_INDEX | PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH);
594+
595+
$this->assertSame($value, $this->propertyAccessor->getValue($objectOrArray, $path));
596+
}
597+
582598
public function testTicket5755()
583599
{
584600
$object = new Ticket5775Object();

0 commit comments

Comments
 (0)