Skip to content

Commit 0d17e4e

Browse files
committed
AC-6437:Ensure PHP8.1 Support after 7.4 removal - Added PHP 8 Intersection Type support for function Parameters and Return Type
1 parent f84c7aa commit 0d17e4e

File tree

9 files changed

+217
-14
lines changed

9 files changed

+217
-14
lines changed

lib/internal/Magento/Framework/Code/Generator/EntityAbstract.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ private function extractParameterType(
338338
if ($parameterType instanceof \ReflectionUnionType) {
339339
$parameterType = $parameterType->getTypes();
340340
$parameterType = implode('|', $parameterType);
341+
} elseif ($parameterType instanceof \ReflectionIntersectionType) {
342+
$parameterType = $parameterType->getTypes();
343+
$parameterType = implode('&', $parameterType);
341344
} else {
342345
$parameterType = $parameterType->getName();
343346
}

lib/internal/Magento/Framework/GetReflectionMethodReturnTypeValueTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ private function getReturnTypeValue(\ReflectionMethod $method): ?string
2323
$returnTypeValue = null;
2424
$returnType = $method->getReturnType();
2525
if ($returnType) {
26-
if ($returnType instanceof \ReflectionUnionType) {
26+
if ($returnType instanceof \ReflectionUnionType || $returnType instanceof \ReflectionIntersectionType) {
2727
$returnTypeValue = [];
2828
foreach ($method->getReturnType()->getTypes() as $type) {
2929
$returnTypeValue[] = $type->getName();
3030
}
3131

32-
$returnTypeValue = implode('|', $returnTypeValue);
32+
$returnTypeValue = implode(
33+
$returnType instanceof \ReflectionUnionType ? '|' : '&',
34+
$returnTypeValue
35+
);
3336
} else {
3437
$className = $method->getDeclaringClass()->getName();
3538
$returnTypeValue = ($returnType->allowsNull() && $returnType->getName() !== 'mixed' ? '?' : '');

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
use Composer\Autoload\ClassLoader;
1111
use Magento\Framework\Code\Generator\Io;
1212
use Magento\Framework\Interception\Code\Generator\Interceptor;
13+
use Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample;
14+
use Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample;
15+
use Magento\Framework\Interception\Code\Generator\Sample;
16+
use Magento\Framework\Interception\Code\Generator\SampleBackendMenu;
17+
use Magento\Framework\Interception\Code\Generator\TSample;
1318
use PHPUnit\Framework\MockObject\MockObject;
1419
use PHPUnit\Framework\TestCase;
1520

@@ -82,25 +87,30 @@ public function interceptorDataProvider()
8287
{
8388
return [
8489
[
85-
\Magento\Framework\Interception\Code\Generator\Sample::class,
86-
\Magento\Framework\Interception\Code\Generator\Sample\Interceptor::class,
90+
Sample::class,
91+
Sample\Interceptor::class,
8792
'Interceptor'
8893
],
8994
[
90-
\Magento\Framework\Interception\Code\Generator\TSample::class,
91-
\Magento\Framework\Interception\Code\Generator\TSample\Interceptor::class,
95+
TSample::class,
96+
TSample\Interceptor::class,
9297
'TInterceptor'
9398
],
9499
[
95-
\Magento\Framework\Interception\Code\Generator\SampleBackendMenu::class,
96-
\Magento\Framework\Interception\Code\Generator\SampleBackendMenu\Interceptor::class,
100+
SampleBackendMenu::class,
101+
SampleBackendMenu\Interceptor::class,
97102
'SampleBackendMenuInterceptor',
98103
],
99104
[
100-
\Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample::class,
101-
\Magento\Framework\Interception\Code\Generator\ReflectionUnionTypeSample\Interceptor::class,
105+
ReflectionUnionTypeSample::class,
106+
ReflectionUnionTypeSample\Interceptor::class,
102107
'ReflectionUnionTypeSampleInterceptor',
103108
],
109+
[
110+
ReflectionIntersectionTypeSample::class,
111+
ReflectionIntersectionTypeSample\Interceptor::class,
112+
'ReflectionIntersectionTypeSampleInterceptor',
113+
],
104114
];
105115
}
106116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
use Magento\Backend\Model\Menu;
11+
12+
class ReflectionIntersectionTypeSample extends Menu
13+
{
14+
/**
15+
* Intersection type attribute
16+
*
17+
* @var ReflectionIntersectionTypeSample&Menu
18+
*/
19+
private ReflectionIntersectionTypeSample&Menu $attribute;
20+
21+
/**
22+
* @return ReflectionIntersectionTypeSample&Menu
23+
*/
24+
public function getValue(): ReflectionIntersectionTypeSample&Menu
25+
{
26+
return $this->attribute;
27+
}
28+
29+
/**
30+
* @param ReflectionIntersectionTypeSample&Menu $value
31+
*/
32+
public function setValue(ReflectionIntersectionTypeSample&Menu $value)
33+
{
34+
$this->attribute = $value;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
namespace Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample;
2+
3+
/**
4+
* Interceptor class for @see \Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample
5+
*/
6+
class Interceptor extends \Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample implements \Magento\Framework\Interception\InterceptorInterface
7+
{
8+
use \Magento\Framework\Interception\Interceptor;
9+
10+
public function __construct(\Psr\Log\LoggerInterface $logger, $pathInMenuStructure = '', ?\Magento\Backend\Model\Menu\Item\Factory $menuItemFactory = null, ?\Magento\Framework\Serialize\SerializerInterface $serializer = null)
11+
{
12+
$this->___init();
13+
parent::__construct($logger, $pathInMenuStructure, $menuItemFactory, $serializer);
14+
}
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function getValue() : \Magento\Backend\Model\Menu&\Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample
20+
{
21+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValue');
22+
return $pluginInfo ? $this->___callPlugins('getValue', func_get_args(), $pluginInfo) : parent::getValue();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function setValue(\Magento\Backend\Model\Menu&\Magento\Framework\Interception\Code\Generator\ReflectionIntersectionTypeSample $value)
29+
{
30+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'setValue');
31+
return $pluginInfo ? $this->___callPlugins('setValue', func_get_args(), $pluginInfo) : parent::setValue($value);
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $index = null)
38+
{
39+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'add');
40+
return $pluginInfo ? $this->___callPlugins('add', func_get_args(), $pluginInfo) : parent::add($item, $parentId, $index);
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function get($itemId)
47+
{
48+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'get');
49+
return $pluginInfo ? $this->___callPlugins('get', func_get_args(), $pluginInfo) : parent::get($itemId);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function move($itemId, $toItemId, $sortIndex = null)
56+
{
57+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'move');
58+
return $pluginInfo ? $this->___callPlugins('move', func_get_args(), $pluginInfo) : parent::move($itemId, $toItemId, $sortIndex);
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function remove($itemId)
65+
{
66+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'remove');
67+
return $pluginInfo ? $this->___callPlugins('remove', func_get_args(), $pluginInfo) : parent::remove($itemId);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function reorder($itemId, $position)
74+
{
75+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'reorder');
76+
return $pluginInfo ? $this->___callPlugins('reorder', func_get_args(), $pluginInfo) : parent::reorder($itemId, $position);
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function isLast(\Magento\Backend\Model\Menu\Item $item)
83+
{
84+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'isLast');
85+
return $pluginInfo ? $this->___callPlugins('isLast', func_get_args(), $pluginInfo) : parent::isLast($item);
86+
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function getFirstAvailable()
92+
{
93+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getFirstAvailable');
94+
return $pluginInfo ? $this->___callPlugins('getFirstAvailable', func_get_args(), $pluginInfo) : parent::getFirstAvailable();
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function getParentItems($itemId)
101+
{
102+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getParentItems');
103+
return $pluginInfo ? $this->___callPlugins('getParentItems', func_get_args(), $pluginInfo) : parent::getParentItems($itemId);
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function serialize()
110+
{
111+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'serialize');
112+
return $pluginInfo ? $this->___callPlugins('serialize', func_get_args(), $pluginInfo) : parent::serialize();
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function toArray()
119+
{
120+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'toArray');
121+
return $pluginInfo ? $this->___callPlugins('toArray', func_get_args(), $pluginInfo) : parent::toArray();
122+
}
123+
124+
/**
125+
* {@inheritdoc}
126+
*/
127+
public function unserialize($serialized)
128+
{
129+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'unserialize');
130+
return $pluginInfo ? $this->___callPlugins('unserialize', func_get_args(), $pluginInfo) : parent::unserialize($serialized);
131+
}
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
public function populateFromArray(array $data)
137+
{
138+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'populateFromArray');
139+
return $pluginInfo ? $this->___callPlugins('populateFromArray', func_get_args(), $pluginInfo) : parent::populateFromArray($data);
140+
}
141+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
class ReflectionUnionTypeSample
1111
{
1212
/**
13+
* Union type attribute
14+
*
1315
* @var int|string
1416
*/
1517
private int|string $attribute;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ private function convertMethodType($type)
175175
{
176176
$returnTypeValue = $type instanceof \ReflectionNamedType ? $type->getName() : $type;
177177

178-
if ($type instanceof \ReflectionUnionType) {
178+
if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
179179
$returnTypeValue = [];
180180
foreach ($type->getTypes() as $type) {
181181
$returnTypeValue[] = $type->getName();
182182
}
183183

184-
$returnTypeValue = implode('|', $returnTypeValue);
184+
$returnTypeValue = implode(
185+
$type instanceof \ReflectionUnionType ? '|' : '&',
186+
$returnTypeValue
187+
);
185188
}
186189

187190
return $returnTypeValue;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,13 +753,16 @@ private function getTypeHintText($type)
753753
{
754754
$returnTypeValue = $type instanceof \ReflectionType ? $type->getName() : $type;
755755

756-
if ($type instanceof \ReflectionUnionType) {
756+
if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
757757
$returnTypeValue = [];
758758
foreach ($type->getTypes() as $type) {
759759
$returnTypeValue[] = $type->getName();
760760
}
761761

762-
$returnTypeValue = implode('|', $returnTypeValue);
762+
$returnTypeValue = implode(
763+
$type instanceof \ReflectionUnionType ? '|' : '&',
764+
$returnTypeValue
765+
);
763766
}
764767

765768
return $returnTypeValue;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Sample
2323
private $config = [];
2424

2525
/**
26+
* Union type attribute
27+
*
2628
* @var int|string
2729
*/
2830
private int|string $attribute;

0 commit comments

Comments
 (0)