Skip to content

[PropertyAccess] NullSafe operator should stop on null value and accept x?.y #48726

Closed
@Vincz

Description

@Vincz

Symfony version(s) affected

6.2.x

Description

The example provided in the doc var_dump($propertyAccessor->getValue($comment, 'person?.firstname')); actually doesn't work.
For it to works, we must set the path as person?.firstname?.
When an null safe null value is encoutenred in the property chain, the reader should stop and return.
At the moment it continues, leading to a read on a null value and an exception.

How to reproduce

<?php

declare(strict_types=1);

require_once './vendor/autoload.php';

use Symfony\Component\PropertyAccess\PropertyAccess;

class Person
{
}

class Comment
{
    public ?Person $person = null;
    public string $message;
}

$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
    ->disableExceptionOnInvalidPropertyPath()
    ->getPropertyAccessor()
;

$comment = new Comment();

$propertyAccessor->getValue($comment, 'person?.name');
// Throw : PHP Fatal error:  Uncaught Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: Cannot read property "name" from an array. Maybe you intended to write the property path as "[name]" instead. in /Volumes/x/dev/ae/opus/vendor/symfony/property-access/PropertyAccessor.php:392

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions