Skip to content

Commit 75f44f5

Browse files
committed
Improve performance of InterfaceValidator
1 parent 29d83b2 commit 75f44f5

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,29 @@
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

1112
class InterfaceValidator
1213
{
13-
const METHOD_BEFORE = 'before';
14-
15-
const METHOD_AROUND = 'around';
16-
17-
const METHOD_AFTER = 'after';
14+
public const METHOD_BEFORE = 'before';
15+
public const METHOD_AROUND = 'around';
16+
public const METHOD_AFTER = 'after';
1817

1918
/**
2019
* Arguments reader model
2120
*
22-
* @var \Magento\Framework\Code\Reader\ArgumentsReader
21+
* @var ArgumentsReader
2322
*/
2423
protected $_argumentsReader;
2524

2625
/**
27-
* @param \Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader
26+
* @param ArgumentsReader $argumentsReader
2827
*/
29-
public function __construct(\Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader = null)
28+
public function __construct(ArgumentsReader $argumentsReader = null)
3029
{
31-
$this->_argumentsReader = $argumentsReader ?: new \Magento\Framework\Code\Reader\ArgumentsReader();
30+
$this->_argumentsReader = $argumentsReader ?? new ArgumentsReader();
3231
}
3332

3433
/**
@@ -50,7 +49,7 @@ public function validate($pluginClass, $interceptedType)
5049
$type = new \ReflectionClass($interceptedType);
5150

5251
foreach ($plugin->getMethods(\ReflectionMethod::IS_PUBLIC) as $pluginMethod) {
53-
/** @var $pluginMethod \ReflectionMethod */
52+
/** @var \ReflectionMethod $pluginMethod */
5453
$originMethodName = $this->getOriginMethodName($pluginMethod->getName());
5554
if ($originMethodName === null) {
5655
continue;
@@ -71,11 +70,8 @@ public function validate($pluginClass, $interceptedType)
7170
$methodType = $this->getMethodType($pluginMethod->getName());
7271

7372
$subject = array_shift($pluginMethodParameters);
74-
if (!$this->_argumentsReader->isCompatibleType(
75-
$subject['type'],
76-
$interceptedType
77-
) || $subject['type'] === null
78-
) {
73+
if ($subject['type'] === null
74+
|| !$this->_argumentsReader->isCompatibleType($subject['type'], $interceptedType)) {
7975
throw new ValidatorException(
8076
new Phrase(
8177
'Invalid [%1] $%2 type in %3::%4. It must be compatible with %5',
@@ -85,6 +81,19 @@ public function validate($pluginClass, $interceptedType)
8581
}
8682

8783
switch ($methodType) {
84+
case self::METHOD_AFTER:
85+
if (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+
}
96+
break;
8897
case self::METHOD_BEFORE:
8998
$this->validateMethodsParameters(
9099
$pluginMethodParameters,
@@ -110,19 +119,6 @@ public function validate($pluginClass, $interceptedType)
110119
$pluginMethod->getName()
111120
);
112121
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;
126122
}
127123
}
128124
}
@@ -184,13 +180,13 @@ protected function getParametersType(\ReflectionParameter $parameter)
184180
protected function getOriginMethodName($pluginMethodName)
185181
{
186182
switch ($this->getMethodType($pluginMethodName)) {
183+
case self::METHOD_AFTER:
184+
return lcfirst(substr($pluginMethodName, 5));
185+
187186
case self::METHOD_BEFORE:
188187
case self::METHOD_AROUND:
189188
return lcfirst(substr($pluginMethodName, 6));
190189

191-
case self::METHOD_AFTER:
192-
return lcfirst(substr($pluginMethodName, 5));
193-
194190
default:
195191
return null;
196192
}
@@ -205,12 +201,14 @@ protected function getOriginMethodName($pluginMethodName)
205201
*/
206202
protected function getMethodType($pluginMethodName)
207203
{
208-
if (substr($pluginMethodName, 0, 6) == self::METHOD_BEFORE) {
204+
if (0 === strpos($pluginMethodName, self::METHOD_AFTER)) {
205+
return self::METHOD_AFTER;
206+
}
207+
if (0 === strpos($pluginMethodName, self::METHOD_BEFORE)) {
209208
return self::METHOD_BEFORE;
210-
} elseif (substr($pluginMethodName, 0, 6) == self::METHOD_AROUND) {
209+
}
210+
if (0 === strpos($pluginMethodName, self::METHOD_AROUND)) {
211211
return self::METHOD_AROUND;
212-
} elseif (substr($pluginMethodName, 0, 5) == self::METHOD_AFTER) {
213-
return self::METHOD_AFTER;
214212
}
215213

216214
return null;

0 commit comments

Comments
 (0)