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 | * Static shortcut to \Mockery\Container::mock(). |
||
75 | * |
||
76 | * @return \Mockery\MockInterface |
||
77 | */ |
||
78 | 33 | public static function mock() |
|
79 | { |
||
80 | 33 | $args = func_get_args(); |
|
81 | |||
82 | 33 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Static and semantic shortcut for getting a mock from the container |
||
87 | * and applying the spy's expected behavior into it. |
||
88 | * |
||
89 | * @return \Mockery\MockInterface |
||
90 | */ |
||
91 | 7 | public static function spy() |
|
92 | { |
||
93 | 7 | $args = func_get_args(); |
|
94 | 7 | return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing(); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
99 | * |
||
100 | * @return \Mockery\MockInterface |
||
101 | */ |
||
102 | public static function instanceMock() |
||
103 | { |
||
104 | $args = func_get_args(); |
||
105 | |||
106 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
111 | * |
||
112 | * @return \Mockery\MockInterface |
||
113 | */ |
||
114 | 4 | public static function namedMock() |
|
115 | { |
||
116 | 4 | $args = func_get_args(); |
|
117 | 4 | $name = array_shift($args); |
|
118 | |||
119 | 4 | $builder = new MockConfigurationBuilder(); |
|
120 | 4 | $builder->setName($name); |
|
121 | |||
122 | 4 | array_unshift($args, $builder); |
|
123 | |||
124 | 4 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Static shortcut to \Mockery\Container::self(). |
||
129 | * |
||
130 | * @throws LogicException |
||
131 | * |
||
132 | * @return \Mockery\MockInterface |
||
133 | */ |
||
134 | 2 | public static function self() |
|
135 | { |
||
136 | 2 | if (is_null(self::$_container)) { |
|
137 | 1 | throw new \LogicException('You have not declared any mocks yet'); |
|
138 | } |
||
139 | |||
140 | 1 | return self::$_container->self(); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Static shortcut to closing up and verifying all mocks in the global |
||
145 | * container, and resetting the container static variable to null. |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | 405 | public static function close() |
|
150 | { |
||
151 | 405 | foreach (self::$_filesToCleanUp as $fileName) { |
|
152 | 18 | @unlink($fileName); |
|
153 | 405 | } |
|
154 | 405 | self::$_filesToCleanUp = []; |
|
155 | |||
156 | 405 | if (is_null(self::$_container)) { |
|
157 | return; |
||
158 | } |
||
159 | |||
160 | 405 | self::$_container->mockery_teardown(); |
|
161 | 403 | self::$_container->mockery_close(); |
|
162 | 403 | self::$_container = null; |
|
163 | 403 | } |
|
164 | |||
165 | /** |
||
166 | * Static fetching of a mock associated with a name or explicit class poser. |
||
167 | * |
||
168 | * @param $name |
||
169 | * |
||
170 | * @return \Mockery\Mock |
||
171 | */ |
||
172 | 11 | public static function fetchMock($name) |
|
173 | { |
||
174 | 11 | return self::$_container->fetchMock($name); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Lazy loader and getter for |
||
179 | * the container property. |
||
180 | * |
||
181 | * @return Mockery\Container |
||
182 | */ |
||
183 | 425 | public static function getContainer() |
|
184 | { |
||
185 | 425 | if (is_null(self::$_container)) { |
|
186 | 398 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
187 | 398 | } |
|
188 | |||
189 | 425 | return self::$_container; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Setter for the $_generator static propery. |
||
194 | * |
||
195 | * @param \Mockery\Generator\Generator $generator |
||
196 | */ |
||
197 | public static function setGenerator(Generator $generator) |
||
198 | { |
||
199 | self::$_generator = $generator; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Lazy loader method and getter for |
||
204 | * the generator property. |
||
205 | * |
||
206 | * @return Generator |
||
207 | */ |
||
208 | 398 | public static function getGenerator() |
|
216 | |||
217 | /** |
||
218 | * Creates and returns a default generator |
||
219 | * used inside this class. |
||
220 | * |
||
221 | * @return CachingGenerator |
||
222 | */ |
||
223 | 398 | public static function getDefaultGenerator() |
|
224 | { |
||
225 | 398 | return new CachingGenerator(StringManipulationGenerator::withDefaultPasses()); |
|
227 | |||
228 | /** |
||
229 | * Setter for the $_loader static property. |
||
230 | * |
||
231 | * @param Loader $loader |
||
232 | */ |
||
233 | public static function setLoader(Loader $loader) |
||
237 | |||
238 | /** |
||
239 | * Lazy loader method and getter for |
||
240 | * the $_loader property. |
||
241 | * |
||
242 | * @return Loader |
||
243 | */ |
||
244 | 398 | public static function getLoader() |
|
252 | |||
253 | /** |
||
254 | * Gets an EvalLoader to be used as default. |
||
255 | * |
||
256 | * @return EvalLoader |
||
257 | */ |
||
258 | 272 | public static function getDefaultLoader() |
|
262 | |||
263 | /** |
||
264 | * Set the container. |
||
265 | * |
||
266 | * @param \Mockery\Container $container |
||
267 | * |
||
268 | * @return \Mockery\Container |
||
269 | */ |
||
270 | 18 | public static function setContainer(Mockery\Container $container) |
|
274 | |||
275 | /** |
||
276 | * Reset the container to null. |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | 15 | public static function resetContainer() |
|
284 | |||
285 | /** |
||
286 | * Return instance of ANY matcher. |
||
287 | * |
||
288 | * @return \Mockery\Matcher\Any |
||
289 | */ |
||
290 | 5 | public static function any() |
|
294 | |||
295 | /** |
||
296 | * Return instance of TYPE matcher. |
||
297 | * |
||
298 | * @param $expected |
||
299 | * |
||
300 | * @return \Mockery\Matcher\Type |
||
301 | */ |
||
302 | 48 | public static function type($expected) |
|
306 | |||
307 | /** |
||
308 | * Return instance of DUCKTYPE matcher. |
||
309 | * |
||
310 | * @return \Mockery\Matcher\Ducktype |
||
311 | */ |
||
312 | 3 | public static function ducktype() |
|
316 | |||
317 | /** |
||
318 | * Return instance of SUBSET matcher. |
||
319 | * |
||
320 | * @param array $part |
||
321 | * |
||
322 | * @return \Mockery\Matcher\Subset |
||
323 | */ |
||
324 | 3 | public static function subset(array $part) |
|
328 | |||
329 | /** |
||
330 | * Return instance of CONTAINS matcher. |
||
331 | * |
||
332 | * @return \Mockery\Matcher\Contains |
||
333 | */ |
||
334 | 3 | public static function contains() |
|
338 | |||
339 | /** |
||
340 | * Return instance of HASKEY matcher. |
||
341 | * |
||
342 | * @param $key |
||
343 | * |
||
344 | * @return \Mockery\Matcher\HasKey |
||
345 | */ |
||
346 | 3 | public static function hasKey($key) |
|
350 | |||
351 | /** |
||
352 | * Return instance of HASVALUE matcher. |
||
353 | * |
||
354 | * @param $val |
||
355 | * |
||
356 | * @return \Mockery\Matcher\HasValue |
||
357 | */ |
||
358 | 3 | public static function hasValue($val) |
|
362 | |||
363 | /** |
||
364 | * Return instance of CLOSURE matcher. |
||
365 | * |
||
366 | * @param $closure |
||
367 | * |
||
368 | * @return \Mockery\Matcher\Closure |
||
369 | */ |
||
370 | 7 | public static function on($closure) |
|
374 | |||
375 | /** |
||
376 | * Return instance of MUSTBE matcher. |
||
377 | * |
||
378 | * @param $expected |
||
379 | * |
||
380 | * @return \Mockery\Matcher\MustBe |
||
381 | */ |
||
382 | 6 | public static function mustBe($expected) |
|
386 | |||
387 | /** |
||
388 | * Return instance of NOT matcher. |
||
389 | * |
||
390 | * @param $expected |
||
391 | * |
||
392 | * @return \Mockery\Matcher\Not |
||
393 | */ |
||
394 | 3 | public static function not($expected) |
|
398 | |||
399 | /** |
||
400 | * Return instance of ANYOF matcher. |
||
401 | * |
||
402 | * @return \Mockery\Matcher\AnyOf |
||
403 | */ |
||
404 | 3 | public static function anyOf() |
|
408 | |||
409 | /** |
||
410 | * Return instance of NOTANYOF matcher. |
||
411 | * |
||
412 | * @return \Mockery\Matcher\NotAnyOf |
||
413 | */ |
||
414 | 3 | public static function notAnyOf() |
|
418 | |||
419 | /** |
||
420 | * Lazy loader and Getter for the global |
||
421 | * configuration container. |
||
422 | * |
||
423 | * @return \Mockery\Configuration |
||
424 | */ |
||
425 | 418 | public static function getConfiguration() |
|
433 | |||
434 | /** |
||
435 | * Utility method to format method name and arguments into a string. |
||
436 | * |
||
437 | * @param string $method |
||
438 | * @param array $arguments |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 92 | public static function formatArgs($method, array $arguments = null) |
|
455 | |||
456 | /** |
||
457 | * Gets the string representation |
||
458 | * of any passed argument. |
||
459 | * |
||
460 | * @param $argument |
||
461 | * @param $depth |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | 58 | private static function formatArgument($argument, $depth = 0) |
|
506 | |||
507 | /** |
||
508 | * Utility function to format objects to printable arrays. |
||
509 | * |
||
510 | * @param array $objects |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | 52 | public static function formatObjects(array $objects = null) |
|
542 | |||
543 | /** |
||
544 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
545 | * |
||
546 | * @param $object |
||
547 | * @param int $nesting |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | 7 | private static function objectToArray($object, $nesting = 3) |
|
563 | |||
564 | /** |
||
565 | * Returns all public instance properties. |
||
566 | * |
||
567 | * @param $object |
||
568 | * @param $nesting |
||
569 | * |
||
570 | * @return array |
||
571 | */ |
||
572 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
587 | |||
588 | /** |
||
589 | * Returns all object getters. |
||
590 | * |
||
591 | * @param $object |
||
592 | * @param $nesting |
||
593 | * |
||
594 | * @return array |
||
595 | */ |
||
596 | 7 | private static function extractGetters($object, $nesting) |
|
621 | |||
622 | /** |
||
623 | * Utility method used for recursively generating |
||
624 | * an object or array representation. |
||
625 | * |
||
626 | * @param $argument |
||
627 | * @param $nesting |
||
628 | * |
||
629 | * @return mixed |
||
630 | */ |
||
631 | 1 | private static function cleanupNesting($argument, $nesting) |
|
646 | |||
647 | /** |
||
648 | * Utility method for recursively |
||
649 | * gerating a representation |
||
650 | * of the given array. |
||
651 | * |
||
652 | * @param array $argument |
||
653 | * @param int $nesting |
||
654 | * |
||
655 | * @return mixed |
||
656 | */ |
||
657 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
673 | |||
674 | /** |
||
675 | * Utility function to parse shouldReceive() arguments and generate |
||
676 | * expectations from such as needed. |
||
677 | * |
||
678 | * @param Mockery\MockInterface $mock |
||
679 | * @param array $args |
||
680 | * @param callable $add |
||
681 | * @return \Mockery\CompositeExpectation |
||
682 | */ |
||
683 | 305 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
701 | |||
702 | /** |
||
703 | * Sets up expectations on the members of the CompositeExpectation and |
||
704 | * builds up any demeter chain that was passed to shouldReceive. |
||
705 | * |
||
706 | * @param \Mockery\MockInterface $mock |
||
707 | * @param string $arg |
||
708 | * @param callable $add |
||
709 | * @throws Mockery\Exception |
||
710 | * @return \Mockery\ExpectationInterface |
||
711 | */ |
||
712 | 305 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
763 | |||
764 | /** |
||
765 | * Gets a new demeter configured |
||
766 | * mock from the container. |
||
767 | * |
||
768 | * @param \Mockery\Container $container |
||
769 | * @param string $method |
||
770 | * @param Mockery\ExpectationInterface $exp |
||
771 | * |
||
772 | * @return \Mockery\Mock |
||
773 | */ |
||
774 | 11 | private static function getNewDemeterMock( |
|
784 | |||
785 | /** |
||
786 | * Gets an specific demeter mock from |
||
787 | * the ones kept by the container. |
||
788 | * |
||
789 | * @param \Mockery\Container $container |
||
790 | * @param string $demeterMockKey |
||
791 | * |
||
792 | * @return mixed |
||
793 | */ |
||
794 | 5 | private static function getExistingDemeterMock( |
|
803 | |||
804 | /** |
||
805 | * Checks if the passed array representing a demeter |
||
806 | * chain with the method names is empty. |
||
807 | * |
||
808 | * @param array $methodNames |
||
809 | * |
||
810 | * @return bool |
||
811 | */ |
||
812 | 299 | private static function noMoreElementsInChain(array $methodNames) |
|
816 | |||
817 | 17 | public static function declareClass($fqn) |
|
821 | |||
822 | 2 | public static function declareInterface($fqn) |
|
826 | |||
827 | 18 | private static function declareType($fqn, $type) |
|
853 | |||
854 | /** |
||
855 | * Register a file to be deleted on tearDown. |
||
856 | * |
||
857 | * @param string $fileName |
||
858 | */ |
||
859 | 18 | public static function registerFileForCleanUp($fileName) |
|
863 | } |
||
864 |
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: