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

Cannot use non-nullable properties with AccessInterceptorScopeLocalizer #682

Closed
petr-buchin opened this issue Apr 6, 2021 · 9 comments
Closed

Comments

@petr-buchin
Copy link

For now it's impossible to create proxies for objects that have non-nullable properties, even if that properties are being set in constructor.

Example:

namespace Test;

use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory as ProxyFactory;

class Foo
{
    public function __construct(protected Bar $bar)
    {
    }
}

$factory = new ProxyFactory();
$foo = new Foo();

$factory->makeProxy($foo, [], []);

This code generates an exception:

[ProxyManager\Exception\UnsupportedProxiedClassException]                                                                                     
  Cannot create references for following properties of class Test\Foo: bar

Is this an expected behavior?

@Ocramius
Copy link
Owner

Ocramius commented Apr 6, 2021 via email

@petr-buchin
Copy link
Author

@Ocramius as far as I see, this behavior can be fixed, if method Properties::onlyNonReferenceableProperties() will use constructor method reflection to see, what arguments does constructor have.

This way it can be sure that this properties will be initialized at the time of referencing them.

Do I understand that right?

If so, I could create a PR to address this issue.

@Ocramius
Copy link
Owner

Ocramius commented Apr 6, 2021 via email

@petr-buchin
Copy link
Author

petr-buchin commented Apr 6, 2021

or declare the property explicitly, like we did pre-php-8.0.

@Ocramius I tried declaring property explicitly and having standard constructor with assigning this property a value, but that did not help

@Ocramius
Copy link
Owner

Ocramius commented Apr 6, 2021 via email

@petr-buchin
Copy link
Author

@Ocramius oh, that's too bad to declare all properties as nullable :(

But anyway, thank you very much for your help!

@Ocramius
Copy link
Owner

Ocramius commented Apr 6, 2021 via email

@petr-buchyn
Copy link

@Ocramius unfortunately I need to use this kind of proxies, because I need to intercept method calls on $this, and they are not intercepted by value holder proxy.

What do you think about creating a proxy type that will be similar to scope localizer, but will use Closure::call() to access properties instead of direct references?

It probably would be slower, but it will have it’s use cases.

I would be happy to implement that kind of proxy in this library instead of creating another one.

@Ocramius
Copy link
Owner

Ocramius commented Apr 6, 2021 via email

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

3 participants