11<?php
22
3+ declare (strict_types=1 );
4+
35namespace AspectMock \Core ;
46
57use AspectMock \Intercept \FunctionInjector ;
6- use Go \Aop \Aspect ;
78use AspectMock \Intercept \MethodInvocation ;
9+ use Closure ;
10+ use Go \Aop \Aspect ;
811
912class Mocker implements Aspect
1013{
1114
12- protected $ classMap = [];
13- protected $ objectMap = [];
14- protected $ funcMap = [];
15- protected $ methodMap = ['__call ' , '__callStatic ' ];
16- protected $ dynamicMethods = ['__call ' , '__callStatic ' ];
15+ protected array $ classMap = [];
16+
17+ protected array $ objectMap = [];
18+
19+ protected array $ funcMap = [];
20+
21+ protected array $ methodMap = ['__call ' , '__callStatic ' ];
22+
23+ protected array $ dynamicMethods = ['__call ' , '__callStatic ' ];
1724
1825 public function fakeMethodsAndRegisterCalls ($ class , $ declaredClass , $ method , $ params , $ static )
1926 {
2027 $ result = __AM_CONTINUE__ ;
2128 $ invocation = null ;
2229
2330 if (in_array ($ method , $ this ->methodMap )) {
24- $ invocation = new \ AspectMock \ Intercept \ MethodInvocation ();
31+ $ invocation = new MethodInvocation ();
2532 $ invocation ->setThis ($ class );
2633 $ invocation ->setMethod ($ method );
2734 $ invocation ->setArguments ($ params );
@@ -39,40 +46,42 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
3946 if (isset ($ this ->objectMap [spl_object_hash ($ class )])) {
4047 Registry::registerInstanceCall ($ class , $ method , $ params );
4148 }
49+
4250 $ class = get_class ($ class );
4351 }
4452
4553 if (isset ($ this ->classMap [$ class ])) {
4654 Registry::registerClassCall ($ class , $ method , $ params );
4755 }
56+
4857 if ($ class != $ declaredClass && isset ($ this ->classMap [$ declaredClass ])) {
4958 Registry::registerClassCall ($ declaredClass , $ method , $ params );
5059 }
5160
52- if ($ invocation instanceof \ AspectMock \ Intercept \ MethodInvocation) {
61+ if ($ invocation instanceof MethodInvocation) {
5362 $ result = $ this ->invokeFakedMethods ($ invocation );
5463 }
55-
64+
5665 return $ result ;
5766 }
5867
5968 public function fakeFunctionAndRegisterCalls ($ namespace , $ function , $ args )
6069 {
6170 $ result = __AM_CONTINUE__ ;
62- $ fullFuncName = " $ namespace\\ $ function" ;
71+ $ fullFuncName = sprintf ( ' %s\%s ' , $ namespace, $ function) ;
6372 Registry::registerFunctionCall ($ fullFuncName , $ args );
6473
6574 if (isset ($ this ->funcMap [$ fullFuncName ])) {
6675 $ func = $ this ->funcMap [$ fullFuncName ];
67- if (is_callable ($ func )) {
68- $ result = call_user_func_array ($ func , $ args );
69- } else {
70- $ result = $ func ;
71- }
76+ $ result = is_callable ($ func ) ? call_user_func_array ($ func , $ args ) : $ func ;
7277 }
78+
7379 return $ result ;
7480 }
7581
82+ /**
83+ * @return mixed
84+ */
7685 protected function invokeFakedMethods (MethodInvocation $ invocation )
7786 {
7887 $ method = $ invocation ->getMethod ();
@@ -155,6 +164,7 @@ protected function invokeFakedMethods(MethodInvocation $invocation)
155164 }
156165 }
157166 }
167+
158168 return __AM_CONTINUE__ ;
159169 }
160170
@@ -164,10 +174,12 @@ protected function getObjectMethodStubParams($obj, $method_name)
164174 if (!isset ($ this ->objectMap [$ oid ])) {
165175 return false ;
166176 }
177+
167178 $ params = $ this ->objectMap [$ oid ];
168179 if (!array_key_exists ($ method_name , $ params )) {
169180 return false ;
170181 }
182+
171183 return $ params ;
172184 }
173185
@@ -176,10 +188,12 @@ protected function getClassMethodStubParams($class_name, $method_name)
176188 if (!isset ($ this ->classMap [$ class_name ])) {
177189 return false ;
178190 }
191+
179192 $ params = $ this ->classMap [$ class_name ];
180193 if (!array_key_exists ($ method_name , $ params )) {
181194 return false ;
182195 }
196+
183197 return $ params ;
184198 }
185199
@@ -192,14 +206,15 @@ protected function stub(MethodInvocation $invocation, $params)
192206 $ replacedMethod = $ this ->turnToClosure ($ replacedMethod );
193207
194208 if ($ invocation ->isStatic ()) {
195- $ replacedMethod = \ Closure::bind ($ replacedMethod , null , $ invocation ->getThis ());
209+ $ replacedMethod = Closure::bind ($ replacedMethod , null , $ invocation ->getThis ());
196210 } else {
197211 $ replacedMethod = $ replacedMethod ->bindTo ($ invocation ->getThis (), get_class ($ invocation ->getThis ()));
198212 }
213+
199214 return call_user_func_array ($ replacedMethod , $ invocation ->getArguments ());
200215 }
201216
202- protected function stubMagicMethod (MethodInvocation $ invocation , $ params )
217+ protected function stubMagicMethod (MethodInvocation $ invocation , array $ params )
203218 {
204219 $ args = $ invocation ->getArguments ();
205220 $ name = array_shift ($ args );
@@ -208,56 +223,62 @@ protected function stubMagicMethod(MethodInvocation $invocation, $params)
208223 $ replacedMethod = $ this ->turnToClosure ($ replacedMethod );
209224
210225 if ($ invocation ->isStatic ()) {
211- \ Closure::bind ($ replacedMethod , null , $ invocation ->getThis ());
226+ Closure::bind ($ replacedMethod , null , $ invocation ->getThis ());
212227 } else {
213228 $ replacedMethod = $ replacedMethod ->bindTo ($ invocation ->getThis (), get_class ($ invocation ->getThis ()));
214229 }
230+
215231 return call_user_func_array ($ replacedMethod , $ args );
216232 }
217233
218234
219- protected function turnToClosure ($ returnValue )
235+ protected function turnToClosure ($ returnValue ): Closure
220236 {
221- if ($ returnValue instanceof \ Closure) {
237+ if ($ returnValue instanceof Closure) {
222238 return $ returnValue ;
223239 }
224- return function () use ($ returnValue ) {
225- return $ returnValue ;
226- };
240+
241+ return fn () => $ returnValue ;
227242 }
228243
229- public function registerClass ($ class , $ params = [])
244+ public function registerClass (string $ class , array $ params = []): void
230245 {
231246 $ class = ltrim ($ class , '\\' );
232247 if (isset ($ this ->classMap [$ class ])) {
233248 $ params = array_merge ($ this ->classMap [$ class ], $ params );
234249 }
250+
235251 $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
236252 $ this ->classMap [$ class ] = $ params ;
237253 }
238254
239- public function registerObject ($ object , $ params = [])
255+ public function registerObject (object $ object , array $ params = []): void
240256 {
241257 $ hash = spl_object_hash ($ object );
242258 if (isset ($ this ->objectMap [$ hash ])) {
243259 $ params = array_merge ($ this ->objectMap [$ hash ], $ params );
244260 }
261+
245262 $ this ->objectMap [$ hash ] = $ params ;
246263 $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
247264 }
248265
249- public function registerFunc ($ namespace , $ func , $ body )
266+ /**
267+ * @param string|Closure $func
268+ */
269+ public function registerFunc (string $ namespace , $ func , $ body ): void
250270 {
251271 $ namespace = ltrim ($ namespace , '\\' );
252- if (!function_exists ("$ namespace\\ $ func " )) {
272+ if (!function_exists ("{ $ namespace}\\{ $ func} " )) {
253273 $ injector = new FunctionInjector ($ namespace , $ func );
254274 $ injector ->save ();
255275 $ injector ->inject ();
256276 }
257- $ this ->funcMap ["$ namespace \\$ func " ] = $ body ;
277+
278+ $ this ->funcMap ["{$ namespace }\\{$ func }" ] = $ body ;
258279 }
259280
260- public function clean ($ objectOrClass = null )
281+ public function clean ($ objectOrClass = null ): void
261282 {
262283 if (!$ objectOrClass ) {
263284 $ this ->classMap = [];
@@ -270,5 +291,4 @@ public function clean($objectOrClass = null)
270291 unset($ this ->classMap [$ objectOrClass ]);
271292 }
272293 }
273-
274294}
0 commit comments