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
12
+ /**
13
+ * @SuppressWarnings(PHPMD.NPathComplexity)
14
+ */
11
15
class InterfaceValidator
12
16
{
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 ' ;
18
20
19
21
/**
20
22
* Arguments reader model
21
23
*
22
- * @var \Magento\Framework\Code\Reader\ ArgumentsReader
24
+ * @var ArgumentsReader
23
25
*/
24
26
protected $ _argumentsReader ;
25
27
26
28
/**
27
- * @param \Magento\Framework\Code\Reader\ ArgumentsReader $argumentsReader
29
+ * @param ArgumentsReader $argumentsReader
28
30
*/
29
- public function __construct (\ Magento \ Framework \ Code \ Reader \ ArgumentsReader $ argumentsReader = null )
31
+ public function __construct (ArgumentsReader $ argumentsReader = null )
30
32
{
31
- $ this ->_argumentsReader = $ argumentsReader ?: new \ Magento \ Framework \ Code \ Reader \ ArgumentsReader ();
33
+ $ this ->_argumentsReader = $ argumentsReader ?? new ArgumentsReader ();
32
34
}
33
35
34
36
/**
@@ -50,7 +52,7 @@ public function validate($pluginClass, $interceptedType)
50
52
$ type = new \ReflectionClass ($ interceptedType );
51
53
52
54
foreach ($ plugin ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ pluginMethod ) {
53
- /** @var $pluginMethod \ReflectionMethod */
55
+ /** @var \ReflectionMethod $pluginMethod */
54
56
$ originMethodName = $ this ->getOriginMethodName ($ pluginMethod ->getName ());
55
57
if ($ originMethodName === null ) {
56
58
continue ;
@@ -63,19 +65,11 @@ public function validate($pluginClass, $interceptedType)
63
65
)
64
66
);
65
67
}
66
- $ originMethod = $ type ->getMethod ($ originMethodName );
67
68
68
69
$ pluginMethodParameters = $ this ->getMethodParameters ($ pluginMethod );
69
- $ originMethodParameters = $ this ->getMethodParameters ($ originMethod );
70
-
71
- $ methodType = $ this ->getMethodType ($ pluginMethod ->getName ());
72
-
73
70
$ 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 )) {
79
73
throw new ValidatorException (
80
74
new Phrase (
81
75
'Invalid [%1] $%2 type in %3::%4. It must be compatible with %5 ' ,
@@ -84,45 +78,50 @@ public function validate($pluginClass, $interceptedType)
84
78
);
85
79
}
86
80
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
+ )
111
116
);
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 ;
126
125
}
127
126
}
128
127
}
@@ -170,8 +169,7 @@ protected function validateMethodsParameters(array $pluginParameters, array $ori
170
169
protected function getParametersType (\ReflectionParameter $ parameter )
171
170
{
172
171
$ parameterClass = $ parameter ->getClass ();
173
- $ type = $ parameterClass ? '\\' . $ parameterClass ->getName () : ($ parameter ->isArray () ? 'array ' : null );
174
- return $ type ;
172
+ return $ parameterClass ? '\\' . $ parameterClass ->getName () : ($ parameter ->isArray () ? 'array ' : null );
175
173
}
176
174
177
175
/**
@@ -183,17 +181,16 @@ protected function getParametersType(\ReflectionParameter $parameter)
183
181
*/
184
182
protected function getOriginMethodName ($ pluginMethodName )
185
183
{
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 );
190
185
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 )) ;
196
191
}
192
+
193
+ return null ;
197
194
}
198
195
199
196
/**
@@ -205,12 +202,14 @@ protected function getOriginMethodName($pluginMethodName)
205
202
*/
206
203
protected function getMethodType ($ pluginMethodName )
207
204
{
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 )) {
209
209
return self ::METHOD_BEFORE ;
210
- } elseif (substr ($ pluginMethodName , 0 , 6 ) == self ::METHOD_AROUND ) {
210
+ }
211
+ if (0 === strpos ($ pluginMethodName , self ::METHOD_AROUND )) {
211
212
return self ::METHOD_AROUND ;
212
- } elseif (substr ($ pluginMethodName , 0 , 5 ) == self ::METHOD_AFTER ) {
213
- return self ::METHOD_AFTER ;
214
213
}
215
214
216
215
return null ;
0 commit comments