creatuity / magento2-interceptors

New interceptors approach for Magento 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incompatible with PHP 8.0 ReflectionUnionType and ReflectionIntersectionType

rhoerr opened this issue · comments

In the latest Magento release with paypal/module-braintree 4.5.0 -> 4.5.0-p1, a union return type was introduced:

\PayPal\Braintree\Controller\Payment\GetNonce::execute

-    public function execute()
+    public function execute(): Json|ResultInterface|ResponseInterface

This causes an error in compilation:

There is an error in vendor/creatuity/magento2-interceptors/Generator/CompiledInterceptor.php at line: 597
Call to undefined method ReflectionUnionType::getName()
#0 vendor/creatuity/magento2-interceptors/Generator/CompiledInterceptor.php(227): Creatuity\Interception\Generator\CompiledInterceptor->getCompiledMethodInfo(Object(ReflectionMethod), Array)
#1 vendor/creatuity/magento2-interceptors/Generator/CompiledInterceptor.php(154): Creatuity\Interception\Generator\CompiledInterceptor->overrideMethodsAndGeneratePluginGetters(Object(ReflectionClass))
#2 vendor/creatuity/magento2-interceptors/Generator/CompiledInterceptor.php(127): Creatuity\Interception\Generator\CompiledInterceptor->generateMethodsAndProperties()

The reflection data for this method is:

    /**
     * @inheritdoc
     */
    Method [ <user, overwrites Magento\Framework\App\ActionInterface, prototype Magento\Framework\App\ActionInterface> public method execute ] {
      @@ vendor/paypal/module-braintree-core/Controller/Payment/GetNonce.php 58 - 76

      - Parameters [0] {
      }
      - Return [ Magento\Framework\Controller\Result\Json|Magento\Framework\Controller\ResultInterface|Magento\Framework\App\ResponseInterface ]
    }

The same error will occur for any union or intersection return type encountered, because getCompiledMethodInfo() references ReflectionType->getName() in two places, but only ReflectionNamedType implements that method; ReflectionUnionType and ReflectionIntersectionType do not. See: https://www.php.net/manual/en/class.reflectiontype.php

This will require changes to https://github.com/creatuity/magento2-interceptors/blob/master/Generator/CompiledInterceptor.php#L593 to handle the situation where getReturnType() is one of those new types and contains multiple sub-types, obtained by getTypes().