11<?php
2+
23namespace AspectMock \Core ;
4+
35use AspectMock \Intercept \FunctionInjector ;
46use Go \Aop \Aspect ;
57use AspectMock \Intercept \MethodInvocation ;
68
7- class Mocker implements Aspect {
9+ class Mocker implements Aspect
10+ {
811
9- protected $ classMap = [];
10- protected $ objectMap = [];
11- protected $ funcMap = [];
12- protected $ methodMap = ['__call ' , '__callStatic ' ];
12+ protected $ classMap = [];
13+ protected $ objectMap = [];
14+ protected $ funcMap = [];
15+ protected $ methodMap = ['__call ' , '__callStatic ' ];
1316 protected $ dynamicMethods = ['__call ' , '__callStatic ' ];
1417
1518 public function fakeMethodsAndRegisterCalls ($ class , $ declaredClass , $ method , $ params , $ static )
1619 {
17- // $method = $invocation->getMethod();
18- // $obj = $invocation->getThis();
1920 $ result = __AM_CONTINUE__ ;
2021
2122 if (in_array ($ method , $ this ->methodMap )) {
@@ -35,19 +36,25 @@ public function fakeMethodsAndRegisterCalls($class, $declaredClass, $method, $pa
3536 }
3637
3738 if (!$ static ) {
38- if (isset ($ this ->objectMap [spl_object_hash ($ class )])) Registry::registerInstanceCall ($ class , $ method , $ params );
39+ if (isset ($ this ->objectMap [spl_object_hash ($ class )])) {
40+ Registry::registerInstanceCall ($ class , $ method , $ params );
41+ }
3942 $ class = get_class ($ class );
4043 }
4144
42- if (isset ($ this ->classMap [$ class ])) Registry::registerClassCall ($ class , $ method , $ params );
43- if ($ class != $ declaredClass && isset ($ this ->classMap [$ declaredClass ])) Registry::registerClassCall ($ declaredClass , $ method , $ params );
45+ if (isset ($ this ->classMap [$ class ])) {
46+ Registry::registerClassCall ($ class , $ method , $ params );
47+ }
48+ if ($ class != $ declaredClass && isset ($ this ->classMap [$ declaredClass ])) {
49+ Registry::registerClassCall ($ declaredClass , $ method , $ params );
50+ }
4451
4552 return $ result ;
4653 }
4754
4855 public function fakeFunctionAndRegisterCalls ($ namespace , $ function , $ args )
4956 {
50- $ result = __AM_CONTINUE__ ;
57+ $ result = __AM_CONTINUE__ ;
5158 $ fullFuncName = "$ namespace \\$ function " ;
5259 Registry::registerFunctionCall ($ fullFuncName , $ args );
5360
@@ -65,57 +72,83 @@ public function fakeFunctionAndRegisterCalls($namespace, $function, $args)
6572 protected function invokeFakedMethods (MethodInvocation $ invocation )
6673 {
6774 $ method = $ invocation ->getMethod ();
68- if (!in_array ($ method , $ this ->methodMap )) return __AM_CONTINUE__ ;
75+ if (!in_array ($ method , $ this ->methodMap )) {
76+ return __AM_CONTINUE__ ;
77+ }
6978
7079 $ obj = $ invocation ->getThis ();
7180
7281 if (is_object ($ obj )) {
7382 // instance method
7483 $ params = $ this ->getObjectMethodStubParams ($ obj , $ method );
75- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
84+ if ($ params !== false ) {
85+ return $ this ->stub ($ invocation , $ params );
86+ }
7687
7788 // class method
7889 $ params = $ this ->getClassMethodStubParams (get_class ($ obj ), $ method );
79- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
90+ if ($ params !== false ) {
91+ return $ this ->stub ($ invocation , $ params );
92+ }
8093
8194 // inheritance
8295 $ params = $ this ->getClassMethodStubParams ($ invocation ->getDeclaredClass (), $ method );
83- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
96+ if ($ params !== false ) {
97+ return $ this ->stub ($ invocation , $ params );
98+ }
8499
85100 // magic methods
86101 if ($ method == '__call ' ) {
87- $ args = $ invocation ->getArguments ();
102+ $ args = $ invocation ->getArguments ();
88103 $ method = array_shift ($ args );
89104
90105 $ params = $ this ->getObjectMethodStubParams ($ obj , $ method );
91- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
106+ if ($ params !== false ) {
107+ return $ this ->stubMagicMethod ($ invocation , $ params );
108+ }
92109
93110 // magic class method
94111 $ params = $ this ->getClassMethodStubParams (get_class ($ obj ), $ method );
95- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
112+ if ($ params !== false ) {
113+ return $ this ->stubMagicMethod ($ invocation , $ params );
114+ }
96115
97116 // inheritance
98117 $ calledClass = $ invocation ->getDeclaredClass ();
99- $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
100- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
118+ $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
119+ if ($ params !== false ) {
120+ return $ this ->stubMagicMethod ($ invocation , $ params );
121+ }
101122 }
102123 } else {
103124 // static method
104125 $ params = $ this ->getClassMethodStubParams ($ obj , $ method );
105- if ($ params !== false ) return $ this ->stub ($ invocation , $ params );
126+ if ($ params !== false ) {
127+ return $ this ->stub ($ invocation , $ params );
128+ }
129+
130+ // inheritance
131+ $ params = $ this ->getClassMethodStubParams ($ invocation ->getDeclaredClass (), $ method );
132+ if ($ params !== false ) {
133+ return $ this ->stub ($ invocation , $ params );
134+ }
106135
107136 // magic static method (facade)
108137 if ($ method == '__callStatic ' ) {
109- $ args = $ invocation ->getArguments ();
138+ $ args = $ invocation ->getArguments ();
110139 $ method = array_shift ($ args );
111140
112141 $ params = $ this ->getClassMethodStubParams ($ obj , $ method );
113- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
142+ if ($ params !== false ) {
143+ return $ this ->stubMagicMethod ($ invocation , $ params );
144+ }
114145
115146 // inheritance
116147 $ calledClass = $ invocation ->getDeclaredClass ();
117- $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
118- if ($ params !== false ) return $ this ->stubMagicMethod ($ invocation , $ params );
148+ $ params = $ this ->getClassMethodStubParams ($ calledClass , $ method );
149+ if ($ params !== false ) {
150+ return $ this ->stubMagicMethod ($ invocation , $ params );
151+ }
119152 }
120153 }
121154 return __AM_CONTINUE__ ;
@@ -124,17 +157,25 @@ protected function invokeFakedMethods(MethodInvocation $invocation)
124157 protected function getObjectMethodStubParams ($ obj , $ method_name )
125158 {
126159 $ oid = spl_object_hash ($ obj );
127- if (!isset ($ this ->objectMap [$ oid ])) return false ;
160+ if (!isset ($ this ->objectMap [$ oid ])) {
161+ return false ;
162+ }
128163 $ params = $ this ->objectMap [$ oid ];
129- if (!array_key_exists ($ method_name ,$ params )) return false ;
164+ if (!array_key_exists ($ method_name , $ params )) {
165+ return false ;
166+ }
130167 return $ params ;
131168 }
132169
133170 protected function getClassMethodStubParams ($ class_name , $ method_name )
134171 {
135- if (!isset ($ this ->classMap [$ class_name ])) return false ;
172+ if (!isset ($ this ->classMap [$ class_name ])) {
173+ return false ;
174+ }
136175 $ params = $ this ->classMap [$ class_name ];
137- if (!array_key_exists ($ method_name ,$ params )) return false ;
176+ if (!array_key_exists ($ method_name , $ params )) {
177+ return false ;
178+ }
138179 return $ params ;
139180 }
140181
@@ -173,35 +214,37 @@ protected function stubMagicMethod(MethodInvocation $invocation, $params)
173214
174215 protected function turnToClosure ($ returnValue )
175216 {
176- if ($ returnValue instanceof \Closure) return $ returnValue ;
177- return function () use ($ returnValue ) {
217+ if ($ returnValue instanceof \Closure) {
218+ return $ returnValue ;
219+ }
220+ return function () use ($ returnValue ) {
178221 return $ returnValue ;
179222 };
180223 }
181224
182- public function registerClass ($ class , $ params = array () )
225+ public function registerClass ($ class , $ params = [] )
183226 {
184- $ class = ltrim ($ class ,'\\' );
227+ $ class = ltrim ($ class , '\\' );
185228 if (isset ($ this ->classMap [$ class ])) {
186229 $ params = array_merge ($ this ->classMap [$ class ], $ params );
187230 }
188- $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
231+ $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
189232 $ this ->classMap [$ class ] = $ params ;
190233 }
191234
192- public function registerObject ($ object , $ params = array () )
235+ public function registerObject ($ object , $ params = [] )
193236 {
194237 $ hash = spl_object_hash ($ object );
195238 if (isset ($ this ->objectMap [$ hash ])) {
196239 $ params = array_merge ($ this ->objectMap [$ hash ], $ params );
197240 }
198241 $ this ->objectMap [$ hash ] = $ params ;
199- $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
242+ $ this ->methodMap = array_merge ($ this ->methodMap , array_keys ($ params ));
200243 }
201244
202245 public function registerFunc ($ namespace , $ func , $ body )
203246 {
204- $ namespace = ltrim ($ namespace ,'\\' );
247+ $ namespace = ltrim ($ namespace , '\\' );
205248 if (!function_exists ("$ namespace \\$ func " )) {
206249 $ injector = new FunctionInjector ($ namespace , $ func );
207250 $ injector ->save ();
@@ -213,10 +256,10 @@ public function registerFunc($namespace, $func, $body)
213256 public function clean ($ objectOrClass = null )
214257 {
215258 if (!$ objectOrClass ) {
216- $ this ->classMap = [];
259+ $ this ->classMap = [];
217260 $ this ->objectMap = [];
218- $ this ->methodMap = ['__call ' ,'__callStatic ' ];
219- $ this ->funcMap = [];
261+ $ this ->methodMap = ['__call ' , '__callStatic ' ];
262+ $ this ->funcMap = [];
220263 } elseif (is_object ($ objectOrClass )) {
221264 unset($ this ->objectMap [spl_object_hash ($ objectOrClass )]);
222265 } else {
0 commit comments