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 | 432 | public static function close() |
|
149 | { |
||
150 | 432 | foreach (self::$_filesToCleanUp as $fileName) { |
|
151 | 18 | @unlink($fileName); |
|
152 | } |
||
153 | 432 | self::$_filesToCleanUp = []; |
|
154 | |||
155 | 432 | if (is_null(self::$_container)) { |
|
156 | return; |
||
157 | } |
||
158 | |||
159 | 432 | self::$_container->mockery_teardown(); |
|
160 | 432 | self::$_container->mockery_close(); |
|
161 | 432 | self::$_container = null; |
|
162 | 432 | } |
|
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 | 461 | public static function getContainer() |
|
183 | { |
||
184 | 461 | if (is_null(self::$_container)) { |
|
185 | 427 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
186 | } |
||
187 | |||
188 | 461 | 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 | 427 | public static function getGenerator() |
|
208 | { |
||
209 | 427 | if (is_null(self::$_generator)) { |
|
210 | 1 | self::$_generator = self::getDefaultGenerator(); |
|
211 | } |
||
212 | |||
213 | 427 | return self::$_generator; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Creates and returns a default generator |
||
218 | * used inside this class. |
||
219 | * |
||
220 | * @return CachingGenerator |
||
221 | */ |
||
222 | 427 | public static function getDefaultGenerator() |
|
223 | { |
||
224 | 427 | $generator = new StringManipulationGenerator(array( |
|
225 | 427 | new CallTypeHintPass(), |
|
226 | 427 | new MagicMethodTypeHintsPass(), |
|
227 | 427 | new ClassPass(), |
|
228 | 427 | new ClassNamePass(), |
|
229 | 427 | new InstanceMockPass(), |
|
230 | 427 | new InterfacePass(), |
|
231 | 427 | new MethodDefinitionPass(), |
|
232 | 427 | new RemoveUnserializeForInternalSerializableClassesPass(), |
|
233 | 427 | new RemoveBuiltinMethodsThatAreFinalPass(), |
|
234 | 427 | new RemoveDestructorPass(), |
|
235 | )); |
||
236 | |||
237 | 427 | return new CachingGenerator($generator); |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Setter for the $_loader static property. |
||
242 | * |
||
243 | * @param Loader $loader |
||
244 | */ |
||
245 | public static function setLoader(Loader $loader) |
||
249 | |||
250 | /** |
||
251 | * Lazy loader method and getter for |
||
252 | * the $_loader property. |
||
253 | * |
||
254 | * @return Loader |
||
255 | */ |
||
256 | 427 | public static function getLoader() |
|
257 | { |
||
258 | 427 | if (is_null(self::$_loader)) { |
|
259 | 1 | self::$_loader = self::getDefaultLoader(); |
|
260 | } |
||
261 | |||
262 | 427 | return self::$_loader; |
|
263 | } |
||
264 | |||
265 | /** |
||
266 | * Gets an EvalLoader to be used as default. |
||
267 | * |
||
268 | * @return EvalLoader |
||
269 | */ |
||
270 | 302 | public static function getDefaultLoader() |
|
274 | |||
275 | /** |
||
276 | * Set the container. |
||
277 | * |
||
278 | * @param \Mockery\Container $container |
||
279 | * |
||
280 | * @return \Mockery\Container |
||
281 | */ |
||
282 | 18 | public static function setContainer(Mockery\Container $container) |
|
286 | |||
287 | /** |
||
288 | * Reset the container to null. |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | 14 | public static function resetContainer() |
|
296 | |||
297 | /** |
||
298 | * Return instance of ANY matcher. |
||
299 | * |
||
300 | * @return \Mockery\Matcher\Any |
||
301 | */ |
||
302 | 5 | public static function any() |
|
306 | |||
307 | /** |
||
308 | * Return instance of TYPE matcher. |
||
309 | * |
||
310 | * @param $expected |
||
311 | * |
||
312 | * @return \Mockery\Matcher\Type |
||
313 | */ |
||
314 | 47 | public static function type($expected) |
|
318 | |||
319 | /** |
||
320 | * Return instance of DUCKTYPE matcher. |
||
321 | * |
||
322 | * @return \Mockery\Matcher\Ducktype |
||
323 | */ |
||
324 | 3 | public static function ducktype() |
|
328 | |||
329 | /** |
||
330 | * Return instance of SUBSET matcher. |
||
331 | * |
||
332 | * @param array $part |
||
333 | * |
||
334 | * @return \Mockery\Matcher\Subset |
||
335 | */ |
||
336 | 3 | public static function subset(array $part) |
|
340 | |||
341 | /** |
||
342 | * Return instance of CONTAINS matcher. |
||
343 | * |
||
344 | * @return \Mockery\Matcher\Contains |
||
345 | */ |
||
346 | 3 | public static function contains() |
|
350 | |||
351 | /** |
||
352 | * Return instance of HASKEY matcher. |
||
353 | * |
||
354 | * @param $key |
||
355 | * |
||
356 | * @return \Mockery\Matcher\HasKey |
||
357 | */ |
||
358 | 3 | 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 | * @return \Mockery\Matcher\AnyOf |
||
415 | */ |
||
416 | 3 | public static function anyOf() |
|
420 | |||
421 | /** |
||
422 | * Return instance of NOTANYOF matcher. |
||
423 | * |
||
424 | * @return \Mockery\Matcher\NotAnyOf |
||
425 | */ |
||
426 | 3 | public static function notAnyOf() |
|
430 | |||
431 | /** |
||
432 | * Lazy loader and Getter for the global |
||
433 | * configuration container. |
||
434 | * |
||
435 | * @return \Mockery\Configuration |
||
436 | */ |
||
437 | 454 | public static function getConfiguration() |
|
438 | { |
||
439 | 454 | if (is_null(self::$_config)) { |
|
440 | 1 | self::$_config = new \Mockery\Configuration(); |
|
441 | } |
||
442 | |||
443 | 454 | return self::$_config; |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * Utility method to format method name and arguments into a string. |
||
448 | * |
||
449 | * @param string $method |
||
450 | * @param array $arguments |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | 90 | public static function formatArgs($method, array $arguments = null) |
|
455 | { |
||
456 | 90 | if (is_null($arguments)) { |
|
457 | return $method . '()'; |
||
458 | } |
||
459 | |||
460 | 90 | $formattedArguments = array(); |
|
461 | 90 | foreach ($arguments as $argument) { |
|
462 | 56 | $formattedArguments[] = self::formatArgument($argument); |
|
463 | } |
||
464 | |||
465 | 90 | return $method . '(' . implode(', ', $formattedArguments) . ')'; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * Gets the string representation |
||
470 | * of any passed argument. |
||
471 | * |
||
472 | * @param $argument |
||
473 | * @param $depth |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | 56 | private static function formatArgument($argument, $depth = 0) |
|
478 | { |
||
479 | 56 | if (is_object($argument)) { |
|
480 | 8 | return 'object(' . get_class($argument) . ')'; |
|
481 | } |
||
482 | |||
483 | 52 | if (is_int($argument) || is_float($argument)) { |
|
484 | 35 | return $argument; |
|
485 | } |
||
486 | |||
487 | 26 | if (is_array($argument)) { |
|
488 | 13 | if ($depth === 1) { |
|
489 | 2 | $argument = '[...]'; |
|
490 | } else { |
||
491 | 13 | $sample = array(); |
|
492 | 13 | foreach ($argument as $key => $value) { |
|
493 | 12 | $key = is_int($key) ? $key : "'$key'"; |
|
494 | 12 | $value = self::formatArgument($value, $depth + 1); |
|
495 | 12 | $sample[] = "$key => $value"; |
|
496 | } |
||
497 | |||
498 | 13 | $argument = "[".implode(", ", $sample)."]"; |
|
499 | } |
||
500 | |||
501 | 13 | return ((strlen($argument) > 1000) ? substr($argument, 0, 1000).'...]' : $argument); |
|
502 | } |
||
503 | |||
504 | 17 | if (is_bool($argument)) { |
|
505 | 1 | return $argument ? 'true' : 'false'; |
|
506 | } |
||
507 | |||
508 | 16 | if (is_resource($argument)) { |
|
509 | 2 | return 'resource(...)'; |
|
510 | } |
||
511 | |||
512 | 14 | if (is_null($argument)) { |
|
513 | 1 | return 'NULL'; |
|
514 | } |
||
515 | |||
516 | 13 | return "'".(string) $argument."'"; |
|
517 | } |
||
518 | |||
519 | /** |
||
520 | * Utility function to format objects to printable arrays. |
||
521 | * |
||
522 | * @param array $objects |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | 52 | public static function formatObjects(array $objects = null) |
|
527 | { |
||
528 | 52 | static $formatting; |
|
529 | |||
530 | 52 | if ($formatting) { |
|
531 | 1 | return '[Recursion]'; |
|
532 | } |
||
533 | |||
534 | 52 | if (is_null($objects)) { |
|
535 | return ''; |
||
536 | } |
||
537 | |||
538 | 52 | $objects = array_filter($objects, 'is_object'); |
|
539 | 52 | if (empty($objects)) { |
|
540 | 45 | return ''; |
|
541 | } |
||
542 | |||
543 | 7 | $formatting = true; |
|
544 | 7 | $parts = array(); |
|
545 | |||
546 | 7 | foreach ($objects as $object) { |
|
547 | 7 | $parts[get_class($object)] = self::objectToArray($object); |
|
548 | } |
||
549 | |||
550 | 7 | $formatting = false; |
|
551 | |||
552 | 7 | return 'Objects: ( ' . var_export($parts, true) . ')'; |
|
553 | } |
||
554 | |||
555 | /** |
||
556 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
557 | * |
||
558 | * @param $object |
||
559 | * @param int $nesting |
||
560 | * |
||
561 | * @return array |
||
562 | */ |
||
563 | 7 | private static function objectToArray($object, $nesting = 3) |
|
564 | { |
||
565 | 7 | if ($nesting == 0) { |
|
566 | return array('...'); |
||
567 | } |
||
568 | |||
569 | return array( |
||
570 | 7 | 'class' => get_class($object), |
|
571 | 7 | 'properties' => self::extractInstancePublicProperties($object, $nesting), |
|
572 | 7 | 'getters' => self::extractGetters($object, $nesting) |
|
573 | ); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Returns all public instance properties. |
||
578 | * |
||
579 | * @param $object |
||
580 | * @param $nesting |
||
581 | * |
||
582 | * @return array |
||
583 | */ |
||
584 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
585 | { |
||
586 | 7 | $reflection = new \ReflectionClass(get_class($object)); |
|
587 | 7 | $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); |
|
588 | 7 | $cleanedProperties = array(); |
|
589 | |||
590 | 7 | foreach ($properties as $publicProperty) { |
|
591 | 1 | if (!$publicProperty->isStatic()) { |
|
592 | $name = $publicProperty->getName(); |
||
593 | 1 | $cleanedProperties[$name] = self::cleanupNesting($object->$name, $nesting); |
|
594 | } |
||
595 | } |
||
596 | |||
597 | 7 | return $cleanedProperties; |
|
598 | } |
||
599 | |||
600 | /** |
||
601 | * Returns all object getters. |
||
602 | * |
||
603 | * @param $object |
||
604 | * @param $nesting |
||
605 | * |
||
606 | * @return array |
||
607 | */ |
||
608 | 7 | private static function extractGetters($object, $nesting) |
|
609 | { |
||
610 | 7 | $reflection = new \ReflectionClass(get_class($object)); |
|
611 | 7 | $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
612 | 7 | $getters = array(); |
|
613 | |||
614 | 7 | foreach ($publicMethods as $publicMethod) { |
|
615 | 5 | $name = $publicMethod->getName(); |
|
616 | 5 | $irrelevantName = (substr($name, 0, 3) !== 'get' && substr($name, 0, 2) !== 'is'); |
|
617 | 5 | $isStatic = $publicMethod->isStatic(); |
|
618 | 5 | $numberOfParameters = $publicMethod->getNumberOfParameters(); |
|
619 | |||
620 | 5 | if ($irrelevantName || $numberOfParameters != 0 || $isStatic) { |
|
621 | 5 | continue; |
|
622 | } |
||
623 | |||
624 | try { |
||
625 | 2 | $getters[$name] = self::cleanupNesting($object->$name(), $nesting); |
|
626 | 1 | } catch (\Exception $e) { |
|
627 | 2 | $getters[$name] = '!! ' . get_class($e) . ': ' . $e->getMessage() . ' !!'; |
|
628 | } |
||
629 | } |
||
630 | |||
631 | 7 | return $getters; |
|
632 | } |
||
633 | |||
634 | /** |
||
635 | * Utility method used for recursively generating |
||
636 | * an object or array representation. |
||
637 | * |
||
638 | * @param $argument |
||
639 | * @param $nesting |
||
640 | * |
||
641 | * @return mixed |
||
642 | */ |
||
643 | 1 | private static function cleanupNesting($argument, $nesting) |
|
658 | |||
659 | /** |
||
660 | * Utility method for recursively |
||
661 | * gerating a representation |
||
662 | * of the given array. |
||
663 | * |
||
664 | * @param array $argument |
||
665 | * @param int $nesting |
||
666 | * |
||
667 | * @return mixed |
||
668 | */ |
||
669 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
670 | { |
||
671 | 1 | if ($nesting == 0) { |
|
672 | 1 | return '...'; |
|
673 | } |
||
674 | |||
675 | 1 | foreach ($argument as $key => $value) { |
|
676 | 1 | if (is_array($value)) { |
|
677 | 1 | $argument[$key] = self::cleanupArray($value, $nesting - 1); |
|
678 | } elseif (is_object($value)) { |
||
679 | 1 | $argument[$key] = self::objectToArray($value, $nesting - 1); |
|
680 | } |
||
681 | } |
||
682 | |||
683 | 1 | return $argument; |
|
684 | } |
||
685 | |||
686 | /** |
||
687 | * Utility function to parse shouldReceive() arguments and generate |
||
688 | * expectations from such as needed. |
||
689 | * |
||
690 | * @param Mockery\MockInterface $mock |
||
691 | * @param array $args |
||
692 | * @param callable $add |
||
693 | * @return \Mockery\CompositeExpectation |
||
694 | */ |
||
695 | 329 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
696 | { |
||
697 | 329 | $composite = new \Mockery\CompositeExpectation(); |
|
698 | |||
699 | 329 | foreach ($args as $arg) { |
|
700 | 329 | if (is_array($arg)) { |
|
701 | 16 | foreach ($arg as $k => $v) { |
|
702 | 16 | $expectation = self::buildDemeterChain($mock, $k, $add)->andReturn($v); |
|
703 | 16 | $composite->add($expectation); |
|
704 | } |
||
705 | } elseif (is_string($arg)) { |
||
706 | 317 | $expectation = self::buildDemeterChain($mock, $arg, $add); |
|
707 | 323 | $composite->add($expectation); |
|
708 | } |
||
709 | } |
||
710 | |||
711 | 323 | return $composite; |
|
712 | } |
||
713 | |||
714 | /** |
||
715 | * Sets up expectations on the members of the CompositeExpectation and |
||
716 | * builds up any demeter chain that was passed to shouldReceive. |
||
717 | * |
||
718 | * @param \Mockery\MockInterface $mock |
||
719 | * @param string $arg |
||
720 | * @param callable $add |
||
721 | * @throws Mockery\Exception |
||
722 | * @return \Mockery\ExpectationInterface |
||
723 | */ |
||
724 | 329 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
725 | { |
||
726 | /** @var Mockery\Container $container */ |
||
727 | 329 | $container = $mock->mockery_getContainer(); |
|
728 | 329 | $methodNames = explode('->', $arg); |
|
729 | 329 | reset($methodNames); |
|
730 | |||
731 | 329 | if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() |
|
732 | 329 | && !$mock->mockery_isAnonymous() |
|
733 | 329 | && !in_array(current($methodNames), $mock->mockery_getMockableMethods()) |
|
734 | ) { |
||
735 | 4 | throw new \Mockery\Exception( |
|
736 | 'Mockery\'s configuration currently forbids mocking the method ' |
||
737 | 4 | . current($methodNames) . ' as it does not exist on the class or object ' |
|
738 | 4 | . 'being mocked' |
|
739 | ); |
||
740 | } |
||
741 | |||
742 | /** @var ExpectationInterface|null $expectations */ |
||
743 | 325 | $expectations = null; |
|
744 | |||
745 | /** @var Callable $nextExp */ |
||
746 | $nextExp = function ($method) use ($add) { |
||
747 | 325 | return $add($method); |
|
748 | 325 | }; |
|
749 | |||
750 | 325 | while (true) { |
|
751 | 325 | $method = array_shift($methodNames); |
|
752 | 325 | $expectations = $mock->mockery_getExpectationsFor($method); |
|
753 | |||
754 | 325 | if (is_null($expectations) || self::noMoreElementsInChain($methodNames)) { |
|
755 | 325 | $expectations = $nextExp($method); |
|
756 | 323 | if (self::noMoreElementsInChain($methodNames)) { |
|
757 | 323 | break; |
|
758 | } |
||
759 | |||
760 | 11 | $mock = self::getNewDemeterMock($container, $method, $expectations); |
|
761 | } else { |
||
762 | 5 | $demeterMockKey = $container->getKeyOfDemeterMockFor($method); |
|
763 | 5 | if ($demeterMockKey) { |
|
764 | 5 | $mock = self::getExistingDemeterMock($container, $demeterMockKey); |
|
765 | } |
||
766 | } |
||
767 | |||
768 | 11 | $nextExp = function ($n) use ($mock) { |
|
769 | 11 | return $mock->shouldReceive($n); |
|
770 | 11 | }; |
|
771 | } |
||
772 | |||
773 | 323 | return $expectations; |
|
774 | } |
||
775 | |||
776 | /** |
||
777 | * Gets a new demeter configured |
||
778 | * mock from the container. |
||
779 | * |
||
780 | * @param \Mockery\Container $container |
||
781 | * @param string $method |
||
782 | * @param Mockery\ExpectationInterface $exp |
||
783 | * |
||
784 | * @return \Mockery\Mock |
||
785 | */ |
||
786 | 11 | private static function getNewDemeterMock( |
|
787 | Mockery\Container $container, |
||
788 | $method, |
||
789 | Mockery\ExpectationInterface $exp |
||
790 | ) { |
||
791 | 11 | $mock = $container->mock('demeter_' . $method); |
|
792 | 11 | $exp->andReturn($mock); |
|
793 | |||
794 | 11 | return $mock; |
|
795 | } |
||
796 | |||
797 | /** |
||
798 | * Gets an specific demeter mock from |
||
799 | * the ones kept by the container. |
||
800 | * |
||
801 | * @param \Mockery\Container $container |
||
802 | * @param string $demeterMockKey |
||
803 | * |
||
804 | * @return mixed |
||
805 | */ |
||
806 | 5 | private static function getExistingDemeterMock( |
|
807 | Mockery\Container $container, |
||
808 | $demeterMockKey |
||
809 | ) { |
||
810 | 5 | $mocks = $container->getMocks(); |
|
811 | 5 | $mock = $mocks[$demeterMockKey]; |
|
812 | |||
813 | 5 | return $mock; |
|
814 | } |
||
815 | |||
816 | /** |
||
817 | * Checks if the passed array representing a demeter |
||
818 | * chain with the method names is empty. |
||
819 | * |
||
820 | * @param array $methodNames |
||
821 | * |
||
822 | * @return bool |
||
823 | */ |
||
824 | 323 | private static function noMoreElementsInChain(array $methodNames) |
|
828 | |||
829 | 17 | public static function declareClass($fqn) |
|
830 | { |
||
833 | |||
834 | 2 | public static function declareInterface($fqn) |
|
838 | |||
839 | 18 | private static function declareType($fqn, $type) |
|
840 | { |
||
841 | 18 | $targetCode = "<?php "; |
|
842 | 18 | $shortName = $fqn; |
|
843 | |||
844 | 18 | if (strpos($fqn, "\\")) { |
|
845 | 4 | $parts = explode("\\", $fqn); |
|
846 | |||
847 | 4 | $shortName = trim(array_pop($parts)); |
|
848 | 4 | $namespace = implode("\\", $parts); |
|
849 | |||
850 | 4 | $targetCode.= "namespace $namespace;\n"; |
|
851 | } |
||
852 | |||
853 | 18 | $targetCode.= "$type $shortName {} "; |
|
854 | |||
855 | /* |
||
856 | * We could eval here, but it doesn't play well with the way |
||
857 | * PHPUnit tries to backup global state and the require definition |
||
858 | * loader |
||
859 | */ |
||
860 | 18 | $tmpfname = tempnam(sys_get_temp_dir(), "Mockery"); |
|
861 | 18 | file_put_contents($tmpfname, $targetCode); |
|
862 | 18 | require $tmpfname; |
|
863 | 18 | \Mockery::registerFileForCleanUp($tmpfname); |
|
864 | 18 | } |
|
865 | |||
866 | /** |
||
867 | * Register a file to be deleted on tearDown. |
||
868 | * |
||
869 | * @param string $fileName |
||
870 | */ |
||
871 | 18 | public static function registerFileForCleanUp($fileName) |
|
875 | } |
||
876 |
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: