Skip to content

2.4.5 - Interceptor code generation fails on Union return types #39611

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

Closed
5 tasks
Skullsneeze opened this issue Feb 7, 2025 · 2 comments
Closed
5 tasks

2.4.5 - Interceptor code generation fails on Union return types #39611

Skullsneeze opened this issue Feb 7, 2025 · 2 comments
Labels
Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@Skullsneeze
Copy link
Contributor

Summary

Similar to the issue described in #39502 code generation for interceptors also fails when union return types are used (eg. int|string). In the current 2.4 develop branch this is already refactored, and the responsible logic is moved to lib/internal/Magento/Framework/GetReflectionMethodReturnTypeValueTrait.php. However, for 2.4.5-p10, this is still an existing issue.

Examples

Create a new class which extends \Magento\Framework\App\ActionInterface. In the implemented execute() method add the return types to the method signature:

public function execute(): ResponseInterface|Json|ResultInterface

Run the setup:di:compile command.

You should now receive an error when the Interceptor code for the class is generated.

Call to undefined method ReflectionUnionType::getName() in vendor/magento/framework/Interception/Code/Generator/Interceptor.php

Proposed solution

The following patch applies a change similar to the fix done in the issue #39502. The original commit was e4d1043

Patch:

diff --git a/vendor/magento/framework/Interception/Code/Generator/Interceptor.php b/vendor/magento/framework/Interception/Code/Generator/Interceptor.php
--- a/vendor/magento/framework/Interception/Code/Generator/Interceptor.php
+++ b/vendor/magento/framework/Interception/Code/Generator/Interceptor.php
@@ -212,10 +212,17 @@
         $returnTypeValue = null;
         $returnType = $method->getReturnType();
         if ($returnType) {
-            $returnTypeValue = ($returnType->allowsNull() ? '?' : '');
-            $returnTypeValue .= ($returnType->getName() === 'self')
-                ? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
-                : $returnType->getName();
+            if ($returnType instanceof \ReflectionUnionType) {
+                $returnTypeValue = implode('|', $returnType->getTypes());
+            } elseif ($returnType->getName() === 'self') {
+                $returnTypeValue = $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName());
+            } else {
+                $returnTypeValue = $returnType->getName();
+            }
+
+            if ($returnType->allowsNull()) {
+                $returnTypeValue = "?$returnTypeValue";
+            }
         }
 
         return $returnTypeValue;

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@Skullsneeze Skullsneeze added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Feb 7, 2025
Copy link

m2-assistant bot commented Feb 7, 2025

Hi @Skullsneeze. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@Skullsneeze
Copy link
Contributor Author

Closing this as this is fixed in the latest develop version, but I did want to provide a patch for those who might still face this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
None yet
Development

No branches or pull requests

1 participant