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 |
||
40 | class Mockery |
||
41 | { |
||
42 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
43 | |||
44 | /** |
||
45 | * Global container to hold all mocks for the current unit test running. |
||
46 | * |
||
47 | * @var \Mockery\Container |
||
48 | */ |
||
49 | protected static $_container = null; |
||
50 | |||
51 | /** |
||
52 | * Global configuration handler containing configuration options. |
||
53 | * |
||
54 | * @var \Mockery\Configuration |
||
55 | */ |
||
56 | protected static $_config = null; |
||
57 | |||
58 | /** |
||
59 | * @var \Mockery\Generator\Generator |
||
60 | */ |
||
61 | protected static $_generator; |
||
62 | |||
63 | /** |
||
64 | * @var \Mockery\Loader\Loader |
||
65 | */ |
||
66 | protected static $_loader; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private static $_filesToCleanUp = []; |
||
72 | |||
73 | /** |
||
74 | * Defines the global helper functions |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 3 | public static function globalHelpers() |
|
82 | |||
83 | /** |
||
84 | * Static shortcut to \Mockery\Container::mock(). |
||
85 | * |
||
86 | * @return \Mockery\MockInterface |
||
87 | */ |
||
88 | 34 | public static function mock() |
|
94 | |||
95 | /** |
||
96 | * Static and semantic shortcut for getting a mock from the container |
||
97 | * and applying the spy's expected behavior into it. |
||
98 | * |
||
99 | * @return \Mockery\MockInterface |
||
100 | */ |
||
101 | 8 | public static function spy() |
|
106 | |||
107 | /** |
||
108 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
109 | * |
||
110 | * @return \Mockery\MockInterface |
||
111 | */ |
||
112 | public static function instanceMock() |
||
118 | |||
119 | /** |
||
120 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
121 | * |
||
122 | * @return \Mockery\MockInterface |
||
123 | */ |
||
124 | 5 | public static function namedMock() |
|
136 | |||
137 | /** |
||
138 | * Static shortcut to \Mockery\Container::self(). |
||
139 | * |
||
140 | * @throws LogicException |
||
141 | * |
||
142 | * @return \Mockery\MockInterface |
||
143 | */ |
||
144 | 2 | public static function self() |
|
152 | |||
153 | /** |
||
154 | * Static shortcut to closing up and verifying all mocks in the global |
||
155 | * container, and resetting the container static variable to null. |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | 405 | public static function close() |
|
174 | |||
175 | /** |
||
176 | * Static fetching of a mock associated with a name or explicit class poser. |
||
177 | * |
||
178 | * @param $name |
||
179 | * |
||
180 | * @return \Mockery\Mock |
||
181 | */ |
||
182 | 11 | public static function fetchMock($name) |
|
186 | |||
187 | /** |
||
188 | * Lazy loader and getter for |
||
189 | * the container property. |
||
190 | * |
||
191 | * @return Mockery\Container |
||
192 | */ |
||
193 | 428 | public static function getContainer() |
|
201 | |||
202 | /** |
||
203 | * Setter for the $_generator static propery. |
||
204 | * |
||
205 | * @param \Mockery\Generator\Generator $generator |
||
206 | */ |
||
207 | public static function setGenerator(Generator $generator) |
||
211 | |||
212 | /** |
||
213 | * Lazy loader method and getter for |
||
214 | * the generator property. |
||
215 | * |
||
216 | * @return Generator |
||
217 | */ |
||
218 | 398 | public static function getGenerator() |
|
226 | |||
227 | /** |
||
228 | * Creates and returns a default generator |
||
229 | * used inside this class. |
||
230 | * |
||
231 | * @return CachingGenerator |
||
232 | */ |
||
233 | 398 | public static function getDefaultGenerator() |
|
237 | |||
238 | /** |
||
239 | * Setter for the $_loader static property. |
||
240 | * |
||
241 | * @param Loader $loader |
||
242 | */ |
||
243 | public static function setLoader(Loader $loader) |
||
247 | |||
248 | /** |
||
249 | * Lazy loader method and getter for |
||
250 | * the $_loader property. |
||
251 | * |
||
252 | * @return Loader |
||
253 | */ |
||
254 | 398 | public static function getLoader() |
|
262 | |||
263 | /** |
||
264 | * Gets an EvalLoader to be used as default. |
||
265 | * |
||
266 | * @return EvalLoader |
||
267 | */ |
||
268 | 272 | public static function getDefaultLoader() |
|
272 | |||
273 | /** |
||
274 | * Set the container. |
||
275 | * |
||
276 | * @param \Mockery\Container $container |
||
277 | * |
||
278 | * @return \Mockery\Container |
||
279 | */ |
||
280 | 18 | public static function setContainer(Mockery\Container $container) |
|
284 | |||
285 | /** |
||
286 | * Reset the container to null. |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | 15 | public static function resetContainer() |
|
294 | |||
295 | /** |
||
296 | * Return instance of ANY matcher. |
||
297 | * |
||
298 | * @return \Mockery\Matcher\Any |
||
299 | */ |
||
300 | 5 | public static function any() |
|
304 | |||
305 | /** |
||
306 | * Return instance of TYPE matcher. |
||
307 | * |
||
308 | * @param $expected |
||
309 | * |
||
310 | * @return \Mockery\Matcher\Type |
||
311 | */ |
||
312 | 48 | public static function type($expected) |
|
316 | |||
317 | /** |
||
318 | * Return instance of DUCKTYPE matcher. |
||
319 | * |
||
320 | * @return \Mockery\Matcher\Ducktype |
||
321 | */ |
||
322 | 3 | public static function ducktype() |
|
326 | |||
327 | /** |
||
328 | * Return instance of SUBSET matcher. |
||
329 | * |
||
330 | * @param array $part |
||
331 | * |
||
332 | * @return \Mockery\Matcher\Subset |
||
333 | */ |
||
334 | 3 | public static function subset(array $part) |
|
338 | |||
339 | /** |
||
340 | * Return instance of CONTAINS matcher. |
||
341 | * |
||
342 | * @return \Mockery\Matcher\Contains |
||
343 | */ |
||
344 | 3 | public static function contains() |
|
348 | |||
349 | /** |
||
350 | * Return instance of HASKEY matcher. |
||
351 | * |
||
352 | * @param $key |
||
353 | * |
||
354 | * @return \Mockery\Matcher\HasKey |
||
355 | */ |
||
356 | 3 | public static function hasKey($key) |
|
360 | |||
361 | /** |
||
362 | * Return instance of HASVALUE matcher. |
||
363 | * |
||
364 | * @param $val |
||
365 | * |
||
366 | * @return \Mockery\Matcher\HasValue |
||
367 | */ |
||
368 | 3 | public static function hasValue($val) |
|
372 | |||
373 | /** |
||
374 | * Return instance of CLOSURE matcher. |
||
375 | * |
||
376 | * @param $closure |
||
377 | * |
||
378 | * @return \Mockery\Matcher\Closure |
||
379 | */ |
||
380 | 7 | public static function on($closure) |
|
384 | |||
385 | /** |
||
386 | * Return instance of MUSTBE matcher. |
||
387 | * |
||
388 | * @param $expected |
||
389 | * |
||
390 | * @return \Mockery\Matcher\MustBe |
||
391 | */ |
||
392 | 6 | public static function mustBe($expected) |
|
396 | |||
397 | /** |
||
398 | * Return instance of NOT matcher. |
||
399 | * |
||
400 | * @param $expected |
||
401 | * |
||
402 | * @return \Mockery\Matcher\Not |
||
403 | */ |
||
404 | 3 | public static function not($expected) |
|
408 | |||
409 | /** |
||
410 | * Return instance of ANYOF matcher. |
||
411 | * |
||
412 | * @return \Mockery\Matcher\AnyOf |
||
413 | */ |
||
414 | 3 | public static function anyOf() |
|
418 | |||
419 | /** |
||
420 | * Return instance of NOTANYOF matcher. |
||
421 | * |
||
422 | * @return \Mockery\Matcher\NotAnyOf |
||
423 | */ |
||
424 | 3 | public static function notAnyOf() |
|
428 | |||
429 | /** |
||
430 | * Lazy loader and Getter for the global |
||
431 | * configuration container. |
||
432 | * |
||
433 | * @return \Mockery\Configuration |
||
434 | */ |
||
435 | 421 | public static function getConfiguration() |
|
443 | |||
444 | /** |
||
445 | * Utility method to format method name and arguments into a string. |
||
446 | * |
||
447 | * @param string $method |
||
448 | * @param array $arguments |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | 92 | public static function formatArgs($method, array $arguments = null) |
|
465 | |||
466 | /** |
||
467 | * Gets the string representation |
||
468 | * of any passed argument. |
||
469 | * |
||
470 | * @param $argument |
||
471 | * @param $depth |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | 58 | private static function formatArgument($argument, $depth = 0) |
|
516 | |||
517 | /** |
||
518 | * Utility function to format objects to printable arrays. |
||
519 | * |
||
520 | * @param array $objects |
||
521 | * |
||
522 | * @return string |
||
523 | */ |
||
524 | 52 | public static function formatObjects(array $objects = null) |
|
552 | |||
553 | /** |
||
554 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
555 | * |
||
556 | * @param $object |
||
557 | * @param int $nesting |
||
558 | * |
||
559 | * @return array |
||
560 | */ |
||
561 | 7 | private static function objectToArray($object, $nesting = 3) |
|
573 | |||
574 | /** |
||
575 | * Returns all public instance properties. |
||
576 | * |
||
577 | * @param $object |
||
578 | * @param $nesting |
||
579 | * |
||
580 | * @return array |
||
581 | */ |
||
582 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
597 | |||
598 | /** |
||
599 | * Returns all object getters. |
||
600 | * |
||
601 | * @param $object |
||
602 | * @param $nesting |
||
603 | * |
||
604 | * @return array |
||
605 | */ |
||
606 | 7 | private static function extractGetters($object, $nesting) |
|
631 | |||
632 | /** |
||
633 | * Utility method used for recursively generating |
||
634 | * an object or array representation. |
||
635 | * |
||
636 | * @param $argument |
||
637 | * @param $nesting |
||
638 | * |
||
639 | * @return mixed |
||
640 | */ |
||
641 | 1 | private static function cleanupNesting($argument, $nesting) |
|
656 | |||
657 | /** |
||
658 | * Utility method for recursively |
||
659 | * gerating a representation |
||
660 | * of the given array. |
||
661 | * |
||
662 | * @param array $argument |
||
663 | * @param int $nesting |
||
664 | * |
||
665 | * @return mixed |
||
666 | */ |
||
667 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
683 | |||
684 | /** |
||
685 | * Utility function to parse shouldReceive() arguments and generate |
||
686 | * expectations from such as needed. |
||
687 | * |
||
688 | * @param Mockery\MockInterface $mock |
||
689 | * @param array $args |
||
690 | * @param callable $add |
||
691 | * @return \Mockery\CompositeExpectation |
||
692 | */ |
||
693 | 305 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
711 | |||
712 | /** |
||
713 | * Sets up expectations on the members of the CompositeExpectation and |
||
714 | * builds up any demeter chain that was passed to shouldReceive. |
||
715 | * |
||
716 | * @param \Mockery\MockInterface $mock |
||
717 | * @param string $arg |
||
718 | * @param callable $add |
||
719 | * @throws Mockery\Exception |
||
720 | * @return \Mockery\ExpectationInterface |
||
721 | */ |
||
722 | 305 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
773 | |||
774 | /** |
||
775 | * Gets a new demeter configured |
||
776 | * mock from the container. |
||
777 | * |
||
778 | * @param \Mockery\Container $container |
||
779 | * @param string $method |
||
780 | * @param Mockery\ExpectationInterface $exp |
||
781 | * |
||
782 | * @return \Mockery\Mock |
||
783 | */ |
||
784 | 11 | private static function getNewDemeterMock( |
|
794 | |||
795 | /** |
||
796 | * Gets an specific demeter mock from |
||
797 | * the ones kept by the container. |
||
798 | * |
||
799 | * @param \Mockery\Container $container |
||
800 | * @param string $demeterMockKey |
||
801 | * |
||
802 | * @return mixed |
||
803 | */ |
||
804 | 5 | private static function getExistingDemeterMock( |
|
813 | |||
814 | /** |
||
815 | * Checks if the passed array representing a demeter |
||
816 | * chain with the method names is empty. |
||
817 | * |
||
818 | * @param array $methodNames |
||
819 | * |
||
820 | * @return bool |
||
821 | */ |
||
822 | 299 | private static function noMoreElementsInChain(array $methodNames) |
|
826 | |||
827 | 17 | public static function declareClass($fqn) |
|
831 | |||
832 | 2 | public static function declareInterface($fqn) |
|
836 | |||
837 | 18 | private static function declareType($fqn, $type) |
|
863 | |||
864 | /** |
||
865 | * Register a file to be deleted on tearDown. |
||
866 | * |
||
867 | * @param string $fileName |
||
868 | */ |
||
869 | 18 | public static function registerFileForCleanUp($fileName) |
|
873 | } |
||
874 |
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: