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

Nullable constructor property promotion support #784

Open
Grundik opened this issue May 16, 2023 · 2 comments · Fixed by FriendsOfPHP/proxy-manager-lts#32 · May be fixed by #785
Open

Nullable constructor property promotion support #784

Grundik opened this issue May 16, 2023 · 2 comments · Fixed by FriendsOfPHP/proxy-manager-lts#32 · May be fixed by #785

Comments

@Grundik
Copy link

Grundik commented May 16, 2023

Nullable modifier is lost in promoted constructor properties. For example, this code will result fatal error:

class Test {
    public function __construct(
        protected ?int $value
    ) {
    }
}

$factory = new \ProxyManager\Factory\LazyLoadingValueHolderFactory();

$proxy = $factory->createProxy(
    Test::class,
    function (& $wrappedObject, $proxy, $method, $parameters, & $initializer) {
        $wrappedObject = new Test(null);
    }
);
PHP Fatal error:  Type of ProxyManagerGeneratedProxy\__PM__\Test\Generated1c12fa522c43907846cdf9382a0d356f::$value must be ?int (as in class Test) in .../vendor/ocramius/proxy-manager/src/ProxyManager/GeneratorStrategy/EvaluatingGeneratorStrategy.php(54) : eval()'d code on line 3

And indeed, property is no longer nullable in generated proxy:

namespace ProxyManagerGeneratedProxy\__PM__\Test;

class Generated7ef5eb7812f288337a0d82be0b42baa6 extends \Test implements \ProxyManager\Proxy\VirtualProxyInterface
{
...

    public function __construct(protected int $value) // <---------------- nullable modificator is lost
    {
        static $reflection;

        if (! $this->valueHoldere9785) {
            $reflection = $reflection ?? new \ReflectionClass('Test');
            $this->valueHoldere9785 = $reflection->newInstanceWithoutConstructor();
        unset($this->value);

        }

        $this->valueHoldere9785->__construct($value);
    }
...
@Grundik
Copy link
Author

Grundik commented May 16, 2023

Issue does not occur with laminas-code 4.4 (specified in the composer.lock), since it does not supports constructior property promotion at all. But it laminas-code 4.7 does support that, and then this issue arises.

@Grundik
Copy link
Author

Grundik commented May 16, 2023

I've added tests into that PR: #785

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