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

Skip to content

Commit a689085

Browse files
bug #50280 [PropertyAccess] Fix nullsafe operator on array index (HypeMC)
This PR was merged into the 6.2 branch. Discussion ---------- [PropertyAccess] Fix nullsafe operator on array index | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Currently the nullsafe operator doesn't work with array index paths. However, this is not obvious at first since `THROW_ON_INVALID_PROPERTY_PATH` is not enabled by default: ```php use Symfony\Component\PropertyAccess\PropertyAccessor; var_dump((new PropertyAccessor())->getValue([], '[foo]')); // NULL var_dump((new PropertyAccessor())->getValue([], '[foo?]')); // NULL var_dump((new PropertyAccessor(throw: PropertyAccessor::THROW_ON_INVALID_INDEX))->getValue([], '[foo]')); // Cannot read index "foo" while trying to traverse path "[foo]". var_dump((new PropertyAccessor(throw: PropertyAccessor::THROW_ON_INVALID_INDEX))->getValue([], '[foo?]')); // Cannot read index "foo" while trying to traverse path "[foo]". (THIS IS WRONG, SHOULD BE NULL) ``` Commits ------- 5509d17 [PropertyAccess] Fix nullsafe operator on array index
2 parents 749b304 + 5509d17 commit a689085

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)