5
5
*/
6
6
namespace Magento \Framework \Interception \Code ;
7
7
8
+ use Magento \Framework \Code \Reader \ArgumentsReader ;
8
9
use Magento \Framework \Exception \ValidatorException ;
9
10
use Magento \Framework \Phrase ;
10
11
11
12
class InterfaceValidator
12
13
{
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 ' ;
18
17
19
18
/**
20
19
* Arguments reader model
21
20
*
22
- * @var \Magento\Framework\Code\Reader\ ArgumentsReader
21
+ * @var ArgumentsReader
23
22
*/
24
23
protected $ _argumentsReader ;
25
24
26
25
/**
27
- * @param \Magento\Framework\Code\Reader\ ArgumentsReader $argumentsReader
26
+ * @param ArgumentsReader $argumentsReader
28
27
*/
29
- public function __construct (\ Magento \ Framework \ Code \ Reader \ ArgumentsReader $ argumentsReader = null )
28
+ public function __construct (ArgumentsReader $ argumentsReader = null )
30
29
{
31
- $ this ->_argumentsReader = $ argumentsReader ?: new \ Magento \ Framework \ Code \ Reader \ ArgumentsReader ();
30
+ $ this ->_argumentsReader = $ argumentsReader ?? new ArgumentsReader ();
32
31
}
33
32
34
33
/**
@@ -50,7 +49,7 @@ public function validate($pluginClass, $interceptedType)
50
49
$ type = new \ReflectionClass ($ interceptedType );
51
50
52
51
foreach ($ plugin ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ pluginMethod ) {
53
- /** @var $pluginMethod \ReflectionMethod */
52
+ /** @var \ReflectionMethod $pluginMethod */
54
53
$ originMethodName = $ this ->getOriginMethodName ($ pluginMethod ->getName ());
55
54
if ($ originMethodName === null ) {
56
55
continue ;
@@ -71,11 +70,8 @@ public function validate($pluginClass, $interceptedType)
71
70
$ methodType = $ this ->getMethodType ($ pluginMethod ->getName ());
72
71
73
72
$ 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 )) {
79
75
throw new ValidatorException (
80
76
new Phrase (
81
77
'Invalid [%1] $%2 type in %3::%4. It must be compatible with %5 ' ,
@@ -85,6 +81,19 @@ public function validate($pluginClass, $interceptedType)
85
81
}
86
82
87
83
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 ;
88
97
case self ::METHOD_BEFORE :
89
98
$ this ->validateMethodsParameters (
90
99
$ pluginMethodParameters ,
@@ -110,19 +119,6 @@ public function validate($pluginClass, $interceptedType)
110
119
$ pluginMethod ->getName ()
111
120
);
112
121
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 ;
126
122
}
127
123
}
128
124
}
@@ -184,13 +180,13 @@ protected function getParametersType(\ReflectionParameter $parameter)
184
180
protected function getOriginMethodName ($ pluginMethodName )
185
181
{
186
182
switch ($ this ->getMethodType ($ pluginMethodName )) {
183
+ case self ::METHOD_AFTER :
184
+ return lcfirst (substr ($ pluginMethodName , 5 ));
185
+
187
186
case self ::METHOD_BEFORE :
188
187
case self ::METHOD_AROUND :
189
188
return lcfirst (substr ($ pluginMethodName , 6 ));
190
189
191
- case self ::METHOD_AFTER :
192
- return lcfirst (substr ($ pluginMethodName , 5 ));
193
-
194
190
default :
195
191
return null ;
196
192
}
@@ -205,12 +201,14 @@ protected function getOriginMethodName($pluginMethodName)
205
201
*/
206
202
protected function getMethodType ($ pluginMethodName )
207
203
{
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 )) {
209
208
return self ::METHOD_BEFORE ;
210
- } elseif (substr ($ pluginMethodName , 0 , 6 ) == self ::METHOD_AROUND ) {
209
+ }
210
+ if (0 === strpos ($ pluginMethodName , self ::METHOD_AROUND )) {
211
211
return self ::METHOD_AROUND ;
212
- } elseif (substr ($ pluginMethodName , 0 , 5 ) == self ::METHOD_AFTER ) {
213
- return self ::METHOD_AFTER ;
214
212
}
215
213
216
214
return null ;
0 commit comments