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 | 299 | 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 | 43 | 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 | 229 | public function verifyCall(array $args) |
|
188 | |||
189 | /** |
||
190 | * Throws an exception if the expectation has been configured to do so |
||
191 | * |
||
192 | * @throws \Exception|\Throwable |
||
193 | * @return void |
||
194 | */ |
||
195 | 221 | private function throwAsNecessary($return) |
|
211 | 227 | ||
212 | /** |
||
213 | 227 | * Sets public properties with queued values to the mock object |
|
214 | * |
||
215 | 227 | * @param array $args |
|
216 | 1 | * @return mixed |
|
217 | 226 | */ |
|
218 | 5 | protected function _setValues() |
|
227 | |||
228 | /** |
||
229 | * Fetch the return value for the matching args |
||
230 | * |
||
231 | 267 | * @param array $args |
|
232 | * @return mixed |
||
233 | 267 | */ |
|
234 | 138 | protected function _getReturnValue(array $args) |
|
248 | |||
249 | /** |
||
250 | * Checks if this expectation is eligible for additional calls |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function isEligible() |
||
263 | 1 | ||
264 | 1 | /** |
|
265 | 229 | * Check if there is a constraint on call count |
|
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | public function isCallCountConstrained() |
||
273 | |||
274 | 141 | /** |
|
275 | 119 | * Verify call order |
|
276 | 126 | * |
|
277 | 124 | * @return void |
|
278 | */ |
||
279 | public function validateOrder() |
||
289 | |||
290 | /** |
||
291 | * Verify this expectation |
||
292 | * |
||
293 | * @return bool |
||
294 | 273 | */ |
|
295 | public function verify() |
||
301 | |||
302 | 108 | /** |
|
303 | 108 | * Check if the registered expectation is a MultiArgumentClosureExpectation. |
|
304 | 8 | * @return bool |
|
305 | */ |
||
306 | 102 | private function isMultiArgumentClosureExpectation() |
|
310 | |||
311 | 64 | /** |
|
312 | * Check if passed arguments match an argument expectation |
||
313 | 66 | * |
|
314 | * @param array $args |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function matchArgs(array $args) |
||
338 | 6 | ||
339 | 3 | /** |
|
340 | * Check if passed argument matches an argument expectation |
||
341 | 3 | * |
|
342 | 89 | * @param mixed $expected |
|
343 | 1 | * @param mixed &$actual |
|
344 | 1 | * @return bool |
|
345 | 1 | */ |
|
346 | protected function _matchArg($expected, &$actual) |
||
379 | 133 | ||
380 | 133 | /** |
|
381 | 133 | * Expected argument setter for the expectation |
|
382 | * |
||
383 | * @param mixed ... |
||
384 | * @return self |
||
385 | */ |
||
386 | public function with() |
||
390 | 6 | ||
391 | /** |
||
392 | 6 | * Expected arguments for the expectation passed as an array |
|
393 | 6 | * |
|
394 | 6 | * @param array $arguments |
|
395 | * @return self |
||
396 | */ |
||
397 | private function withArgsInArray(array $arguments) |
||
406 | 142 | ||
407 | 135 | /** |
|
408 | 142 | * Expected arguments have to be matched by the given closure. |
|
409 | 6 | * |
|
410 | 6 | * @param Closure $closure |
|
411 | 1 | * @return self |
|
412 | 1 | */ |
|
413 | private function withArgsMatchedByClosure(Closure $closure) |
||
419 | |||
420 | /** |
||
421 | * Expected arguments for the expectation passed as an array or a closure that matches each passed argument on |
||
422 | 6 | * each function call. |
|
423 | * |
||
424 | 6 | * @param array|Closure $argsOrClosure |
|
425 | 6 | * @return self |
|
426 | 6 | */ |
|
427 | public function withArgs($argsOrClosure) |
||
439 | |||
440 | /** |
||
441 | * Set with() as no arguments expected |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | public function withNoArgs() |
||
451 | |||
452 | /** |
||
453 | * Set expectation that any arguments are acceptable |
||
454 | * |
||
455 | * @return self |
||
456 | */ |
||
457 | 1 | public function withAnyArgs() |
|
462 | |||
463 | /** |
||
464 | * Set a return value, or sequential queue of return values |
||
465 | * |
||
466 | * @param mixed ... |
||
467 | * @return self |
||
468 | 2 | */ |
|
469 | public function andReturn() |
||
474 | |||
475 | /** |
||
476 | * Return this mock, like a fluent interface |
||
477 | * |
||
478 | * @return self |
||
479 | */ |
||
480 | public function andReturnSelf() |
||
484 | 1 | ||
485 | 1 | /** |
|
486 | * Set a sequential queue of return values with an array |
||
487 | * |
||
488 | * @param array $values |
||
489 | * @return self |
||
490 | */ |
||
491 | public function andReturnValues(array $values) |
||
496 | 1 | ||
497 | /** |
||
498 | * Set a closure or sequence of closures with which to generate return |
||
499 | * values. The arguments passed to the expected method are passed to the |
||
500 | * closures as parameters. |
||
501 | * |
||
502 | * @param callable ... |
||
503 | * @return self |
||
504 | 1 | */ |
|
505 | public function andReturnUsing() |
||
510 | |||
511 | 1 | /** |
|
512 | * Return a self-returning black hole object. |
||
513 | * |
||
514 | 1 | * @return self |
|
515 | */ |
||
516 | 1 | public function andReturnUndefined() |
|
521 | |||
522 | /** |
||
523 | * Return null. This is merely a language construct for Mock describing. |
||
524 | * |
||
525 | * @return self |
||
526 | */ |
||
527 | public function andReturnNull() |
||
531 | 6 | ||
532 | 2 | public function andReturnFalse() |
|
536 | 6 | ||
537 | public function andReturnTrue() |
||
541 | |||
542 | /** |
||
543 | * Set Exception class and arguments to that class to be thrown |
||
544 | * |
||
545 | 2 | * @param string|\Exception $exception |
|
546 | * @param string $message |
||
547 | 2 | * @param int $code |
|
548 | 2 | * @param \Exception $previous |
|
549 | 2 | * @return self |
|
550 | 1 | */ |
|
551 | public function andThrow($exception, $message = '', $code = 0, \Exception $previous = null) |
||
561 | |||
562 | /** |
||
563 | 8 | * Set Exception classes to be thrown |
|
564 | * |
||
565 | 8 | * @param array $exceptions |
|
566 | 8 | * @return self |
|
567 | 8 | */ |
|
568 | 8 | public function andThrowExceptions(array $exceptions) |
|
578 | |||
579 | 3 | /** |
|
580 | * Register values to be set to a public property each time this expectation occurs |
||
581 | 3 | * |
|
582 | * @param string $name |
||
583 | * @param mixed $value |
||
584 | * @return self |
||
585 | */ |
||
586 | public function andSet($name, $value) |
||
593 | |||
594 | /** |
||
595 | * Alias to andSet(). Allows the natural English construct |
||
596 | * - set('foo', 'bar')->andReturn('bar') |
||
597 | * |
||
598 | * @param string $name |
||
599 | * @param mixed $value |
||
600 | * @return self |
||
601 | 156 | */ |
|
602 | public function set($name, $value) |
||
606 | 156 | ||
607 | 1 | /** |
|
608 | * Indicates this expectation should occur zero or more times |
||
609 | 155 | * |
|
610 | 155 | * @return self |
|
611 | 155 | */ |
|
612 | public function zeroOrMoreTimes() |
||
616 | |||
617 | /** |
||
618 | * Indicates the number of times this expectation should occur |
||
619 | 38 | * |
|
620 | * @param int $limit |
||
621 | 38 | * @throws InvalidArgumentException |
|
622 | * @return self |
||
623 | */ |
||
624 | public function times($limit = null) |
||
636 | |||
637 | /** |
||
638 | * Indicates that this expectation is never expected to be called |
||
639 | 18 | * |
|
640 | * @return self |
||
641 | 18 | */ |
|
642 | public function never() |
||
646 | |||
647 | /** |
||
648 | * Indicates that this expectation is expected exactly once |
||
649 | 14 | * |
|
650 | * @return self |
||
651 | 14 | */ |
|
652 | 14 | public function once() |
|
656 | |||
657 | /** |
||
658 | * Indicates that this expectation is expected exactly twice |
||
659 | * |
||
660 | 7 | * @return self |
|
661 | */ |
||
662 | 7 | public function twice() |
|
666 | |||
667 | /** |
||
668 | * Sets next count validator to the AtLeast instance |
||
669 | * |
||
670 | * @return self |
||
671 | */ |
||
672 | public function atLeast() |
||
677 | |||
678 | /** |
||
679 | * Sets next count validator to the AtMost instance |
||
680 | * |
||
681 | * @return self |
||
682 | */ |
||
683 | 17 | public function atMost() |
|
688 | 16 | ||
689 | /** |
||
690 | 17 | * Shorthand for setting minimum and maximum constraints on call counts |
|
691 | 17 | * |
|
692 | * @param int $minimum |
||
693 | * @param int $maximum |
||
694 | */ |
||
695 | public function between($minimum, $maximum) |
||
699 | 1 | ||
700 | /** |
||
701 | 1 | * Indicates that this expectation must be called in a specific given order |
|
702 | 1 | * |
|
703 | * @param string $group Name of the ordered group |
||
704 | * @return self |
||
705 | */ |
||
706 | public function ordered($group = null) |
||
716 | 16 | ||
717 | 17 | /** |
|
718 | 2 | * Indicates call order should apply globally |
|
719 | 2 | * |
|
720 | 4 | * @return self |
|
721 | 4 | */ |
|
722 | public function globally() |
||
727 | |||
728 | /** |
||
729 | * Setup the ordering tracking on the mock or mock container |
||
730 | * |
||
731 | 1 | * @param string $group |
|
732 | * @param object $ordering |
||
733 | 1 | * @return int |
|
734 | */ |
||
735 | protected function _defineOrdered($group, $ordering) |
||
748 | |||
749 | /** |
||
750 | * Return order number |
||
751 | * |
||
752 | * @return int |
||
753 | */ |
||
754 | public function getOrderNumber() |
||
758 | |||
759 | /** |
||
760 | * Mark this expectation as being a default |
||
761 | * |
||
762 | * @return self |
||
763 | */ |
||
764 | public function byDefault() |
||
772 | |||
773 | /** |
||
774 | 3 | * Return the parent mock of the expectation |
|
775 | 3 | * |
|
776 | * @return \Mockery\MockInterface |
||
777 | */ |
||
778 | public function getMock() |
||
782 | 9 | ||
783 | /** |
||
784 | 9 | * Flag this expectation as calling the original class method with the |
|
785 | 9 | * any provided arguments instead of using a return value queue. |
|
786 | 9 | * |
|
787 | 6 | * @return self |
|
788 | 9 | */ |
|
789 | 9 | public function passthru() |
|
800 | |||
801 | /** |
||
802 | * Cloning logic |
||
803 | * |
||
804 | */ |
||
805 | public function __clone() |
||
814 | |||
815 | public function getName() |
||
819 | } |
||
820 |