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

Skip to content

[PropertyAccess] Fix nullsafe operator on array index #50280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
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