Complex classes like Expectation 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 Expectation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Expectation implements ExpectationInterface |
||
27 | { |
||
28 | /** |
||
29 | * Mock object to which this expectation belongs |
||
30 | * |
||
31 | * @var object |
||
32 | */ |
||
33 | protected $_mock = null; |
||
34 | |||
35 | /** |
||
36 | * Method name |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $_name = null; |
||
41 | |||
42 | /** |
||
43 | * Arguments expected by this expectation |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $_expectedArgs = array(); |
||
48 | |||
49 | /** |
||
50 | * Count validator store |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $_countValidators = array(); |
||
55 | |||
56 | /** |
||
57 | * The count validator class to use |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $_countValidatorClass = 'Mockery\CountValidator\Exact'; |
||
62 | |||
63 | /** |
||
64 | * Actual count of calls to this expectation |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $_actualCount = 0; |
||
69 | |||
70 | /** |
||
71 | * Value to return from this expectation |
||
72 | * |
||
73 | * @var mixed |
||
74 | */ |
||
75 | protected $_returnValue = null; |
||
76 | |||
77 | /** |
||
78 | * Array of return values as a queue for multiple return sequence |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $_returnQueue = array(); |
||
83 | |||
84 | /** |
||
85 | * Array of closures executed with given arguments to generate a result |
||
86 | * to be returned |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $_closureQueue = array(); |
||
91 | |||
92 | /** |
||
93 | * Array of values to be set when this expectation matches |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $_setQueue = array(); |
||
98 | |||
99 | /** |
||
100 | * Integer representing the call order of this expectation |
||
101 | * |
||
102 | * @var int |
||
103 | */ |
||
104 | protected $_orderNumber = null; |
||
105 | |||
106 | /** |
||
107 | * Integer representing the call order of this expectation on a global basis |
||
108 | * |
||
109 | * @var int |
||
110 | */ |
||
111 | protected $_globalOrderNumber = null; |
||
112 | |||
113 | /** |
||
114 | * Flag indicating that an exception is expected to be throw (not returned) |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | protected $_throw = false; |
||
119 | |||
120 | /** |
||
121 | * Flag indicating whether the order of calling is determined locally or |
||
122 | * globally |
||
123 | * |
||
124 | * @var bool |
||
125 | */ |
||
126 | protected $_globally = false; |
||
127 | |||
128 | /** |
||
129 | * Flag indicating we expect no arguments |
||
130 | * |
||
131 | * @var bool |
||
132 | */ |
||
133 | protected $_noArgsExpectation = false; |
||
134 | |||
135 | /** |
||
136 | * Flag indicating if the return value should be obtained from the original |
||
137 | * class method instead of returning predefined values from the return queue |
||
138 | * |
||
139 | * @var bool |
||
140 | */ |
||
141 | protected $_passthru = false; |
||
142 | |||
143 | /** |
||
144 | * Constructor |
||
145 | * |
||
146 | * @param \Mockery\MockInterface $mock |
||
147 | * @param string $name |
||
148 | */ |
||
149 | 306 | public function __construct(\Mockery\MockInterface $mock, $name) |
|
154 | |||
155 | /** |
||
156 | * Return a string with the method name and arguments formatted |
||
157 | * |
||
158 | * @param string $name Name of the expected method |
||
159 | * @param array $args List of arguments to the method |
||
160 | * @return string |
||
161 | */ |
||
162 | 45 | public function __toString() |
|
166 | |||
167 | /** |
||
168 | * Verify the current call, i.e. that the given arguments match those |
||
169 | * of this expectation |
||
170 | * |
||
171 | * @param array $args |
||
172 | * @return mixed |
||
173 | */ |
||
174 | 235 | public function verifyCall(array $args) |
|
188 | |||
189 | /** |
||
190 | * Sets public properties with queued values to the mock object |
||
191 | * |
||
192 | * @param array $args |
||
|
|||
193 | * @return mixed |
||
194 | */ |
||
195 | 227 | protected function _setValues() |
|
204 | |||
205 | /** |
||
206 | * Fetch the return value for the matching args |
||
207 | * |
||
208 | * @param array $args |
||
209 | * @return mixed |
||
210 | */ |
||
211 | 233 | protected function _getReturnValue(array $args) |
|
212 | { |
||
213 | 233 | if (count($this->_closureQueue) > 1) { |
|
214 | return call_user_func_array(array_shift($this->_closureQueue), $args); |
||
215 | 233 | } elseif (count($this->_closureQueue) > 0) { |
|
216 | 1 | return call_user_func_array(current($this->_closureQueue), $args); |
|
217 | 232 | } elseif (count($this->_returnQueue) > 1) { |
|
218 | 5 | return array_shift($this->_returnQueue); |
|
219 | 231 | } elseif (count($this->_returnQueue) > 0) { |
|
220 | 93 | return current($this->_returnQueue); |
|
221 | } |
||
222 | |||
223 | 139 | return $this->_mock->mockery_returnValueForMethod($this->_name); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Checks if this expectation is eligible for additional calls |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | 273 | public function isEligible() |
|
232 | { |
||
233 | 273 | foreach ($this->_countValidators as $validator) { |
|
234 | 139 | if (!$validator->isEligible($this->_actualCount)) { |
|
235 | 22 | return false; |
|
236 | } |
||
237 | 270 | } |
|
238 | 270 | return true; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Check if there is a constraint on call count |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isCallCountConstrained() |
||
247 | { |
||
248 | return (count($this->_countValidators) > 0); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Verify call order |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | 235 | public function validateOrder() |
|
266 | |||
267 | /** |
||
268 | * Verify this expectation |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 144 | public function verify() |
|
278 | |||
279 | /** |
||
280 | * Check if the registered expectation is a MultiArgumentClosureExpectation. |
||
281 | * @return bool |
||
282 | */ |
||
283 | 116 | private function isMultiArgumentClosureExpectation() |
|
287 | |||
288 | /** |
||
289 | * Check if passed arguments match an argument expectation |
||
290 | * |
||
291 | * @param array $args |
||
292 | * @return bool |
||
293 | */ |
||
294 | 279 | public function matchArgs(array $args) |
|
315 | |||
316 | /** |
||
317 | * Check if passed argument matches an argument expectation |
||
318 | * |
||
319 | * @param mixed $expected |
||
320 | * @param mixed &$actual |
||
321 | * @return bool |
||
322 | */ |
||
323 | 108 | protected function _matchArg($expected, &$actual) |
|
324 | { |
||
325 | 108 | if ($expected === $actual) { |
|
326 | 30 | return true; |
|
327 | } |
||
328 | 90 | if (!is_object($expected) && !is_object($actual) && $expected == $actual) { |
|
329 | return true; |
||
330 | } |
||
331 | 90 | if (is_string($expected) && !is_array($actual) && !is_object($actual)) { |
|
332 | # push/pop an error handler here to to make sure no error/exception thrown if $expected is not a regex |
||
333 | 6 | set_error_handler(function () { |
|
334 | 6 | }); |
|
335 | 6 | $result = preg_match($expected, (string) $actual); |
|
336 | 6 | restore_error_handler(); |
|
337 | |||
338 | 6 | if ($result) { |
|
339 | 3 | return true; |
|
340 | } |
||
341 | 3 | } |
|
342 | 89 | if (is_string($expected) && is_object($actual)) { |
|
343 | 1 | $result = $actual instanceof $expected; |
|
344 | 1 | if ($result) { |
|
345 | 1 | return true; |
|
346 | } |
||
347 | } |
||
348 | 88 | if ($expected instanceof \Mockery\Matcher\MatcherAbstract) { |
|
349 | 67 | return $expected->match($actual); |
|
350 | } |
||
351 | 21 | if ($expected instanceof \Hamcrest\Matcher || $expected instanceof \Hamcrest_Matcher) { |
|
352 | 3 | return $expected->matches($actual); |
|
353 | } |
||
354 | 18 | return false; |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * Expected argument setter for the expectation |
||
359 | * |
||
360 | * @param mixed ... |
||
361 | * @return self |
||
362 | */ |
||
363 | 130 | public function with() |
|
367 | |||
368 | /** |
||
369 | * Expected arguments for the expectation passed as an array |
||
370 | * |
||
371 | * @param array $arguments |
||
372 | * @return self |
||
373 | */ |
||
374 | 139 | private function withArgsInArray(array $arguments) |
|
383 | |||
384 | /** |
||
385 | * Expected arguments have to be matched by the given closure. |
||
386 | * |
||
387 | * @param Closure $closure |
||
388 | * @return self |
||
389 | */ |
||
390 | 6 | private function withArgsMatchedByClosure(Closure $closure) |
|
396 | |||
397 | /** |
||
398 | * Expected arguments for the expectation passed as an array or a closure that matches each passed argument on |
||
399 | * each function call. |
||
400 | * |
||
401 | * @param array|Closure $argsOrClosure |
||
402 | * @return self |
||
403 | */ |
||
404 | 146 | public function withArgs($argsOrClosure) |
|
416 | |||
417 | /** |
||
418 | * Set with() as no arguments expected |
||
419 | * |
||
420 | * @return self |
||
421 | */ |
||
422 | 7 | public function withNoArgs() |
|
428 | |||
429 | /** |
||
430 | * Set expectation that any arguments are acceptable |
||
431 | * |
||
432 | * @return self |
||
433 | */ |
||
434 | 3 | public function withAnyArgs() |
|
440 | |||
441 | /** |
||
442 | * Set a return value, or sequential queue of return values |
||
443 | * |
||
444 | * @param mixed ... |
||
445 | * @return self |
||
446 | */ |
||
447 | 106 | public function andReturn() |
|
452 | |||
453 | /** |
||
454 | * Set a return value, or sequential queue of return values |
||
455 | * |
||
456 | * @param mixed ... |
||
457 | * @return self |
||
458 | */ |
||
459 | public function andReturns() |
||
463 | |||
464 | /** |
||
465 | * Return this mock, like a fluent interface |
||
466 | * |
||
467 | * @return self |
||
468 | */ |
||
469 | 1 | public function andReturnSelf() |
|
473 | |||
474 | /** |
||
475 | * Set a sequential queue of return values with an array |
||
476 | * |
||
477 | * @param array $values |
||
478 | * @return self |
||
479 | */ |
||
480 | 2 | public function andReturnValues(array $values) |
|
485 | |||
486 | /** |
||
487 | * Set a closure or sequence of closures with which to generate return |
||
488 | * values. The arguments passed to the expected method are passed to the |
||
489 | * closures as parameters. |
||
490 | * |
||
491 | * @param callable ... |
||
492 | * @return self |
||
493 | */ |
||
494 | 1 | public function andReturnUsing() |
|
499 | |||
500 | /** |
||
501 | * Return a self-returning black hole object. |
||
502 | * |
||
503 | * @return self |
||
504 | */ |
||
505 | 1 | public function andReturnUndefined() |
|
510 | |||
511 | /** |
||
512 | * Return null. This is merely a language construct for Mock describing. |
||
513 | * |
||
514 | * @return self |
||
515 | */ |
||
516 | 1 | public function andReturnNull() |
|
520 | |||
521 | 1 | public function andReturnFalse() |
|
525 | |||
526 | 1 | public function andReturnTrue() |
|
530 | |||
531 | /** |
||
532 | * Set Exception class and arguments to that class to be thrown |
||
533 | * |
||
534 | * @param string|\Exception $exception |
||
535 | * @param string $message |
||
536 | * @param int $code |
||
537 | * @param \Exception $previous |
||
538 | * @return self |
||
539 | */ |
||
540 | 6 | public function andThrow($exception, $message = '', $code = 0, \Exception $previous = null) |
|
550 | |||
551 | /** |
||
552 | * Set Exception classes to be thrown |
||
553 | * |
||
554 | * @param array $exceptions |
||
555 | * @return self |
||
556 | */ |
||
557 | 2 | public function andThrowExceptions(array $exceptions) |
|
567 | |||
568 | /** |
||
569 | * Register values to be set to a public property each time this expectation occurs |
||
570 | * |
||
571 | * @param string $name |
||
572 | * @param mixed $value |
||
573 | * @return self |
||
574 | */ |
||
575 | 8 | public function andSet($name, $value) |
|
582 | |||
583 | /** |
||
584 | * Alias to andSet(). Allows the natural English construct |
||
585 | * - set('foo', 'bar')->andReturn('bar') |
||
586 | * |
||
587 | * @param string $name |
||
588 | * @param mixed $value |
||
589 | * @return self |
||
590 | */ |
||
591 | 3 | public function set($name, $value) |
|
595 | |||
596 | /** |
||
597 | * Indicates this expectation should occur zero or more times |
||
598 | * |
||
599 | * @return self |
||
600 | */ |
||
601 | 2 | public function zeroOrMoreTimes() |
|
605 | |||
606 | /** |
||
607 | * Indicates the number of times this expectation should occur |
||
608 | * |
||
609 | * @param int $limit |
||
610 | * @throws InvalidArgumentException |
||
611 | * @return self |
||
612 | */ |
||
613 | 158 | public function times($limit = null) |
|
625 | |||
626 | /** |
||
627 | * Indicates that this expectation is never expected to be called |
||
628 | * |
||
629 | * @return self |
||
630 | */ |
||
631 | 38 | public function never() |
|
635 | |||
636 | /** |
||
637 | * Indicates that this expectation is expected exactly once |
||
638 | * |
||
639 | * @return self |
||
640 | */ |
||
641 | 108 | public function once() |
|
645 | |||
646 | /** |
||
647 | * Indicates that this expectation is expected exactly twice |
||
648 | * |
||
649 | * @return self |
||
650 | */ |
||
651 | 19 | public function twice() |
|
655 | |||
656 | /** |
||
657 | * Sets next count validator to the AtLeast instance |
||
658 | * |
||
659 | * @return self |
||
660 | */ |
||
661 | 14 | public function atLeast() |
|
666 | |||
667 | /** |
||
668 | * Sets next count validator to the AtMost instance |
||
669 | * |
||
670 | * @return self |
||
671 | */ |
||
672 | 7 | public function atMost() |
|
677 | |||
678 | /** |
||
679 | * Shorthand for setting minimum and maximum constraints on call counts |
||
680 | * |
||
681 | * @param int $minimum |
||
682 | * @param int $maximum |
||
683 | */ |
||
684 | public function between($minimum, $maximum) |
||
688 | |||
689 | /** |
||
690 | * Indicates that this expectation must be called in a specific given order |
||
691 | * |
||
692 | * @param string $group Name of the ordered group |
||
693 | * @return self |
||
694 | */ |
||
695 | 17 | public function ordered($group = null) |
|
705 | |||
706 | /** |
||
707 | * Indicates call order should apply globally |
||
708 | * |
||
709 | * @return self |
||
710 | */ |
||
711 | 1 | public function globally() |
|
716 | |||
717 | /** |
||
718 | * Setup the ordering tracking on the mock or mock container |
||
719 | * |
||
720 | * @param string $group |
||
721 | * @param object $ordering |
||
722 | * @return int |
||
723 | */ |
||
724 | 17 | protected function _defineOrdered($group, $ordering) |
|
737 | |||
738 | /** |
||
739 | * Return order number |
||
740 | * |
||
741 | * @return int |
||
742 | */ |
||
743 | 1 | public function getOrderNumber() |
|
747 | |||
748 | /** |
||
749 | * Mark this expectation as being a default |
||
750 | * |
||
751 | * @return self |
||
752 | */ |
||
753 | 25 | public function byDefault() |
|
761 | |||
762 | /** |
||
763 | * Return the parent mock of the expectation |
||
764 | * |
||
765 | * @return \Mockery\MockInterface |
||
766 | */ |
||
767 | 29 | public function getMock() |
|
771 | |||
772 | /** |
||
773 | * Flag this expectation as calling the original class method with the |
||
774 | * any provided arguments instead of using a return value queue. |
||
775 | * |
||
776 | * @return self |
||
777 | */ |
||
778 | 3 | public function passthru() |
|
789 | |||
790 | /** |
||
791 | * Cloning logic |
||
792 | * |
||
793 | */ |
||
794 | 9 | public function __clone() |
|
803 | |||
804 | 6 | public function getName() |
|
808 | } |
||
809 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.