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 | 53 | 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 | 9 | 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 | 416 | public static function close() |
|
160 | { |
||
161 | 416 | foreach (self::$_filesToCleanUp as $fileName) { |
|
162 | 18 | @unlink($fileName); |
|
163 | } |
||
164 | 416 | self::$_filesToCleanUp = []; |
|
165 | |||
166 | 416 | if (is_null(self::$_container)) { |
|
167 | return; |
||
168 | } |
||
169 | |||
170 | 416 | self::$_container->mockery_teardown(); |
|
171 | 414 | self::$_container->mockery_close(); |
|
172 | 414 | self::$_container = null; |
|
173 | 414 | } |
|
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 | 453 | public static function getContainer() |
|
194 | { |
||
195 | 453 | if (is_null(self::$_container)) { |
|
196 | 409 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
197 | } |
||
198 | |||
199 | 453 | return self::$_container; |
|
200 | } |
||
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 | 409 | public static function getGenerator() |
|
219 | { |
||
220 | 409 | if (is_null(self::$_generator)) { |
|
221 | 1 | self::$_generator = self::getDefaultGenerator(); |
|
222 | } |
||
223 | |||
224 | 409 | return self::$_generator; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Creates and returns a default generator |
||
229 | * used inside this class. |
||
230 | * |
||
231 | * @return CachingGenerator |
||
232 | */ |
||
233 | 409 | 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 | 409 | public static function getLoader() |
|
255 | { |
||
256 | 409 | if (is_null(self::$_loader)) { |
|
257 | 1 | self::$_loader = self::getDefaultLoader(); |
|
258 | } |
||
259 | |||
260 | 409 | return self::$_loader; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Gets an EvalLoader to be used as default. |
||
265 | * |
||
266 | * @return EvalLoader |
||
267 | */ |
||
268 | 286 | 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 | 14 | 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 | 47 | public static function type($expected) |
|
313 | { |
||
314 | 47 | return new \Mockery\Matcher\Type($expected); |
|
315 | } |
||
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 | 6 | 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 | 446 | public static function getConfiguration() |
|
436 | { |
||
437 | 446 | if (is_null(self::$_config)) { |
|
438 | 1 | self::$_config = new \Mockery\Configuration(); |
|
439 | } |
||
440 | |||
441 | 446 | return self::$_config; |
|
442 | } |
||
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) |
|
453 | { |
||
454 | 92 | if (is_null($arguments)) { |
|
455 | return $method . '()'; |
||
456 | } |
||
457 | |||
458 | 92 | $formattedArguments = array(); |
|
459 | 92 | foreach ($arguments as $argument) { |
|
460 | 58 | $formattedArguments[] = self::formatArgument($argument); |
|
461 | } |
||
462 | |||
463 | 92 | return $method . '(' . implode(', ', $formattedArguments) . ')'; |
|
464 | } |
||
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) |
|
476 | { |
||
477 | 58 | if (is_object($argument)) { |
|
478 | 8 | return 'object(' . get_class($argument) . ')'; |
|
479 | } |
||
480 | |||
481 | 54 | if (is_int($argument) || is_float($argument)) { |
|
482 | 37 | return $argument; |
|
483 | } |
||
484 | |||
485 | 26 | if (is_array($argument)) { |
|
486 | 13 | if ($depth === 1) { |
|
487 | 2 | $argument = '[...]'; |
|
488 | } else { |
||
489 | 13 | $sample = array(); |
|
490 | 13 | foreach ($argument as $key => $value) { |
|
491 | 12 | $key = is_int($key) ? $key : "'$key'"; |
|
492 | 12 | $value = self::formatArgument($value, $depth + 1); |
|
493 | 12 | $sample[] = "$key => $value"; |
|
494 | } |
||
495 | |||
496 | 13 | $argument = "[".implode(", ", $sample)."]"; |
|
497 | } |
||
498 | |||
499 | 13 | return ((strlen($argument) > 1000) ? substr($argument, 0, 1000).'...]' : $argument); |
|
500 | } |
||
501 | |||
502 | 17 | if (is_bool($argument)) { |
|
503 | 1 | return $argument ? 'true' : 'false'; |
|
504 | } |
||
505 | |||
506 | 16 | if (is_resource($argument)) { |
|
507 | 2 | return 'resource(...)'; |
|
508 | } |
||
509 | |||
510 | 14 | if (is_null($argument)) { |
|
511 | 1 | return 'NULL'; |
|
512 | } |
||
513 | |||
514 | 13 | return "'".(string) $argument."'"; |
|
515 | } |
||
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) |
|
525 | { |
||
526 | 52 | static $formatting; |
|
527 | |||
528 | 52 | if ($formatting) { |
|
529 | 1 | return '[Recursion]'; |
|
530 | } |
||
531 | |||
532 | 52 | if (is_null($objects)) { |
|
533 | return ''; |
||
534 | } |
||
535 | |||
536 | 52 | $objects = array_filter($objects, 'is_object'); |
|
537 | 52 | if (empty($objects)) { |
|
538 | 45 | return ''; |
|
539 | } |
||
540 | |||
541 | 7 | $formatting = true; |
|
542 | 7 | $parts = array(); |
|
543 | |||
544 | 7 | foreach ($objects as $object) { |
|
545 | 7 | $parts[get_class($object)] = self::objectToArray($object); |
|
546 | } |
||
547 | |||
548 | 7 | $formatting = false; |
|
549 | |||
550 | 7 | return 'Objects: ( ' . var_export($parts, true) . ')'; |
|
551 | } |
||
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) |
|
562 | { |
||
563 | 7 | if ($nesting == 0) { |
|
564 | return array('...'); |
||
565 | } |
||
566 | |||
567 | return array( |
||
568 | 7 | 'class' => get_class($object), |
|
569 | 7 | 'properties' => self::extractInstancePublicProperties($object, $nesting), |
|
570 | 7 | 'getters' => self::extractGetters($object, $nesting) |
|
571 | ); |
||
572 | } |
||
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) |
|
583 | { |
||
584 | 7 | $reflection = new \ReflectionClass(get_class($object)); |
|
585 | 7 | $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); |
|
586 | 7 | $cleanedProperties = array(); |
|
587 | |||
588 | 7 | foreach ($properties as $publicProperty) { |
|
589 | 1 | if (!$publicProperty->isStatic()) { |
|
590 | $name = $publicProperty->getName(); |
||
591 | 1 | $cleanedProperties[$name] = self::cleanupNesting($object->$name, $nesting); |
|
592 | } |
||
593 | } |
||
594 | |||
595 | 7 | return $cleanedProperties; |
|
596 | } |
||
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) |
|
607 | { |
||
608 | 7 | $reflection = new \ReflectionClass(get_class($object)); |
|
609 | 7 | $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
610 | 7 | $getters = array(); |
|
611 | |||
612 | 7 | foreach ($publicMethods as $publicMethod) { |
|
613 | 5 | $name = $publicMethod->getName(); |
|
614 | 5 | $irrelevantName = (substr($name, 0, 3) !== 'get' && substr($name, 0, 2) !== 'is'); |
|
615 | 5 | $isStatic = $publicMethod->isStatic(); |
|
616 | 5 | $numberOfParameters = $publicMethod->getNumberOfParameters(); |
|
617 | |||
618 | 5 | if ($irrelevantName || $numberOfParameters != 0 || $isStatic) { |
|
619 | 5 | continue; |
|
620 | } |
||
621 | |||
622 | try { |
||
623 | 2 | $getters[$name] = self::cleanupNesting($object->$name(), $nesting); |
|
624 | 1 | } catch (\Exception $e) { |
|
625 | 2 | $getters[$name] = '!! ' . get_class($e) . ': ' . $e->getMessage() . ' !!'; |
|
626 | } |
||
627 | } |
||
628 | |||
629 | 7 | return $getters; |
|
630 | } |
||
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) |
|
668 | { |
||
669 | 1 | if ($nesting == 0) { |
|
670 | 1 | return '...'; |
|
671 | } |
||
672 | |||
673 | 1 | foreach ($argument as $key => $value) { |
|
674 | 1 | if (is_array($value)) { |
|
675 | 1 | $argument[$key] = self::cleanupArray($value, $nesting - 1); |
|
676 | } elseif (is_object($value)) { |
||
677 | 1 | $argument[$key] = self::objectToArray($value, $nesting - 1); |
|
678 | } |
||
679 | } |
||
680 | |||
681 | 1 | return $argument; |
|
682 | } |
||
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 | 323 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
694 | { |
||
695 | 323 | $composite = new \Mockery\CompositeExpectation(); |
|
696 | |||
697 | 323 | foreach ($args as $arg) { |
|
698 | 323 | if (is_array($arg)) { |
|
699 | 16 | foreach ($arg as $k => $v) { |
|
700 | 16 | $expectation = self::buildDemeterChain($mock, $k, $add)->andReturn($v); |
|
701 | 16 | $composite->add($expectation); |
|
702 | } |
||
703 | } elseif (is_string($arg)) { |
||
704 | 311 | $expectation = self::buildDemeterChain($mock, $arg, $add); |
|
705 | 317 | $composite->add($expectation); |
|
706 | } |
||
707 | } |
||
708 | |||
709 | 317 | return $composite; |
|
710 | } |
||
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 | 323 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
723 | { |
||
724 | /** @var Mockery\Container $container */ |
||
725 | 323 | $container = $mock->mockery_getContainer(); |
|
726 | 323 | $methodNames = explode('->', $arg); |
|
727 | 323 | reset($methodNames); |
|
728 | |||
729 | 323 | if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() |
|
730 | 323 | && !$mock->mockery_isAnonymous() |
|
731 | 323 | && !in_array(current($methodNames), $mock->mockery_getMockableMethods()) |
|
732 | ) { |
||
733 | 4 | throw new \Mockery\Exception( |
|
734 | 'Mockery\'s configuration currently forbids mocking the method ' |
||
735 | 4 | . current($methodNames) . ' as it does not exist on the class or object ' |
|
736 | 4 | . 'being mocked' |
|
737 | ); |
||
738 | } |
||
739 | |||
740 | /** @var ExpectationInterface|null $expectations */ |
||
741 | 319 | $expectations = null; |
|
742 | |||
743 | /** @var Callable $nextExp */ |
||
744 | $nextExp = function ($method) use ($add) { |
||
745 | 319 | return $add($method); |
|
746 | 319 | }; |
|
747 | |||
748 | 319 | while (true) { |
|
749 | 319 | $method = array_shift($methodNames); |
|
750 | 319 | $expectations = $mock->mockery_getExpectationsFor($method); |
|
751 | |||
752 | 319 | if (is_null($expectations) || self::noMoreElementsInChain($methodNames)) { |
|
753 | 319 | $expectations = $nextExp($method); |
|
754 | 317 | if (self::noMoreElementsInChain($methodNames)) { |
|
755 | 317 | break; |
|
756 | } |
||
757 | |||
758 | 11 | $mock = self::getNewDemeterMock($container, $method, $expectations); |
|
759 | } else { |
||
760 | 5 | $demeterMockKey = $container->getKeyOfDemeterMockFor($method); |
|
761 | 5 | if ($demeterMockKey) { |
|
762 | 5 | $mock = self::getExistingDemeterMock($container, $demeterMockKey); |
|
763 | } |
||
764 | } |
||
765 | |||
766 | 11 | $nextExp = function ($n) use ($mock) { |
|
767 | 11 | return $mock->shouldReceive($n); |
|
768 | 11 | }; |
|
769 | } |
||
770 | |||
771 | 317 | return $expectations; |
|
772 | } |
||
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 | 317 | 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) |
|
838 | { |
||
839 | 18 | $targetCode = "<?php "; |
|
840 | 18 | $shortName = $fqn; |
|
841 | |||
842 | 18 | if (strpos($fqn, "\\")) { |
|
843 | 4 | $parts = explode("\\", $fqn); |
|
844 | |||
845 | 4 | $shortName = trim(array_pop($parts)); |
|
846 | 4 | $namespace = implode("\\", $parts); |
|
847 | |||
848 | 4 | $targetCode.= "namespace $namespace;\n"; |
|
849 | } |
||
850 | |||
851 | 18 | $targetCode.= "$type $shortName {} "; |
|
852 | |||
853 | /* |
||
854 | * We could eval here, but it doesn't play well with the way |
||
855 | * PHPUnit tries to backup global state and the require definition |
||
856 | * loader |
||
857 | */ |
||
858 | 18 | $tmpfname = tempnam(sys_get_temp_dir(), "Mockery"); |
|
859 | 18 | file_put_contents($tmpfname, $targetCode); |
|
860 | 18 | require $tmpfname; |
|
861 | 18 | \Mockery::registerFileForCleanUp($tmpfname); |
|
862 | 18 | } |
|
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: