Skip to content

Commit bfaa677

Browse files
committed
AC-6437:Ensure PHP8.1 Support after 7.4 removal - Added PHP 8 Union Types Support for function Return Type
1 parent a4e3f9a commit bfaa677

File tree

10 files changed

+168
-15
lines changed

10 files changed

+168
-15
lines changed

lib/internal/Magento/Framework/Async/Code/Generator/ProxyDeferredGenerator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,20 @@ private function getReturnTypeValue(\ReflectionMethod $method): ?string
246246
$returnTypeValue = null;
247247
$returnType = $method->getReturnType();
248248
if ($returnType) {
249-
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
250-
$returnTypeValue .= ($returnType->getName() === 'self')
251-
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
252-
: $returnType->getName();
249+
if ($returnType instanceof \ReflectionUnionType) {
250+
$returnTypeValue = [];
251+
foreach ($method->getReturnType()->getTypes() as $type) {
252+
$returnTypeValue[] = $type->getName();
253+
}
254+
255+
$returnTypeValue = implode('|', $returnTypeValue);
256+
257+
} else {
258+
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
259+
$returnTypeValue .= ($returnType->getName() === 'self')
260+
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
261+
: $returnType->getName();
262+
}
253263
}
254264

255265
return $returnTypeValue;

lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,21 @@ private function getReturnTypeValue(\ReflectionMethod $method): ?string
212212
$returnTypeValue = null;
213213
$returnType = $method->getReturnType();
214214
if ($returnType) {
215-
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
216-
$returnTypeValue .= ($returnType->getName() === 'self')
217-
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
218-
: $returnType->getName();
215+
216+
if ($returnType instanceof \ReflectionUnionType) {
217+
$returnTypeValue = [];
218+
foreach ($method->getReturnType()->getTypes() as $type) {
219+
$returnTypeValue[] = $type->getName();
220+
}
221+
222+
$returnTypeValue = implode('|', $returnTypeValue);
223+
224+
} else {
225+
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
226+
$returnTypeValue .= ($returnType->getName() === 'self')
227+
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
228+
: $returnType->getName();
229+
}
219230
}
220231

221232
return $returnTypeValue;

lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/InterceptorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function interceptorDataProvider()
9696
\Magento\Framework\Interception\Code\Generator\SampleBackendMenu\Interceptor::class,
9797
'SampleBackendMenuInterceptor',
9898
],
99+
[
100+
\Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample::class,
101+
\Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample\Interceptor::class,
102+
'ReflectionUnionTypeSampleInterceptor',
103+
],
99104
];
100105
}
101106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Interception\Code\Generator;
9+
10+
class ReflectionUnionTypeSample
11+
{
12+
private int|string $attribute;
13+
14+
public function getValue(): int|string
15+
{
16+
return $this->attribute;
17+
}
18+
19+
/**
20+
* @param int|string $value
21+
*/
22+
public function setValue(int|string $value)
23+
{
24+
$this->attribute = $value;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample;
2+
3+
/**
4+
* Interceptor class for @see \Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample
5+
*/
6+
class Interceptor extends \Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample implements \Magento\Framework\Interception\InterceptorInterface
7+
{
8+
use \Magento\Framework\Interception\Interceptor;
9+
10+
public function __construct()
11+
{
12+
$this->___init();
13+
}
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getValue() : int|string
19+
{
20+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValue');
21+
return $pluginInfo ? $this->___callPlugins('getValue', func_get_args(), $pluginInfo) : parent::getValue();
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function setValue(int|string $value)
28+
{
29+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'setValue');
30+
return $pluginInfo ? $this->___callPlugins('setValue', func_get_args(), $pluginInfo) : parent::setValue($value);
31+
}
32+
}

lib/internal/Magento/Framework/MessageQueue/Code/Generator/RemoteServiceGenerator.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
namespace Magento\Framework\MessageQueue\Code\Generator;
77

8+
use Laminas\Code\Reflection\MethodReflection;
89
use Magento\Framework\Code\Generator\DefinedClasses;
910
use Magento\Framework\Code\Generator\Io;
1011
use Magento\Framework\Communication\Config\ReflectionGenerator;
1112
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
1213
use Magento\Framework\MessageQueue\Code\Generator\Config\RemoteServiceReader\Communication as RemoteServiceReader;
1314
use Magento\Framework\Reflection\MethodsMap as ServiceMethodsMap;
14-
use Laminas\Code\Reflection\MethodReflection;
1515

1616
/**
1717
* Code generator for remote services.
@@ -173,7 +173,18 @@ protected function _getClassMethods()
173173
*/
174174
private function convertMethodType($type)
175175
{
176-
return $type instanceof \ReflectionNamedType ? $type->getName() : $type;
176+
$returnTypeValue = $type instanceof \ReflectionNamedType ? $type->getName() : $type;
177+
178+
if ($type instanceof \ReflectionUnionType) {
179+
$returnTypeValue = [];
180+
foreach ($type->getTypes() as $type) {
181+
$returnTypeValue[] = $type->getName();
182+
}
183+
184+
$returnTypeValue = implode('|', $returnTypeValue);
185+
}
186+
187+
return $returnTypeValue;
177188
}
178189

179190
/**

lib/internal/Magento/Framework/ObjectManager/Code/Generator/Proxy.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,20 @@ private function getReturnTypeValue(\ReflectionMethod $method): ?string
280280
$returnTypeValue = null;
281281
$returnType = $method->getReturnType();
282282
if ($returnType) {
283-
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
284-
$returnTypeValue .= ($returnType->getName() === 'self')
285-
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
286-
: $returnType->getName();
283+
if ($returnType instanceof \ReflectionUnionType) {
284+
$returnTypeValue = [];
285+
foreach ($method->getReturnType()->getTypes() as $type) {
286+
$returnTypeValue[] = $type->getName();
287+
}
288+
289+
$returnTypeValue = implode('|', $returnTypeValue);
290+
291+
} else {
292+
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');
293+
$returnTypeValue .= ($returnType->getName() === 'self')
294+
? $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName())
295+
: $returnType->getName();
296+
}
287297
}
288298

289299
return $returnTypeValue;

lib/internal/Magento/Framework/ObjectManager/Code/Generator/Repository.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,17 @@ private function getClassMethods($name)
750750
*/
751751
private function getTypeHintText($type)
752752
{
753-
return $type instanceof \ReflectionType ? $type->getName() : $type;
753+
$returnTypeValue = $type instanceof \ReflectionType ? $type->getName() : $type;
754+
755+
if ($type instanceof \ReflectionUnionType) {
756+
$returnTypeValue = [];
757+
foreach ($type->getTypes() as $type) {
758+
$returnTypeValue[] = $type->getName();
759+
}
760+
761+
$returnTypeValue = implode('|', $returnTypeValue);
762+
}
763+
764+
return $returnTypeValue;
754765
}
755766
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Code/Generator/_files/Sample.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class Sample
2222
*/
2323
private $config = [];
2424

25+
/**
26+
* @var int|string
27+
*/
28+
private int|string $attribute;
29+
2530
/**
2631
* @param array $messages
2732
*/
@@ -53,4 +58,20 @@ public function getConfig(): array
5358
{
5459
return $this->config;
5560
}
61+
62+
/**
63+
* @param int|string $attribute
64+
*/
65+
public function setAttribute(int|string $attribute)
66+
{
67+
$this->attribute = $attribute;
68+
}
69+
70+
/**
71+
* @return int|string
72+
*/
73+
public function getAttribute(): int|string
74+
{
75+
return $this->attribute;
76+
}
5677
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Code/Generator/_files/SampleProxy.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,20 @@ class Sample_Proxy extends Sample implements \Magento\Framework\ObjectManager\No
117117
{
118118
return $this->_getSubject()->getConfig();
119119
}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
public function setAttribute(int|string $attribute)
125+
{
126+
return $this->_getSubject()->setAttribute($attribute);
127+
}
128+
129+
/**
130+
* {@inheritdoc}
131+
*/
132+
public function getAttribute() : int|string
133+
{
134+
return $this->_getSubject()->getAttribute();
135+
}
120136
}

0 commit comments

Comments
 (0)