Skip to content
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

Public readonly properties #26

Open
lexalium opened this issue Sep 1, 2022 · 2 comments
Open

Public readonly properties #26

lexalium opened this issue Sep 1, 2022 · 2 comments

Comments

@lexalium
Copy link

lexalium commented Sep 1, 2022

I have an issue when trying to read public readonly properties from the Proxy. I got an error:

Cannot initialize readonly property App\Entity\Customer::$firstName from scope App\Command\Command

Library version: 1.0.12

Preconditions:
We have a class only with public properties.

<?php

declare(strict_types=1);

namespace App\Entity;

class Customer
{
    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
    }
}

Steps to Reproduce:

  1. Create a Proxy object
$instance = (new LazyLoadingGhostFactory())->createProxy(
    Customer::class,
    static function (
        GhostObjectInterface $ghostObject,
        string $method,
        array $parameters,
        ?Closure &$initializer,
        array $properties,
    ) {
        $initializer = null;

        $properties['firstName'] = 'User';
        $properties['lastName'] = 'Test';

        return true;
    },
);
  1. Getting value from any public property results in an error.
    Cannot initialize readonly property App\Entity\Customer::$firstName from scope App\Command\Command
$output->writeln('First Name: ' . $customer->firstName);
$output->writeln('Last Name: ' . $customer->lastName);

After investigation, I see the $class and $scopeObject variables equal to the App\Command\Command.

} elseif (isset(self::$privateProperties98259[$name])) {
    $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
    $caller  = isset($callers[1]) ? $callers[1] : [];
    $class   = isset($caller['class']) ? $caller['class'] : '';
    ...
}

...
$targetObject = $realInstanceReflection->newInstanceWithoutConstructor();
$accessor = function & () use ($targetObject, $name) {
    return $targetObject->$name;
};
$backtrace = debug_backtrace(true, 2);
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();

Note
But if we will add a getter for the property all works without errors.

class Customer
{
    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }
}
$output->writeln('First Name: ' . $customer->getFirstName());
$output->writeln('Last Name: ' . $customer->lastName);
@nicolas-grekas
Copy link
Collaborator

Can you provide a reproducing test case please?

@nicolas-grekas
Copy link
Collaborator

This test case fails:

--TEST--
Verifies that public readonly properties can be used
--FILE--
<?php

require_once __DIR__ . '/init.php';

use ProxyManager\Proxy\GhostObjectInterface;

class Kitchen
{
    public readonly string $sweets;
}

$factory = new \ProxyManager\Factory\LazyLoadingGhostFactory($configuration);

$proxy = $factory->createProxy(Kitchen::class, function (
    GhostObjectInterface $ghostObject,
    string $method,
    array $parameters,
    ?Closure &$initializer,
    array $properties,
) {
    $properties['sweets'] = 'cookies';
});

echo $proxy->sweets;
?>
--EXPECTF--
cookies

The reason is that PublicScopeSimulator generates an accessor that returns by reference for __get().
Feel free to give a fix a try. Alternatively, you might want to wait for Symfony 6.2, which will provide another implementation of ghost objects that's free from this issue already. See https://symfony.com/blog/revisiting-lazy-loading-proxies-in-php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants