Skip to content

Commit 6aa0925

Browse files
authored
ENGCOM-7482: Improve performance of InterfaceValidator #27903
2 parents f0b96a0 + b6e71ff commit 6aa0925

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55
*/
66
namespace Magento\Framework\Interception\Code;
77

8+
use Magento\Framework\Code\Reader\ArgumentsReader;
89
use Magento\Framework\Exception\ValidatorException;
910
use Magento\Framework\Phrase;
1011

12+
/**
13+
* @SuppressWarnings(PHPMD.NPathComplexity)
14+
*/
1115
class InterfaceValidator
1216
{
13-
const METHOD_BEFORE = 'before';
14-
15-
const METHOD_AROUND = 'around';
16-
17-
const METHOD_AFTER = 'after';
17+
public const METHOD_BEFORE = 'before';
18+
public const METHOD_AROUND = 'around';
19+
public const METHOD_AFTER = 'after';
1820

1921
/**
2022
* Arguments reader model
2123
*
22-
* @var \Magento\Framework\Code\Reader\ArgumentsReader
24+
* @var ArgumentsReader
2325
*/
2426
protected $_argumentsReader;
2527

2628
/**
27-
* @param \Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader
29+
* @param ArgumentsReader $argumentsReader
2830
*/
29-
public function __construct(\Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader = null)
31+
public function __construct(ArgumentsReader $argumentsReader = null)
3032
{
31-
$this->_argumentsReader = $argumentsReader ?: new \Magento\Framework\Code\Reader\ArgumentsReader();
33+
$this->_argumentsReader = $argumentsReader ?? new ArgumentsReader();
3234
}
3335

3436
/**
@@ -50,7 +52,7 @@ public function validate($pluginClass, $interceptedType)
5052
$type = new \ReflectionClass($interceptedType);
5153

5254
foreach ($plugin->getMethods(\ReflectionMethod::IS_PUBLIC) as $pluginMethod) {
53-
/** @var $pluginMethod \ReflectionMethod */
55+
/** @var \ReflectionMethod $pluginMethod */
5456
$originMethodName = $this->getOriginMethodName($pluginMethod->getName());
5557
if ($originMethodName === null) {
5658
continue;
@@ -63,19 +65,11 @@ public function validate($pluginClass, $interceptedType)
6365
)
6466
);
6567
}
66-
$originMethod = $type->getMethod($originMethodName);
6768

6869
$pluginMethodParameters = $this->getMethodParameters($pluginMethod);
69-
$originMethodParameters = $this->getMethodParameters($originMethod);
70-
71-
$methodType = $this->getMethodType($pluginMethod->getName());
72-
7370
$subject = array_shift($pluginMethodParameters);
74-
if (!$this->_argumentsReader->isCompatibleType(
75-
$subject['type'],
76-
$interceptedType
77-
) || $subject['type'] === null
78-
) {
71+
if ($subject['type'] === null
72+
|| !$this->_argumentsReader->isCompatibleType($subject['type'], $interceptedType)) {
7973
throw new ValidatorException(
8074
new Phrase(
8175
'Invalid [%1] $%2 type in %3::%4. It must be compatible with %5',
@@ -84,45 +78,50 @@ public function validate($pluginClass, $interceptedType)
8478
);
8579
}
8680

87-
switch ($methodType) {
88-
case self::METHOD_BEFORE:
89-
$this->validateMethodsParameters(
90-
$pluginMethodParameters,
91-
$originMethodParameters,
92-
$pluginClass,
93-
$pluginMethod->getName()
94-
);
95-
break;
96-
case self::METHOD_AROUND:
97-
$proceed = array_shift($pluginMethodParameters);
98-
if (!$this->_argumentsReader->isCompatibleType($proceed['type'], '\\Closure')) {
99-
throw new ValidatorException(
100-
new Phrase(
101-
'Invalid [%1] $%2 type in %3::%4. It must be compatible with \\Closure',
102-
[$proceed['type'], $proceed['name'], $pluginClass, $pluginMethod->getName()]
103-
)
104-
);
105-
}
106-
$this->validateMethodsParameters(
107-
$pluginMethodParameters,
108-
$originMethodParameters,
109-
$pluginClass,
110-
$pluginMethod->getName()
81+
$originMethod = $type->getMethod($originMethodName);
82+
$originMethodParameters = $this->getMethodParameters($originMethod);
83+
$methodType = $this->getMethodType($pluginMethod->getName());
84+
85+
if (self::METHOD_AFTER === $methodType && count($pluginMethodParameters) > 1) {
86+
// remove result
87+
array_shift($pluginMethodParameters);
88+
$matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters);
89+
$this->validateMethodsParameters(
90+
$pluginMethodParameters,
91+
$matchedParameters,
92+
$pluginClass,
93+
$pluginMethod->getName()
94+
);
95+
continue;
96+
}
97+
98+
if (self::METHOD_BEFORE === $methodType) {
99+
$this->validateMethodsParameters(
100+
$pluginMethodParameters,
101+
$originMethodParameters,
102+
$pluginClass,
103+
$pluginMethod->getName()
104+
);
105+
continue;
106+
}
107+
108+
if (self::METHOD_AROUND === $methodType) {
109+
$proceed = array_shift($pluginMethodParameters);
110+
if (!$this->_argumentsReader->isCompatibleType($proceed['type'], '\\Closure')) {
111+
throw new ValidatorException(
112+
new Phrase(
113+
'Invalid [%1] $%2 type in %3::%4. It must be compatible with \\Closure',
114+
[$proceed['type'], $proceed['name'], $pluginClass, $pluginMethod->getName()]
115+
)
111116
);
112-
break;
113-
case self::METHOD_AFTER:
114-
if (count($pluginMethodParameters) > 1) {
115-
// remove result
116-
array_shift($pluginMethodParameters);
117-
$matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters);
118-
$this->validateMethodsParameters(
119-
$pluginMethodParameters,
120-
$matchedParameters,
121-
$pluginClass,
122-
$pluginMethod->getName()
123-
);
124-
}
125-
break;
117+
}
118+
$this->validateMethodsParameters(
119+
$pluginMethodParameters,
120+
$originMethodParameters,
121+
$pluginClass,
122+
$pluginMethod->getName()
123+
);
124+
continue;
126125
}
127126
}
128127
}
@@ -170,8 +169,7 @@ protected function validateMethodsParameters(array $pluginParameters, array $ori
170169
protected function getParametersType(\ReflectionParameter $parameter)
171170
{
172171
$parameterClass = $parameter->getClass();
173-
$type = $parameterClass ? '\\' . $parameterClass->getName() : ($parameter->isArray() ? 'array' : null);
174-
return $type;
172+
return $parameterClass ? '\\' . $parameterClass->getName() : ($parameter->isArray() ? 'array' : null);
175173
}
176174

177175
/**
@@ -183,17 +181,16 @@ protected function getParametersType(\ReflectionParameter $parameter)
183181
*/
184182
protected function getOriginMethodName($pluginMethodName)
185183
{
186-
switch ($this->getMethodType($pluginMethodName)) {
187-
case self::METHOD_BEFORE:
188-
case self::METHOD_AROUND:
189-
return lcfirst(substr($pluginMethodName, 6));
184+
$methodType = $this->getMethodType($pluginMethodName);
190185

191-
case self::METHOD_AFTER:
192-
return lcfirst(substr($pluginMethodName, 5));
193-
194-
default:
195-
return null;
186+
if (self::METHOD_AFTER === $methodType) {
187+
return lcfirst(substr($pluginMethodName, 5));
188+
}
189+
if (self::METHOD_BEFORE === $methodType || self::METHOD_AROUND === $methodType) {
190+
return lcfirst(substr($pluginMethodName, 6));
196191
}
192+
193+
return null;
197194
}
198195

199196
/**
@@ -205,12 +202,14 @@ protected function getOriginMethodName($pluginMethodName)
205202
*/
206203
protected function getMethodType($pluginMethodName)
207204
{
208-
if (substr($pluginMethodName, 0, 6) == self::METHOD_BEFORE) {
205+
if (0 === strpos($pluginMethodName, self::METHOD_AFTER)) {
206+
return self::METHOD_AFTER;
207+
}
208+
if (0 === strpos($pluginMethodName, self::METHOD_BEFORE)) {
209209
return self::METHOD_BEFORE;
210-
} elseif (substr($pluginMethodName, 0, 6) == self::METHOD_AROUND) {
210+
}
211+
if (0 === strpos($pluginMethodName, self::METHOD_AROUND)) {
211212
return self::METHOD_AROUND;
212-
} elseif (substr($pluginMethodName, 0, 5) == self::METHOD_AFTER) {
213-
return self::METHOD_AFTER;
214213
}
215214

216215
return null;

0 commit comments

Comments
 (0)