neos / flow-development-collection

The unified repository containing the Flow core packages, used for Flow development.
https://flow.neos.io/
MIT License
138 stars 188 forks source link

BUG: named arguments with proxied constructor wont work (fx when passing objects as argument) #3076

Open mhsdesign opened 1 year ago

mhsdesign commented 1 year ago

see https://github.com/neos/flow-development-collection/pull/3082/commits/2f7003f34346a8f6b7131acdaed7f9ce818a1386

for a failing test to prove ;)

class PrototypeClassJ
 {
     public function __construct(
         public ValueObjectClassA $classA
     ) {
     }
 }

you cant currently instantiate this like

new PrototypeClassJ(classA: $classA);

as the constructor of the proxy class will have no arguments.

a proxy constructor looks like:

#
# Start of Flow generated Proxy code
#
/**
 * A class of scope prototype with a value object as constructor argument
 * @codeCoverageIgnore
 */
class PrototypeClassJ extends PrototypeClassJ_Original implements \Neos\Flow\ObjectManagement\Proxy\ProxyInterface {

    /**
     * Autogenerated Proxy Method
     */
    public function __construct()
    {
        $arguments = func_get_args();

        if (!array_key_exists(0, $arguments)) $arguments[0] = \Neos\Flow\Core\Bootstrap::$staticObjectManager->get('Neos\Flow\Tests\Functional\ObjectManagement\Fixtures\ValueObjectClassA');
        if (!array_key_exists(0, $arguments)) throw new \Neos\Flow\ObjectManagement\Exception\UnresolvedDependenciesException('Missing required constructor argument $classA in class ' . __CLASS__ . '. Note that constructor injection is only support for objects of scope singleton (and this is not a singleton) – for other scopes you must pass each required argument to the constructor yourself.', 1296143788);
        parent::__construct(...$arguments);
    }
}

the proxy constructor should have all arguments, but nullable

     public function __construct(
         ?ValueObjectClassA $classA = null
     ) {
     }
mhsdesign commented 1 year ago

Implementing this might make https://github.com/neos/flow-development-collection/issues/2966 easier ^^

mhsdesign commented 1 year ago

This would also allow the Symfony\Component\Serializer\Normalizer\ObjectNormalizer to work with proxy classes see https://github.com/neos/neos-development-collection/blob/0976d919a6268f58e05f827793cd78fecd12c1ea/Neos.ContentRepositoryRegistry/Classes/Infrastructure/Property/Normalizer/ProxyAwareObjectNormalizer.php#L12