Ocramius/ProxyManager

Nullable constructor property promotion support

Grundik opened this issue · 2 comments

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);
    }
...

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.

I've added tests into that PR: #785