Skip to content

Commit a76bcb6

Browse files
committed
Merge branch '31600-deprecated-reflection-method' of github.com:ProkopovVitaliy/magento2 into php8-develop
2 parents ca16be7 + 79e89c8 commit a76bcb6

File tree

10 files changed

+250
-48
lines changed

10 files changed

+250
-48
lines changed

dev/tests/static/framework/Magento/CodeMessDetector/Rule/Design/CookieAndSessionMisuse.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88

99
namespace Magento\CodeMessDetector\Rule\Design;
1010

11+
use Magento\Framework\Session\SessionManagerInterface;
12+
use Magento\Framework\Stdlib\Cookie\CookieReaderInterface;
1113
use PDepend\Source\AST\ASTClass;
1214
use PHPMD\AbstractNode;
1315
use PHPMD\AbstractRule;
1416
use PHPMD\Node\ClassNode;
1517
use PHPMD\Rule\ClassAware;
18+
use ReflectionClass;
19+
use ReflectionException;
20+
use ReflectionParameter;
1621

1722
/**
1823
* Session and Cookies must be used only in HTML Presentation layer.
@@ -105,7 +110,7 @@ private function isControllerPlugin(\ReflectionClass $class): bool
105110
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
106111
if (preg_match('/^(after|around|before).+/i', $method->getName())) {
107112
try {
108-
$argument = $method->getParameters()[0]->getClass();
113+
$argument = $this->getParameterClass($method->getParameters()[0]);
109114
} catch (\Throwable $exception) {
110115
//Non-existing class (autogenerated perhaps) or doesn't have an argument.
111116
continue;
@@ -134,7 +139,7 @@ private function isBlockPlugin(\ReflectionClass $class): bool
134139
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
135140
if (preg_match('/^(after|around|before).+/i', $method->getName())) {
136141
try {
137-
$argument = $method->getParameters()[0]->getClass();
142+
$argument = $this->getParameterClass($method->getParameters()[0]);
138143
} catch (\Throwable $exception) {
139144
//Non-existing class (autogenerated perhaps) or doesn't have an argument.
140145
continue;
@@ -164,14 +169,16 @@ private function doesUseRestrictedClasses(\ReflectionClass $class): bool
164169
if ($constructor) {
165170
foreach ($constructor->getParameters() as $argument) {
166171
try {
167-
if ($class = $argument->getClass()) {
168-
if ($class->isSubclassOf(\Magento\Framework\Session\SessionManagerInterface::class)
169-
|| $class->getName() === \Magento\Framework\Session\SessionManagerInterface::class
170-
|| $class->isSubclassOf(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class)
171-
|| $class->getName() === \Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class
172-
) {
173-
return true;
174-
}
172+
$class = $this->getParameterClass($argument);
173+
if ($class === null) {
174+
continue;
175+
}
176+
if ($class->isSubclassOf(SessionManagerInterface::class)
177+
|| $class->getName() === SessionManagerInterface::class
178+
|| $class->isSubclassOf(CookieReaderInterface::class)
179+
|| $class->getName() === CookieReaderInterface::class
180+
) {
181+
return true;
175182
}
176183
} catch (\ReflectionException $exception) {
177184
//Failed to load the argument's class information
@@ -183,6 +190,22 @@ private function doesUseRestrictedClasses(\ReflectionClass $class): bool
183190
return false;
184191
}
185192

193+
/**
194+
* Get class by reflection parameter
195+
*
196+
* @param ReflectionParameter $reflectionParameter
197+
* @return ReflectionClass|null
198+
* @throws ReflectionException
199+
*/
200+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
201+
{
202+
$parameterType = $reflectionParameter->getType();
203+
204+
return $parameterType && !$parameterType->isBuiltin()
205+
? new ReflectionClass($parameterType->getName())
206+
: null;
207+
}
208+
186209
/**
187210
* @inheritdoc
188211
*

dev/tests/static/framework/Magento/TestFramework/Integrity/Library/Injectable.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Laminas\Code\Reflection\ClassReflection;
99
use Laminas\Code\Reflection\FileReflection;
1010
use Laminas\Code\Reflection\ParameterReflection;
11+
use ReflectionClass;
12+
use ReflectionException;
13+
use ReflectionParameter;
1114

1215
/**
1316
* Provide dependencies for the file
@@ -39,7 +42,7 @@ public function getDependencies(FileReflection $fileReflection)
3942
foreach ($method->getParameters() as $parameter) {
4043
try {
4144
/** @var ParameterReflection $parameter */
42-
$dependency = $parameter->getClass();
45+
$dependency = $this->getParameterClass($parameter);
4346
if ($dependency instanceof ClassReflection) {
4447
$this->dependencies[] = $dependency->getName();
4548
}
@@ -56,4 +59,20 @@ public function getDependencies(FileReflection $fileReflection)
5659

5760
return $this->dependencies;
5861
}
62+
63+
/**
64+
* Get class by reflection parameter
65+
*
66+
* @param ReflectionParameter $reflectionParameter
67+
* @return ReflectionClass|null
68+
* @throws ReflectionException
69+
*/
70+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
71+
{
72+
$parameterType = $reflectionParameter->getType();
73+
74+
return $parameterType && !$parameterType->isBuiltin()
75+
? new ReflectionClass($parameterType->getName())
76+
: null;
77+
}
5978
}

dev/tests/static/testsuite/Magento/Test/Integrity/PublicCodeTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\Test\Integrity;
77

88
use Magento\Framework\App\Utility\Files;
9+
use ReflectionClass;
10+
use ReflectionException;
11+
use ReflectionParameter;
912

1013
/**
1114
* Tests @api annotated code integrity
@@ -277,7 +280,7 @@ private function checkParameters($class, \ReflectionMethod $method, array $nonPu
277280
&& !$parameter->getType()->isBuiltin()
278281
&& !$this->isGenerated($parameter->getType()->getName())
279282
) {
280-
$parameterClass = $parameter->getClass();
283+
$parameterClass = $this->getParameterClass($parameter);
281284
/*
282285
* We don't want to check integrity of @api coverage of classes
283286
* that belong to different vendors, because it is too complicated.
@@ -286,7 +289,7 @@ private function checkParameters($class, \ReflectionMethod $method, array $nonPu
286289
* we don't want to fail test, because Zend is considered public by default,
287290
* and we don't care if Zend classes are @api-annotated
288291
*/
289-
if (!$parameterClass->isInternal()
292+
if ($parameterClass && !$parameterClass->isInternal()
290293
&& $this->areClassesFromSameVendor($parameterClass->getName(), $class)
291294
&& !$this->isPublished($parameterClass)
292295
) {
@@ -296,4 +299,20 @@ private function checkParameters($class, \ReflectionMethod $method, array $nonPu
296299
}
297300
return $nonPublishedClasses;
298301
}
302+
303+
/**
304+
* Get class by reflection parameter
305+
*
306+
* @param ReflectionParameter $reflectionParameter
307+
* @return ReflectionClass|null
308+
* @throws ReflectionException
309+
*/
310+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
311+
{
312+
$parameterType = $reflectionParameter->getType();
313+
314+
return $parameterType && !$parameterType->isBuiltin()
315+
? new ReflectionClass($parameterType->getName())
316+
: null;
317+
}
299318
}

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\Framework\Code\Generator;
77

88
use Laminas\Code\Generator\ValueGenerator;
9+
use ReflectionClass;
10+
use ReflectionException;
11+
use ReflectionParameter;
912

1013
/**
1114
* Abstract entity
@@ -323,29 +326,46 @@ protected function _getNullDefaultValue()
323326
private function extractParameterType(
324327
\ReflectionParameter $parameter
325328
): ?string {
329+
if (!$parameter->hasType()) {
330+
return null;
331+
}
332+
326333
/** @var string|null $typeName */
327334
$typeName = null;
328-
if ($parameter->hasType()) {
329-
if ($parameter->isArray()) {
330-
$typeName = 'array';
331-
} elseif ($parameter->getClass()) {
332-
$typeName = $this->_getFullyQualifiedClassName(
333-
$parameter->getClass()->getName()
334-
);
335-
} elseif ($parameter->isCallable()) {
336-
$typeName = 'callable';
337-
} else {
338-
$typeName = $parameter->getType()->getName();
339-
}
340335

341-
if ($parameter->allowsNull()) {
342-
$typeName = '?' .$typeName;
343-
}
336+
if ($parameter->isArray()) {
337+
$typeName = 'array';
338+
} elseif ($parameterClass = $this->getParameterClass($parameter)) {
339+
$typeName = $this->_getFullyQualifiedClassName($parameterClass->getName());
340+
} elseif ($parameter->isCallable()) {
341+
$typeName = 'callable';
342+
} else {
343+
$typeName = $parameter->getType()->getName();
344+
}
345+
346+
if ($parameter->allowsNull()) {
347+
$typeName = '?' . $typeName;
344348
}
345349

346350
return $typeName;
347351
}
348352

353+
/**
354+
* Get class by reflection parameter
355+
*
356+
* @param ReflectionParameter $reflectionParameter
357+
* @return ReflectionClass|null
358+
* @throws ReflectionException
359+
*/
360+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
361+
{
362+
$parameterType = $reflectionParameter->getType();
363+
364+
return $parameterType && !$parameterType->isBuiltin()
365+
? new ReflectionClass($parameterType->getName())
366+
: null;
367+
}
368+
349369
/**
350370
* Extract parameter default value
351371
*

lib/internal/Magento/Framework/Code/Reader/ArgumentsReader.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Framework\Code\Reader;
77

8+
use ReflectionClass;
9+
use ReflectionException;
10+
use ReflectionParameter;
11+
812
/**
913
* The class arguments reader
1014
*/
@@ -98,11 +102,13 @@ public function getConstructorArguments(\ReflectionClass $class, $groupByPositio
98102
*/
99103
private function processType(\ReflectionClass $class, \Laminas\Code\Reflection\ParameterReflection $parameter)
100104
{
101-
if ($parameter->getClass()) {
102-
return NamespaceResolver::NS_SEPARATOR . $parameter->getClass()->getName();
105+
$parameterClass = $this->getParameterClass($parameter);
106+
107+
if ($parameterClass) {
108+
return NamespaceResolver::NS_SEPARATOR . $parameterClass->getName();
103109
}
104110

105-
$type = $parameter->detectType();
111+
$type = $parameter->detectType();
106112

107113
if ($type === 'null') {
108114
return null;
@@ -121,6 +127,22 @@ private function processType(\ReflectionClass $class, \Laminas\Code\Reflection\P
121127
return $type;
122128
}
123129

130+
/**
131+
* Get class by reflection parameter
132+
*
133+
* @param ReflectionParameter $reflectionParameter
134+
* @return ReflectionClass|null
135+
* @throws ReflectionException
136+
*/
137+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
138+
{
139+
$parameterType = $reflectionParameter->getType();
140+
141+
return $parameterType && !$parameterType->isBuiltin()
142+
? new ReflectionClass($parameterType->getName())
143+
: null;
144+
}
145+
124146
/**
125147
* Get arguments of parent __construct call
126148
*

lib/internal/Magento/Framework/Code/Reader/ClassReader.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Framework\Code\Reader;
77

8+
use ReflectionClass;
9+
use ReflectionException;
10+
use ReflectionParameter;
11+
812
/**
913
* Class ClassReader
1014
*/
@@ -17,46 +21,64 @@ class ClassReader implements ClassReaderInterface
1721
*
1822
* @param string $className
1923
* @return array|null
20-
* @throws \ReflectionException
24+
* @throws ReflectionException
2125
*/
2226
public function getConstructor($className)
2327
{
24-
$class = new \ReflectionClass($className);
28+
$class = new ReflectionClass($className);
2529
$result = null;
2630
$constructor = $class->getConstructor();
2731
if ($constructor) {
2832
$result = [];
29-
/** @var $parameter \ReflectionParameter */
33+
/** @var $parameter ReflectionParameter */
3034
foreach ($constructor->getParameters() as $parameter) {
3135
try {
36+
$parameterClass = $this->getParameterClass($parameter);
37+
3238
$result[] = [
3339
$parameter->getName(),
34-
$parameter->getClass() !== null ? $parameter->getClass()->getName() : null,
40+
$parameterClass ? $parameterClass->getName() : null,
3541
!$parameter->isOptional() && !$parameter->isDefaultValueAvailable(),
3642
$this->getReflectionParameterDefaultValue($parameter),
3743
$parameter->isVariadic(),
3844
];
39-
} catch (\ReflectionException $e) {
45+
} catch (ReflectionException $e) {
4046
$message = sprintf(
4147
'Impossible to process constructor argument %s of %s class',
4248
$parameter->__toString(),
4349
$className
4450
);
45-
throw new \ReflectionException($message, 0, $e);
51+
throw new ReflectionException($message, 0, $e);
4652
}
4753
}
4854
}
4955

5056
return $result;
5157
}
5258

59+
/**
60+
* Get class by reflection parameter
61+
*
62+
* @param ReflectionParameter $reflectionParameter
63+
* @return ReflectionClass|null
64+
* @throws ReflectionException
65+
*/
66+
private function getParameterClass(ReflectionParameter $reflectionParameter): ?ReflectionClass
67+
{
68+
$parameterType = $reflectionParameter->getType();
69+
70+
return $parameterType && !$parameterType->isBuiltin()
71+
? new ReflectionClass($parameterType->getName())
72+
: null;
73+
}
74+
5375
/**
5476
* Get reflection parameter default value
5577
*
56-
* @param \ReflectionParameter $parameter
78+
* @param ReflectionParameter $parameter
5779
* @return array|mixed|null
5880
*/
59-
private function getReflectionParameterDefaultValue(\ReflectionParameter $parameter)
81+
private function getReflectionParameterDefaultValue(ReflectionParameter $parameter)
6082
{
6183
if ($parameter->isVariadic()) {
6284
return [];

0 commit comments

Comments
 (0)