8000 [PropertyAccess] Fix nullsafe operator on array index by HypeMC · Pull Request #50280 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private function readPropertiesUntil(array $zval, PropertyPathInterface $propert
if (($zval[self::VALUE] instanceof \ArrayAccess && !$zval[self::VALUE]->offsetExists($property)) ||
(\is_array($zval[self::VALUE]) && !isset($zval[self::VALUE][$property]) && !\array_key_exists($property, $zval[self::VALUE]))
) {
if (!$ignoreInvalidIndices) {
if (!$ignoreInvalidIndices && !$isNullSafe) {
if (!\is_array($zval[self::VALUE])) {
if (!$zval[self::VALUE] instanceof \Traversable) {
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,28 @@ public static function getValidReadPropertyPaths(): iterable
yield [(object) ['foo' => null], 'foo?.bar.baz', null];
yield [(object) ['foo' => (object) ['bar' => null]], 'foo?.bar?.baz', null];
yield [(object) ['foo' => (object) ['bar' => null]], 'foo.bar?.baz', null];

yield from self::getNullSafeIndexPaths();
}

public static function getNullSafeIndexPaths(): iterable
{
yield [(object) ['foo' => ['bar' => null]], 'foo[bar?].baz', null];
yield [[], '[foo?]', null];
yield [['foo' => ['firstName' => 'Bernhard']], '[foo][bar?]', null];
yield [['foo' => ['firstName' => 'Bernhard']], '[foo][bar?][baz?]', null];
}

/**
* @dataProvider getNullSafeIndexPaths
*/
public function testNullSafeIndexWithThrowOnInvalidIndex($objectOrArray, $path, $value)
{
$this->propertyAccessor = new PropertyAccessor(PropertyAccessor::DISALLOW_MAGIC_METHODS, PropertyAccessor::THROW_ON_INVALID_INDEX | PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH);

$this->assertSame($value, $this->propertyAccessor->getValue($objectOrArray, $path));
}

public function testTicket5755()
{
$object = new Ticket5775Object();
Expand Down
0