Complex classes like Mock 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 Mock, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Mock implements MockInterface |
||
26 | { |
||
27 | /** |
||
28 | * Stores an array of all expectation directors for this mock |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $_mockery_expectations = array(); |
||
33 | |||
34 | /** |
||
35 | * Stores an inital number of expectations that can be manipulated |
||
36 | * while using the getter method. |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $_mockery_expectations_count = 0; |
||
41 | |||
42 | /** |
||
43 | * Flag to indicate whether we can ignore method calls missing from our |
||
44 | * expectations |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $_mockery_ignoreMissing = false; |
||
49 | |||
50 | /** |
||
51 | * Flag to indicate whether we can defer method calls missing from our |
||
52 | * expectations |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $_mockery_deferMissing = false; |
||
57 | |||
58 | /** |
||
59 | * Flag to indicate whether this mock was verified |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $_mockery_verified = false; |
||
64 | |||
65 | /** |
||
66 | * Given name of the mock |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $_mockery_name = null; |
||
71 | |||
72 | /** |
||
73 | * Order number of allocation |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $_mockery_allocatedOrder = 0; |
||
78 | |||
79 | /** |
||
80 | * Current ordered number |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | protected $_mockery_currentOrder = 0; |
||
85 | |||
86 | /** |
||
87 | * Ordered groups |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $_mockery_groups = array(); |
||
92 | |||
93 | /** |
||
94 | * Mock container containing this mock object |
||
95 | * |
||
96 | * @var \Mockery\Container |
||
97 | */ |
||
98 | protected $_mockery_container = null; |
||
99 | |||
100 | /** |
||
101 | * Instance of a core object on which methods are called in the event |
||
102 | * it has been set, and an expectation for one of the object's methods |
||
103 | * does not exist. This implements a simple partial mock proxy system. |
||
104 | * |
||
105 | * @var object |
||
106 | */ |
||
107 | protected $_mockery_partial = null; |
||
108 | |||
109 | /** |
||
110 | * Flag to indicate we should ignore all expectations temporarily. Used |
||
111 | * mainly to prevent expectation matching when in the middle of a mock |
||
112 | * object recording session. |
||
113 | * |
||
114 | * @var bool |
||
115 | */ |
||
116 | protected $_mockery_disableExpectationMatching = false; |
||
117 | |||
118 | /** |
||
119 | * Stores all stubbed public methods separate from any on-object public |
||
120 | * properties that may exist. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $_mockery_mockableProperties = array(); |
||
125 | |||
126 | /** |
||
127 | * @var array |
||
128 | */ |
||
129 | protected $_mockery_mockableMethods = array(); |
||
130 | |||
131 | /** |
||
132 | * Just a local cache for this mock's target's methods |
||
133 | * |
||
134 | * @var ReflectionMethod[] |
||
135 | */ |
||
136 | protected static $_mockery_methods; |
||
137 | |||
138 | protected $_mockery_allowMockingProtectedMethods = false; |
||
139 | |||
140 | protected $_mockery_receivedMethodCalls; |
||
141 | |||
142 | /** |
||
143 | * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
||
144 | * @var mixed |
||
145 | */ |
||
146 | protected $_mockery_defaultReturnValue = null; |
||
147 | |||
148 | /** |
||
149 | * We want to avoid constructors since class is copied to Generator.php |
||
150 | * for inclusion on extending class definitions. |
||
151 | * |
||
152 | * @param \Mockery\Container $container |
||
153 | * @param object $partialObject |
||
154 | * @return void |
||
155 | */ |
||
156 | public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
||
174 | |||
175 | /** |
||
176 | * Set expected method calls |
||
177 | * |
||
178 | * @param string $methodName,... one or many methods that are expected to be called in this mock |
||
179 | * @return \Mockery\Expectation |
||
180 | */ |
||
181 | public function shouldReceive($methodName) |
||
223 | |||
224 | /** |
||
225 | * Shortcut method for setting an expectation that a method should not be called. |
||
226 | * |
||
227 | * @param string $methodName,... one or many methods that are expected not to be called in this mock |
||
228 | * @return \Mockery\Expectation |
||
229 | */ |
||
230 | public function shouldNotReceive($methodName) |
||
231 | { |
||
232 | $expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args()); |
||
233 | $expectation->never(); |
||
234 | return $expectation; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Allows additional methods to be mocked that do not explicitly exist on mocked class |
||
239 | * @param String $method name of the method to be mocked |
||
240 | * @return Mock |
||
241 | */ |
||
242 | public function shouldAllowMockingMethod($method) |
||
247 | |||
248 | /** |
||
249 | * Set mock to ignore unexpected methods and return Undefined class |
||
250 | * @param mixed $returnValue the default return value for calls to missing functions on this mock |
||
251 | * @return Mock |
||
252 | */ |
||
253 | public function shouldIgnoreMissing($returnValue = null) |
||
259 | |||
260 | public function asUndefined() |
||
266 | |||
267 | /** |
||
268 | * @return Mock |
||
269 | */ |
||
270 | public function shouldAllowMockingProtectedMethods() |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Set mock to defer unexpected methods to it's parent |
||
279 | * |
||
280 | * This is particularly useless for this class, as it doesn't have a parent, |
||
281 | * but included for completeness |
||
282 | * |
||
283 | * @return Mock |
||
284 | */ |
||
285 | public function shouldDeferMissing() |
||
290 | |||
291 | /** |
||
292 | * Create an obviously worded alias to shouldDeferMissing() |
||
293 | * |
||
294 | * @return Mock |
||
295 | */ |
||
296 | public function makePartial() |
||
300 | |||
301 | /** |
||
302 | * Accepts a closure which is executed with an object recorder which proxies |
||
303 | * to the partial source object. The intent being to record the |
||
304 | * interactions of a concrete object as a set of expectations on the |
||
305 | * current mock object. The partial may then be passed to a second process |
||
306 | * to see if it fulfils the same (or exact same) contract as the original. |
||
307 | * |
||
308 | * @param Closure $closure |
||
309 | */ |
||
310 | public function shouldExpect(\Closure $closure) |
||
318 | |||
319 | /** |
||
320 | * In the event shouldReceive() accepting one or more methods/returns, |
||
321 | * this method will switch them from normal expectations to default |
||
322 | * expectations |
||
323 | * |
||
324 | * @return self |
||
325 | */ |
||
326 | public function byDefault() |
||
336 | |||
337 | /** |
||
338 | * Capture calls to this mock |
||
339 | */ |
||
340 | public function __call($method, array $args) |
||
344 | |||
345 | public static function __callStatic($method, array $args) |
||
349 | |||
350 | /** |
||
351 | * Forward calls to this magic method to the __call method |
||
352 | */ |
||
353 | public function __toString() |
||
357 | |||
358 | /** |
||
359 | * Iterate across all expectation directors and validate each |
||
360 | * |
||
361 | * @throws \Mockery\CountValidator\Exception |
||
362 | * @return void |
||
363 | */ |
||
364 | public function mockery_verify() |
||
378 | |||
379 | /** |
||
380 | * Tear down tasks for this mock |
||
381 | * |
||
382 | * @return void |
||
383 | */ |
||
384 | public function mockery_teardown() |
||
387 | |||
388 | /** |
||
389 | * Fetch the next available allocation order number |
||
390 | * |
||
391 | * @return int |
||
392 | */ |
||
393 | public function mockery_allocateOrder() |
||
398 | |||
399 | /** |
||
400 | * Set ordering for a group |
||
401 | * |
||
402 | * @param mixed $group |
||
403 | * @param int $order |
||
404 | */ |
||
405 | public function mockery_setGroup($group, $order) |
||
409 | |||
410 | /** |
||
411 | * Fetch array of ordered groups |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | public function mockery_getGroups() |
||
419 | |||
420 | /** |
||
421 | * Set current ordered number |
||
422 | * |
||
423 | * @param int $order |
||
424 | */ |
||
425 | public function mockery_setCurrentOrder($order) |
||
430 | |||
431 | /** |
||
432 | * Get current ordered number |
||
433 | * |
||
434 | * @return int |
||
435 | */ |
||
436 | public function mockery_getCurrentOrder() |
||
440 | |||
441 | /** |
||
442 | * Validate the current mock's ordering |
||
443 | * |
||
444 | * @param string $method |
||
445 | * @param int $order |
||
446 | * @throws \Mockery\Exception |
||
447 | * @return void |
||
448 | */ |
||
449 | public function mockery_validateOrder($method, $order) |
||
465 | |||
466 | /** |
||
467 | * Gets the count of expectations for this mock |
||
468 | * |
||
469 | * @return int |
||
470 | */ |
||
471 | public function mockery_getExpectationCount() |
||
479 | |||
480 | /** |
||
481 | * Return the expectations director for the given method |
||
482 | * |
||
483 | * @var string $method |
||
484 | * @return \Mockery\ExpectationDirector|null |
||
485 | */ |
||
486 | public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
||
490 | |||
491 | /** |
||
492 | * Return the expectations director for the given method |
||
493 | * |
||
494 | * @var string $method |
||
495 | * @return \Mockery\ExpectationDirector|null |
||
496 | */ |
||
497 | public function mockery_getExpectationsFor($method) |
||
503 | |||
504 | /** |
||
505 | * Find an expectation matching the given method and arguments |
||
506 | * |
||
507 | * @var string $method |
||
508 | * @var array $args |
||
509 | * @return \Mockery\Expectation|null |
||
510 | */ |
||
511 | public function mockery_findExpectation($method, array $args) |
||
520 | |||
521 | /** |
||
522 | * Return the container for this mock |
||
523 | * |
||
524 | * @return \Mockery\Container |
||
525 | */ |
||
526 | public function mockery_getContainer() |
||
530 | |||
531 | /** |
||
532 | * Return the name for this mock |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | public function mockery_getName() |
||
540 | |||
541 | /** |
||
542 | * @return array |
||
543 | */ |
||
544 | public function mockery_getMockableProperties() |
||
548 | |||
549 | public function __isset($name) |
||
555 | |||
556 | public function mockery_getExpectations() |
||
560 | |||
561 | /** |
||
562 | * Calls a parent class method and returns the result. Used in a passthru |
||
563 | * expectation where a real return value is required while still taking |
||
564 | * advantage of expectation matching and call count verification. |
||
565 | * |
||
566 | * @param string $name |
||
567 | * @param array $args |
||
568 | * @return mixed |
||
569 | */ |
||
570 | public function mockery_callSubjectMethod($name, array $args) |
||
574 | |||
575 | /** |
||
576 | * @return string[] |
||
577 | */ |
||
578 | public function mockery_getMockableMethods() |
||
582 | |||
583 | /** |
||
584 | * @return bool |
||
585 | */ |
||
586 | public function mockery_isAnonymous() |
||
592 | |||
593 | public function __wakeup() |
||
602 | |||
603 | public function __destruct() |
||
609 | |||
610 | public function mockery_getMethod($name) |
||
620 | |||
621 | /** |
||
622 | * @param string $name Method name. |
||
623 | * |
||
624 | * @return mixed Generated return value based on the declared return value of the named method. |
||
625 | */ |
||
626 | public function mockery_returnValueForMethod($name) |
||
663 | |||
664 | public function shouldHaveReceived($method, $args = null) |
||
676 | |||
677 | public function shouldNotHaveReceived($method, $args = null) |
||
689 | |||
690 | protected static function _mockery_handleStaticMethodCall($method, array $args) |
||
702 | |||
703 | protected function _mockery_getReceivedMethodCalls() |
||
707 | |||
708 | protected function _mockery_handleMethodCall($method, array $args) |
||
709 | { |
||
710 | $this->_mockery_getReceivedMethodCalls()->push(new \Mockery\MethodCall($method, $args)); |
||
711 | |||
712 | $rm = $this->mockery_getMethod($method); |
||
713 | if ($rm && $rm->isProtected() && !$this->_mockery_allowMockingProtectedMethods) { |
||
714 | if ($rm->isAbstract()) { |
||
715 | return; |
||
716 | } |
||
717 | |||
718 | try { |
||
719 | $prototype = $rm->getPrototype(); |
||
720 | if ($prototype->isAbstract()) { |
||
721 | return; |
||
722 | } |
||
723 | } catch (\ReflectionException $re) { |
||
724 | // noop - there is no hasPrototype method |
||
725 | } |
||
726 | |||
727 | return call_user_func_array("parent::$method", $args); |
||
728 | } |
||
729 | |||
730 | if (isset($this->_mockery_expectations[$method]) |
||
731 | && !$this->_mockery_disableExpectationMatching) { |
||
732 | $handler = $this->_mockery_expectations[$method]; |
||
733 | |||
734 | try { |
||
735 | return $handler->call($args); |
||
736 | } catch (\Mockery\Exception\NoMatchingExpectationException $e) { |
||
737 | if (!$this->_mockery_ignoreMissing && !$this->_mockery_deferMissing) { |
||
738 | throw $e; |
||
739 | } |
||
740 | } |
||
741 | } |
||
742 | |||
743 | if (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) { |
||
744 | return call_user_func_array(array($this->_mockery_partial, $method), $args); |
||
745 | } elseif ($this->_mockery_deferMissing && is_callable("parent::$method") && !$this->hasMethodOverloadingInParentClass()) { |
||
746 | return call_user_func_array("parent::$method", $args); |
||
747 | } elseif ($method == '__toString') { |
||
748 | // __toString is special because we force its addition to the class API regardless of the |
||
749 | // original implementation. Thus, we should always return a string rather than honor |
||
750 | // _mockery_ignoreMissing and break the API with an error. |
||
751 | return sprintf("%s#%s", __CLASS__, spl_object_hash($this)); |
||
752 | } elseif ($this->_mockery_ignoreMissing) { |
||
753 | if (\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() || (method_exists($this->_mockery_partial, $method) || is_callable("parent::$method"))) { |
||
754 | if ($this->_mockery_defaultReturnValue instanceof \Mockery\Undefined) { |
||
755 | return call_user_func_array(array($this->_mockery_defaultReturnValue, $method), $args); |
||
756 | } elseif (null === $this->_mockery_defaultReturnValue) { |
||
757 | return $this->mockery_returnValueForMethod($method); |
||
758 | } else { |
||
759 | return $this->_mockery_defaultReturnValue; |
||
760 | } |
||
761 | } |
||
762 | } |
||
763 | |||
764 | $message = 'Method ' . __CLASS__ . '::' . $method . |
||
765 | '() does not exist on this mock object'; |
||
766 | |||
767 | if (!is_null($rm)) { |
||
768 | $message = 'Received ' . __CLASS__ . |
||
769 | '::' . $method . '(), but no expectations were specified'; |
||
770 | } |
||
771 | |||
772 | throw new \BadMethodCallException( |
||
773 | $message |
||
774 | ); |
||
775 | } |
||
776 | |||
777 | /** |
||
778 | * Uses reflection to get the list of all |
||
779 | * methods within the current mock object |
||
780 | * |
||
781 | * @return array |
||
782 | */ |
||
783 | protected function mockery_getMethods() |
||
797 | |||
798 | private function hasMethodOverloadingInParentClass() |
||
803 | |||
804 | /** |
||
805 | * @return array |
||
806 | */ |
||
807 | private function getNonPublicMethods() |
||
818 | } |
||
819 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: