Skip to content

[PropertyAccess] Wrong NoSuchPropertyException with __get magic method #58378

@yann-eugone

Description

@yann-eugone

Symfony version(s) affected

=6.4

Description

If you have a decorator class that uses the __get magic
And if give an instance of this class to the PropertyAccessor

You get a NoSuchPropertyException with the message

Can\'t get a way to read the property "X" in class "Y".

Because PropertyAccessor assume that whenever it gets a PropertyReadInfo::TYPE_PROPERTY access, a property with that name must exists.

This is working well on 5.4, but start failing on 6.4.

How to reproduce

<?php

use Symfony\Component\PropertyAccess\PropertyAccess;

require_once __DIR__ . '/vendor/autoload.php';

class Decorated
{
    public string $foo = 'foo';
}

class Decorator
{
    private Decorated $object;

    public function __construct()
    {
        $this->object = new Decorated();
    }

    public function __get(string $name)
    {
        return $this->object->$name;
    }
}

$properties = PropertyAccess::createPropertyAccessorBuilder()
    ->enableMagicMethods()
    ->getPropertyAccessor();

echo $properties->getValue(new Decorator(), 'foo')."\n";

Possible Solution

The problem was not here in 5.x, because the component was not designed to look for PHP 8.x unitialialized properties.

So the only code involved in the issue is
https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L428-L440

The code was introduced in the PR #54194

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