Complex classes like Mockery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mockery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Mockery |
||
32 | { |
||
33 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
34 | |||
35 | /** |
||
36 | * Global container to hold all mocks for the current unit test running. |
||
37 | * |
||
38 | * @var \Mockery\Container |
||
39 | */ |
||
40 | protected static $_container = null; |
||
41 | |||
42 | /** |
||
43 | * Global configuration handler containing configuration options. |
||
44 | * |
||
45 | * @var \Mockery\Configuration |
||
46 | */ |
||
47 | protected static $_config = null; |
||
48 | |||
49 | /** |
||
50 | * @var \Mockery\Generator\Generator |
||
51 | */ |
||
52 | protected static $_generator; |
||
53 | |||
54 | /** |
||
55 | * @var \Mockery\Loader\Loader |
||
56 | */ |
||
57 | protected static $_loader; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $_filesToCleanUp = []; |
||
63 | |||
64 | /** |
||
65 | * Defines the global helper functions |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | 3 | public static function globalHelpers() |
|
70 | { |
||
71 | 3 | require_once __DIR__.'/helpers.php'; |
|
72 | 3 | } |
|
73 | |||
74 | /** |
||
75 | * Static shortcut to \Mockery\Container::mock(). |
||
76 | * |
||
77 | * @param array $args |
||
78 | * |
||
79 | * @return \Mockery\MockInterface |
||
80 | */ |
||
81 | 60 | public static function mock(...$args) |
|
82 | { |
||
83 | 60 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Static and semantic shortcut for getting a mock from the container |
||
88 | * and applying the spy's expected behavior into it. |
||
89 | * |
||
90 | * @param array $args |
||
91 | * |
||
92 | * @return \Mockery\MockInterface |
||
93 | */ |
||
94 | 26 | public static function spy(...$args) |
|
95 | { |
||
96 | 26 | if (count($args) && $args[0] instanceof \Closure) { |
|
97 | 16 | $args[0] = new ClosureWrapper($args[0]); |
|
98 | } |
||
99 | |||
100 | 26 | return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing(); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
105 | * |
||
106 | * @param array $args |
||
107 | * |
||
108 | * @return \Mockery\MockInterface |
||
109 | */ |
||
110 | public static function instanceMock(...$args) |
||
111 | { |
||
112 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
117 | * |
||
118 | * @param array $args |
||
119 | * |
||
120 | * @return \Mockery\MockInterface |
||
121 | */ |
||
122 | 5 | public static function namedMock(...$args) |
|
123 | { |
||
124 | 5 | $name = array_shift($args); |
|
125 | |||
126 | 5 | $builder = new MockConfigurationBuilder(); |
|
127 | 5 | $builder->setName($name); |
|
128 | |||
129 | 5 | array_unshift($args, $builder); |
|
130 | |||
131 | 5 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Static shortcut to \Mockery\Container::self(). |
||
136 | * |
||
137 | * @throws LogicException |
||
138 | * |
||
139 | * @return \Mockery\MockInterface |
||
140 | */ |
||
141 | 2 | public static function self() |
|
142 | { |
||
143 | 2 | if (is_null(self::$_container)) { |
|
144 | 1 | throw new \LogicException('You have not declared any mocks yet'); |
|
145 | } |
||
146 | |||
147 | 1 | return self::$_container->self(); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Static shortcut to closing up and verifying all mocks in the global |
||
152 | * container, and resetting the container static variable to null. |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 460 | public static function close() |
|
157 | { |
||
158 | 460 | foreach (self::$_filesToCleanUp as $fileName) { |
|
159 | 18 | @unlink($fileName); |
|
160 | } |
||
161 | 460 | self::$_filesToCleanUp = []; |
|
162 | |||
163 | 460 | if (is_null(self::$_container)) { |
|
164 | return; |
||
165 | } |
||
166 | |||
167 | 460 | self::$_container->mockery_teardown(); |
|
168 | 458 | self::$_container->mockery_close(); |
|
169 | 458 | self::$_container = null; |
|
170 | 458 | } |
|
171 | |||
172 | /** |
||
173 | * Static fetching of a mock associated with a name or explicit class poser. |
||
174 | * |
||
175 | * @param $name |
||
176 | * |
||
177 | * @return \Mockery\Mock |
||
178 | */ |
||
179 | 11 | public static function fetchMock($name) |
|
180 | { |
||
181 | 11 | return self::$_container->fetchMock($name); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Lazy loader and getter for |
||
186 | * the container property. |
||
187 | * |
||
188 | * @return Mockery\Container |
||
189 | */ |
||
190 | 498 | public static function getContainer() |
|
191 | { |
||
192 | 498 | if (is_null(self::$_container)) { |
|
193 | 453 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
194 | } |
||
195 | |||
196 | 498 | return self::$_container; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Setter for the $_generator static propery. |
||
201 | * |
||
202 | * @param \Mockery\Generator\Generator $generator |
||
203 | */ |
||
204 | public static function setGenerator(Generator $generator) |
||
205 | { |
||
206 | self::$_generator = $generator; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Lazy loader method and getter for |
||
211 | * the generator property. |
||
212 | * |
||
213 | * @return Generator |
||
214 | */ |
||
215 | 453 | public static function getGenerator() |
|
216 | { |
||
217 | 453 | if (is_null(self::$_generator)) { |
|
218 | 1 | self::$_generator = self::getDefaultGenerator(); |
|
219 | } |
||
220 | |||
221 | 453 | return self::$_generator; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Creates and returns a default generator |
||
226 | * used inside this class. |
||
227 | * |
||
228 | * @return CachingGenerator |
||
229 | */ |
||
230 | 430 | public static function getDefaultGenerator() |
|
231 | { |
||
232 | 430 | return new CachingGenerator(StringManipulationGenerator::withDefaultPasses()); |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * Setter for the $_loader static property. |
||
237 | * |
||
238 | * @param Loader $loader |
||
239 | */ |
||
240 | public static function setLoader(Loader $loader) |
||
241 | { |
||
242 | self::$_loader = $loader; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Lazy loader method and getter for |
||
247 | * the $_loader property. |
||
248 | * |
||
249 | * @return Loader |
||
250 | */ |
||
251 | 453 | public static function getLoader() |
|
252 | { |
||
253 | 453 | if (is_null(self::$_loader)) { |
|
254 | 1 | self::$_loader = self::getDefaultLoader(); |
|
255 | } |
||
256 | |||
257 | 453 | return self::$_loader; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Gets an EvalLoader to be used as default. |
||
262 | * |
||
263 | * @return EvalLoader |
||
264 | */ |
||
265 | 307 | public static function getDefaultLoader() |
|
266 | { |
||
267 | 307 | return new EvalLoader(); |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Set the container. |
||
272 | * |
||
273 | * @param \Mockery\Container $container |
||
274 | * |
||
275 | * @return \Mockery\Container |
||
276 | */ |
||
277 | 18 | public static function setContainer(Mockery\Container $container) |
|
278 | { |
||
279 | 18 | return self::$_container = $container; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Reset the container to null. |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 14 | public static function resetContainer() |
|
288 | { |
||
289 | 14 | self::$_container = null; |
|
290 | 14 | } |
|
291 | |||
292 | /** |
||
293 | * Return instance of ANY matcher. |
||
294 | * |
||
295 | * @return \Mockery\Matcher\Any |
||
296 | */ |
||
297 | 5 | public static function any() |
|
298 | { |
||
299 | 5 | return new \Mockery\Matcher\Any(); |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Return instance of TYPE matcher. |
||
304 | * |
||
305 | * @param $expected |
||
306 | * |
||
307 | * @return \Mockery\Matcher\Type |
||
308 | */ |
||
309 | 47 | public static function type($expected) |
|
310 | { |
||
311 | 47 | return new \Mockery\Matcher\Type($expected); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Return instance of DUCKTYPE matcher. |
||
316 | * |
||
317 | * @param array $args |
||
318 | * |
||
319 | * @return \Mockery\Matcher\Ducktype |
||
320 | */ |
||
321 | 3 | public static function ducktype(...$args) |
|
322 | { |
||
323 | 3 | return new \Mockery\Matcher\Ducktype($args); |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Return instance of SUBSET matcher. |
||
328 | * |
||
329 | * @param array $part |
||
330 | * @param bool $strict - (Optional) True for strict comparison, false for loose |
||
331 | * |
||
332 | * @return \Mockery\Matcher\Subset |
||
333 | */ |
||
334 | 3 | public static function subset(array $part, $strict = true) |
|
335 | { |
||
336 | 3 | return new \Mockery\Matcher\Subset($part, $strict); |
|
337 | } |
||
338 | |||
339 | /** |
||
340 | * Return instance of CONTAINS matcher. |
||
341 | * |
||
342 | * @param array $args |
||
343 | * |
||
344 | * @return \Mockery\Matcher\Contains |
||
345 | */ |
||
346 | 3 | public static function contains(...$args) |
|
347 | { |
||
348 | 3 | return new \Mockery\Matcher\Contains($args); |
|
349 | } |
||
350 | |||
351 | /** |
||
352 | * Return instance of HASKEY matcher. |
||
353 | * |
||
354 | * @param $key |
||
355 | * |
||
356 | * @return \Mockery\Matcher\HasKey |
||
357 | */ |
||
358 | 4 | public static function hasKey($key) |
|
362 | |||
363 | /** |
||
364 | * Return instance of HASVALUE matcher. |
||
365 | * |
||
366 | * @param $val |
||
367 | * |
||
368 | * @return \Mockery\Matcher\HasValue |
||
369 | */ |
||
370 | 3 | public static function hasValue($val) |
|
374 | |||
375 | /** |
||
376 | * Return instance of CLOSURE matcher. |
||
377 | * |
||
378 | * @param $closure |
||
379 | * |
||
380 | * @return \Mockery\Matcher\Closure |
||
381 | */ |
||
382 | 6 | public static function on($closure) |
|
386 | |||
387 | /** |
||
388 | * Return instance of MUSTBE matcher. |
||
389 | * |
||
390 | * @param $expected |
||
391 | * |
||
392 | * @return \Mockery\Matcher\MustBe |
||
393 | */ |
||
394 | 6 | public static function mustBe($expected) |
|
398 | |||
399 | /** |
||
400 | * Return instance of NOT matcher. |
||
401 | * |
||
402 | * @param $expected |
||
403 | * |
||
404 | * @return \Mockery\Matcher\Not |
||
405 | */ |
||
406 | 3 | public static function not($expected) |
|
410 | |||
411 | /** |
||
412 | * Return instance of ANYOF matcher. |
||
413 | * |
||
414 | * @param array $args |
||
415 | * |
||
416 | * @return \Mockery\Matcher\AnyOf |
||
417 | */ |
||
418 | 3 | public static function anyOf(...$args) |
|
419 | { |
||
420 | 3 | return new \Mockery\Matcher\AnyOf($args); |
|
421 | } |
||
422 | |||
423 | /** |
||
424 | * Return instance of NOTANYOF matcher. |
||
425 | * |
||
426 | * @param array $args |
||
427 | * |
||
428 | * @return \Mockery\Matcher\NotAnyOf |
||
429 | */ |
||
430 | 3 | public static function notAnyOf(...$args) |
|
431 | { |
||
432 | 3 | return new \Mockery\Matcher\NotAnyOf($args); |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Lazy loader and Getter for the global |
||
437 | * configuration container. |
||
438 | * |
||
439 | * @return \Mockery\Configuration |
||
440 | */ |
||
441 | 486 | public static function getConfiguration() |
|
449 | |||
450 | /** |
||
451 | * Utility method to format method name and arguments into a string. |
||
452 | * |
||
453 | * @param string $method |
||
454 | * @param array $arguments |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | 102 | public static function formatArgs($method, array $arguments = null) |
|
471 | |||
472 | /** |
||
473 | * Gets the string representation |
||
474 | * of any passed argument. |
||
475 | * |
||
476 | * @param $argument |
||
477 | * @param $depth |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | 101 | private static function formatArgument($argument, $depth = 0) |
|
526 | |||
527 | /** |
||
528 | * Utility function to format objects to printable arrays. |
||
529 | * |
||
530 | * @param array $objects |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | 52 | public static function formatObjects(array $objects = null) |
|
562 | |||
563 | /** |
||
564 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
565 | * |
||
566 | * @param $object |
||
567 | * @param int $nesting |
||
568 | * |
||
569 | * @return array |
||
570 | */ |
||
571 | 7 | private static function objectToArray($object, $nesting = 3) |
|
583 | |||
584 | /** |
||
585 | * Returns all public instance properties. |
||
586 | * |
||
587 | * @param $object |
||
588 | * @param $nesting |
||
589 | * |
||
590 | * @return array |
||
591 | */ |
||
592 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
607 | |||
608 | /** |
||
609 | * Returns all object getters. |
||
610 | * |
||
611 | * @param $object |
||
612 | * @param $nesting |
||
613 | * |
||
614 | * @return array |
||
615 | */ |
||
616 | 7 | private static function extractGetters($object, $nesting) |
|
641 | |||
642 | /** |
||
643 | * Utility method used for recursively generating |
||
644 | * an object or array representation. |
||
645 | * |
||
646 | * @param $argument |
||
647 | * @param $nesting |
||
648 | * |
||
649 | * @return mixed |
||
650 | */ |
||
651 | 1 | private static function cleanupNesting($argument, $nesting) |
|
666 | |||
667 | /** |
||
668 | * Utility method for recursively |
||
669 | * gerating a representation |
||
670 | * of the given array. |
||
671 | * |
||
672 | * @param array $argument |
||
673 | * @param int $nesting |
||
674 | * |
||
675 | * @return mixed |
||
676 | */ |
||
677 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
693 | |||
694 | /** |
||
695 | * Utility function to parse shouldReceive() arguments and generate |
||
696 | * expectations from such as needed. |
||
697 | * |
||
698 | * @param Mockery\MockInterface $mock |
||
699 | * @param array $args |
||
700 | * @param callable $add |
||
701 | * @return \Mockery\CompositeExpectation |
||
702 | */ |
||
703 | 342 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
721 | |||
722 | /** |
||
723 | * Sets up expectations on the members of the CompositeExpectation and |
||
724 | * builds up any demeter chain that was passed to shouldReceive. |
||
725 | * |
||
726 | * @param \Mockery\MockInterface $mock |
||
727 | * @param string $arg |
||
728 | * @param callable $add |
||
729 | * @throws Mockery\Exception |
||
730 | * @return \Mockery\ExpectationInterface |
||
731 | */ |
||
732 | 342 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
783 | |||
784 | /** |
||
785 | * Gets a new demeter configured |
||
786 | * mock from the container. |
||
787 | * |
||
788 | * @param \Mockery\Container $container |
||
789 | * @param string $method |
||
790 | * @param Mockery\ExpectationInterface $exp |
||
791 | * |
||
792 | * @return \Mockery\Mock |
||
793 | */ |
||
794 | 11 | private static function getNewDemeterMock( |
|
804 | |||
805 | /** |
||
806 | * Gets an specific demeter mock from |
||
807 | * the ones kept by the container. |
||
808 | * |
||
809 | * @param \Mockery\Container $container |
||
810 | * @param string $demeterMockKey |
||
811 | * |
||
812 | * @return mixed |
||
813 | */ |
||
814 | 5 | private static function getExistingDemeterMock( |
|
823 | |||
824 | /** |
||
825 | * Checks if the passed array representing a demeter |
||
826 | * chain with the method names is empty. |
||
827 | * |
||
828 | * @param array $methodNames |
||
829 | * |
||
830 | * @return bool |
||
831 | */ |
||
832 | 336 | private static function noMoreElementsInChain(array $methodNames) |
|
836 | |||
837 | 17 | public static function declareClass($fqn) |
|
841 | |||
842 | 2 | public static function declareInterface($fqn) |
|
846 | |||
847 | 18 | private static function declareType($fqn, $type) |
|
873 | |||
874 | /** |
||
875 | * Register a file to be deleted on tearDown. |
||
876 | * |
||
877 | * @param string $fileName |
||
878 | */ |
||
879 | 18 | public static function registerFileForCleanUp($fileName) |
|
883 | } |
||
884 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: