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 |
||
39 | class Mockery |
||
40 | { |
||
41 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
42 | |||
43 | /** |
||
44 | * Global container to hold all mocks for the current unit test running. |
||
45 | * |
||
46 | * @var \Mockery\Container |
||
47 | */ |
||
48 | protected static $_container = null; |
||
49 | |||
50 | /** |
||
51 | * Global configuration handler containing configuration options. |
||
52 | * |
||
53 | * @var \Mockery\Configuration |
||
54 | */ |
||
55 | protected static $_config = null; |
||
56 | |||
57 | /** |
||
58 | * @var \Mockery\Generator\Generator |
||
59 | */ |
||
60 | protected static $_generator; |
||
61 | |||
62 | /** |
||
63 | * @var \Mockery\Loader\Loader |
||
64 | */ |
||
65 | protected static $_loader; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private static $_filesToCleanUp = []; |
||
71 | |||
72 | /** |
||
73 | * Static shortcut to \Mockery\Container::mock(). |
||
74 | * |
||
75 | * @return \Mockery\MockInterface |
||
76 | */ |
||
77 | 45 | public static function mock() |
|
83 | |||
84 | /** |
||
85 | * Static and semantic shortcut for getting a mock from the container |
||
86 | * and applying the spy's expected behavior into it. |
||
87 | * |
||
88 | * @return \Mockery\MockInterface |
||
89 | */ |
||
90 | 8 | public static function spy() |
|
95 | |||
96 | /** |
||
97 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
98 | * |
||
99 | * @return \Mockery\MockInterface |
||
100 | */ |
||
101 | public static function instanceMock() |
||
107 | |||
108 | /** |
||
109 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
110 | * |
||
111 | * @return \Mockery\MockInterface |
||
112 | */ |
||
113 | 4 | public static function namedMock() |
|
125 | |||
126 | /** |
||
127 | * Static shortcut to \Mockery\Container::self(). |
||
128 | * |
||
129 | * @throws LogicException |
||
130 | * |
||
131 | * @return \Mockery\MockInterface |
||
132 | */ |
||
133 | 2 | public static function self() |
|
134 | { |
||
135 | 2 | if (is_null(self::$_container)) { |
|
136 | 1 | throw new \LogicException('You have not declared any mocks yet'); |
|
137 | } |
||
138 | |||
139 | 1 | return self::$_container->self(); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Static shortcut to closing up and verifying all mocks in the global |
||
144 | * container, and resetting the container static variable to null. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | 430 | public static function close() |
|
149 | { |
||
150 | 430 | foreach (self::$_filesToCleanUp as $fileName) { |
|
151 | 18 | @unlink($fileName); |
|
152 | } |
||
153 | 430 | self::$_filesToCleanUp = []; |
|
154 | |||
155 | 430 | if (is_null(self::$_container)) { |
|
156 | return; |
||
157 | } |
||
158 | |||
159 | 430 | self::$_container->mockery_teardown(); |
|
160 | 430 | self::$_container->mockery_close(); |
|
161 | 430 | self::$_container = null; |
|
162 | 430 | } |
|
163 | |||
164 | /** |
||
165 | * Static fetching of a mock associated with a name or explicit class poser. |
||
166 | * |
||
167 | * @param $name |
||
168 | * |
||
169 | * @return \Mockery\Mock |
||
170 | */ |
||
171 | 11 | public static function fetchMock($name) |
|
175 | |||
176 | /** |
||
177 | * Lazy loader and getter for |
||
178 | * the container property. |
||
179 | * |
||
180 | * @return Mockery\Container |
||
181 | */ |
||
182 | 459 | public static function getContainer() |
|
183 | { |
||
184 | 459 | if (is_null(self::$_container)) { |
|
185 | 425 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
186 | } |
||
187 | |||
188 | 459 | return self::$_container; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Setter for the $_generator static propery. |
||
193 | * |
||
194 | * @param \Mockery\Generator\Generator $generator |
||
195 | */ |
||
196 | public static function setGenerator(Generator $generator) |
||
200 | |||
201 | /** |
||
202 | * Lazy loader method and getter for |
||
203 | * the generator property. |
||
204 | * |
||
205 | * @return Generator |
||
206 | */ |
||
207 | 425 | public static function getGenerator() |
|
215 | |||
216 | /** |
||
217 | * Creates and returns a default generator |
||
218 | * used inside this class. |
||
219 | * |
||
220 | * @return CachingGenerator |
||
221 | */ |
||
222 | 425 | public static function getDefaultGenerator() |
|
226 | |||
227 | /** |
||
228 | * Setter for the $_loader static property. |
||
229 | * |
||
230 | * @param Loader $loader |
||
231 | */ |
||
232 | public static function setLoader(Loader $loader) |
||
236 | |||
237 | /** |
||
238 | * Lazy loader method and getter for |
||
239 | * the $_loader property. |
||
240 | * |
||
241 | * @return Loader |
||
242 | */ |
||
243 | 425 | public static function getLoader() |
|
251 | |||
252 | /** |
||
253 | * Gets an EvalLoader to be used as default. |
||
254 | * |
||
255 | * @return EvalLoader |
||
256 | */ |
||
257 | 302 | public static function getDefaultLoader() |
|
261 | |||
262 | /** |
||
263 | * Set the container. |
||
264 | * |
||
265 | * @param \Mockery\Container $container |
||
266 | * |
||
267 | * @return \Mockery\Container |
||
268 | */ |
||
269 | 18 | public static function setContainer(Mockery\Container $container) |
|
273 | |||
274 | /** |
||
275 | * Reset the container to null. |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | 14 | public static function resetContainer() |
|
283 | |||
284 | /** |
||
285 | * Return instance of ANY matcher. |
||
286 | * |
||
287 | * @return \Mockery\Matcher\Any |
||
288 | */ |
||
289 | 5 | public static function any() |
|
293 | |||
294 | /** |
||
295 | * Return instance of TYPE matcher. |
||
296 | * |
||
297 | * @param $expected |
||
298 | * |
||
299 | * @return \Mockery\Matcher\Type |
||
300 | */ |
||
301 | 47 | public static function type($expected) |
|
305 | |||
306 | /** |
||
307 | * Return instance of DUCKTYPE matcher. |
||
308 | * |
||
309 | * @return \Mockery\Matcher\Ducktype |
||
310 | */ |
||
311 | 3 | public static function ducktype() |
|
315 | |||
316 | /** |
||
317 | * Return instance of SUBSET matcher. |
||
318 | * |
||
319 | * @param array $part |
||
320 | * |
||
321 | * @return \Mockery\Matcher\Subset |
||
322 | */ |
||
323 | 3 | public static function subset(array $part) |
|
327 | |||
328 | /** |
||
329 | * Return instance of CONTAINS matcher. |
||
330 | * |
||
331 | * @return \Mockery\Matcher\Contains |
||
332 | */ |
||
333 | 3 | public static function contains() |
|
337 | |||
338 | /** |
||
339 | * Return instance of HASKEY matcher. |
||
340 | * |
||
341 | * @param $key |
||
342 | * |
||
343 | * @return \Mockery\Matcher\HasKey |
||
344 | */ |
||
345 | 3 | public static function hasKey($key) |
|
349 | |||
350 | /** |
||
351 | * Return instance of HASVALUE matcher. |
||
352 | * |
||
353 | * @param $val |
||
354 | * |
||
355 | * @return \Mockery\Matcher\HasValue |
||
356 | */ |
||
357 | 3 | public static function hasValue($val) |
|
361 | |||
362 | /** |
||
363 | * Return instance of CLOSURE matcher. |
||
364 | * |
||
365 | * @param $closure |
||
366 | * |
||
367 | * @return \Mockery\Matcher\Closure |
||
368 | */ |
||
369 | 6 | public static function on($closure) |
|
373 | |||
374 | /** |
||
375 | * Return instance of MUSTBE matcher. |
||
376 | * |
||
377 | * @param $expected |
||
378 | * |
||
379 | * @return \Mockery\Matcher\MustBe |
||
380 | */ |
||
381 | 6 | public static function mustBe($expected) |
|
385 | |||
386 | /** |
||
387 | * Return instance of NOT matcher. |
||
388 | * |
||
389 | * @param $expected |
||
390 | * |
||
391 | * @return \Mockery\Matcher\Not |
||
392 | */ |
||
393 | 3 | public static function not($expected) |
|
397 | |||
398 | /** |
||
399 | * Return instance of ANYOF matcher. |
||
400 | * |
||
401 | * @return \Mockery\Matcher\AnyOf |
||
402 | */ |
||
403 | 3 | public static function anyOf() |
|
407 | |||
408 | /** |
||
409 | * Return instance of NOTANYOF matcher. |
||
410 | * |
||
411 | * @return \Mockery\Matcher\NotAnyOf |
||
412 | */ |
||
413 | 3 | public static function notAnyOf() |
|
417 | |||
418 | /** |
||
419 | * Lazy loader and Getter for the global |
||
420 | * configuration container. |
||
421 | * |
||
422 | * @return \Mockery\Configuration |
||
423 | */ |
||
424 | 452 | public static function getConfiguration() |
|
432 | |||
433 | /** |
||
434 | * Utility method to format method name and arguments into a string. |
||
435 | * |
||
436 | * @param string $method |
||
437 | * @param array $arguments |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 90 | public static function formatArgs($method, array $arguments = null) |
|
454 | |||
455 | /** |
||
456 | * Gets the string representation |
||
457 | * of any passed argument. |
||
458 | * |
||
459 | * @param $argument |
||
460 | * @param $depth |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | 56 | private static function formatArgument($argument, $depth = 0) |
|
505 | |||
506 | /** |
||
507 | * Utility function to format objects to printable arrays. |
||
508 | * |
||
509 | * @param array $objects |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | 52 | public static function formatObjects(array $objects = null) |
|
541 | |||
542 | /** |
||
543 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
544 | * |
||
545 | * @param $object |
||
546 | * @param int $nesting |
||
547 | * |
||
548 | * @return array |
||
549 | */ |
||
550 | 7 | private static function objectToArray($object, $nesting = 3) |
|
562 | |||
563 | /** |
||
564 | * Returns all public instance properties. |
||
565 | * |
||
566 | * @param $object |
||
567 | * @param $nesting |
||
568 | * |
||
569 | * @return array |
||
570 | */ |
||
571 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
586 | |||
587 | /** |
||
588 | * Returns all object getters. |
||
589 | * |
||
590 | * @param $object |
||
591 | * @param $nesting |
||
592 | * |
||
593 | * @return array |
||
594 | */ |
||
595 | 7 | private static function extractGetters($object, $nesting) |
|
620 | |||
621 | /** |
||
622 | * Utility method used for recursively generating |
||
623 | * an object or array representation. |
||
624 | * |
||
625 | * @param $argument |
||
626 | * @param $nesting |
||
627 | * |
||
628 | * @return mixed |
||
629 | */ |
||
630 | 1 | private static function cleanupNesting($argument, $nesting) |
|
645 | |||
646 | /** |
||
647 | * Utility method for recursively |
||
648 | * gerating a representation |
||
649 | * of the given array. |
||
650 | * |
||
651 | * @param array $argument |
||
652 | * @param int $nesting |
||
653 | * |
||
654 | * @return mixed |
||
655 | */ |
||
656 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
672 | |||
673 | /** |
||
674 | * Utility function to parse shouldReceive() arguments and generate |
||
675 | * expectations from such as needed. |
||
676 | * |
||
677 | * @param Mockery\MockInterface $mock |
||
678 | * @param array $args |
||
679 | * @param callable $add |
||
680 | * @return \Mockery\CompositeExpectation |
||
681 | */ |
||
682 | 329 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
700 | |||
701 | /** |
||
702 | * Sets up expectations on the members of the CompositeExpectation and |
||
703 | * builds up any demeter chain that was passed to shouldReceive. |
||
704 | * |
||
705 | * @param \Mockery\MockInterface $mock |
||
706 | * @param string $arg |
||
707 | * @param callable $add |
||
708 | * @throws Mockery\Exception |
||
709 | * @return \Mockery\ExpectationInterface |
||
710 | */ |
||
711 | 329 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
762 | |||
763 | /** |
||
764 | * Gets a new demeter configured |
||
765 | * mock from the container. |
||
766 | * |
||
767 | * @param \Mockery\Container $container |
||
768 | * @param string $method |
||
769 | * @param Mockery\ExpectationInterface $exp |
||
770 | * |
||
771 | * @return \Mockery\Mock |
||
772 | */ |
||
773 | 11 | private static function getNewDemeterMock( |
|
783 | |||
784 | /** |
||
785 | * Gets an specific demeter mock from |
||
786 | * the ones kept by the container. |
||
787 | * |
||
788 | * @param \Mockery\Container $container |
||
789 | * @param string $demeterMockKey |
||
790 | * |
||
791 | * @return mixed |
||
792 | */ |
||
793 | 5 | private static function getExistingDemeterMock( |
|
802 | |||
803 | /** |
||
804 | * Checks if the passed array representing a demeter |
||
805 | * chain with the method names is empty. |
||
806 | * |
||
807 | * @param array $methodNames |
||
808 | * |
||
809 | * @return bool |
||
810 | */ |
||
811 | 323 | private static function noMoreElementsInChain(array $methodNames) |
|
815 | |||
816 | 17 | public static function declareClass($fqn) |
|
820 | |||
821 | 2 | public static function declareInterface($fqn) |
|
825 | |||
826 | 18 | private static function declareType($fqn, $type) |
|
852 | |||
853 | /** |
||
854 | * Register a file to be deleted on tearDown. |
||
855 | * |
||
856 | * @param string $fileName |
||
857 | */ |
||
858 | 18 | public static function registerFileForCleanUp($fileName) |
|
862 | } |
||
863 |
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: