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 |
||
29 | class Expectation implements ExpectationInterface |
||
30 | { |
||
31 | /** |
||
32 | * Mock object to which this expectation belongs |
||
33 | * |
||
34 | * @var object |
||
35 | */ |
||
36 | protected $_mock = null; |
||
37 | |||
38 | /** |
||
39 | * Method name |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $_name = null; |
||
44 | |||
45 | /** |
||
46 | * Arguments expected by this expectation |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $_expectedArgs = array(); |
||
51 | |||
52 | /** |
||
53 | * Count validator store |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $_countValidators = array(); |
||
58 | |||
59 | /** |
||
60 | * The count validator class to use |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $_countValidatorClass = 'Mockery\CountValidator\Exact'; |
||
65 | |||
66 | /** |
||
67 | * Actual count of calls to this expectation |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $_actualCount = 0; |
||
72 | |||
73 | /** |
||
74 | * Value to return from this expectation |
||
75 | * |
||
76 | * @var mixed |
||
77 | */ |
||
78 | protected $_returnValue = null; |
||
79 | |||
80 | /** |
||
81 | * Array of return values as a queue for multiple return sequence |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $_returnQueue = array(); |
||
86 | |||
87 | /** |
||
88 | * Array of closures executed with given arguments to generate a result |
||
89 | * to be returned |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $_closureQueue = array(); |
||
94 | |||
95 | /** |
||
96 | * Array of values to be set when this expectation matches |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $_setQueue = array(); |
||
101 | |||
102 | /** |
||
103 | * Integer representing the call order of this expectation |
||
104 | * |
||
105 | * @var int |
||
106 | */ |
||
107 | protected $_orderNumber = null; |
||
108 | |||
109 | /** |
||
110 | * Integer representing the call order of this expectation on a global basis |
||
111 | * |
||
112 | * @var int |
||
113 | */ |
||
114 | protected $_globalOrderNumber = null; |
||
115 | |||
116 | /** |
||
117 | * Flag indicating that an exception is expected to be throw (not returned) |
||
118 | * |
||
119 | * @var bool |
||
120 | */ |
||
121 | protected $_throw = false; |
||
122 | |||
123 | /** |
||
124 | * Flag indicating whether the order of calling is determined locally or |
||
125 | * globally |
||
126 | * |
||
127 | * @var bool |
||
128 | */ |
||
129 | protected $_globally = false; |
||
130 | |||
131 | /** |
||
132 | * Flag indicating if the return value should be obtained from the original |
||
133 | * class method instead of returning predefined values from the return queue |
||
134 | * |
||
135 | * @var bool |
||
136 | */ |
||
137 | protected $_passthru = false; |
||
138 | |||
139 | /** |
||
140 | * Constructor |
||
141 | * |
||
142 | * @param \Mockery\MockInterface $mock |
||
143 | * @param string $name |
||
144 | */ |
||
145 | 327 | public function __construct(\Mockery\MockInterface $mock, $name) |
|
151 | |||
152 | /** |
||
153 | * Return a string with the method name and arguments formatted |
||
154 | * |
||
155 | * @param string $name Name of the expected method |
||
156 | * @param array $args List of arguments to the method |
||
157 | * @return string |
||
158 | */ |
||
159 | 45 | public function __toString() |
|
163 | |||
164 | /** |
||
165 | * Verify the current call, i.e. that the given arguments match those |
||
166 | * of this expectation |
||
167 | * |
||
168 | * @param array $args |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 256 | public function verifyCall(array $args) |
|
185 | |||
186 | /** |
||
187 | * Throws an exception if the expectation has been configured to do so |
||
188 | * |
||
189 | * @throws \Exception|\Throwable |
||
190 | * @return void |
||
191 | */ |
||
192 | 254 | private function throwAsNecessary($return) |
|
208 | |||
209 | /** |
||
210 | * Sets public properties with queued values to the mock object |
||
211 | * |
||
212 | * @param array $args |
||
213 | * @return mixed |
||
214 | */ |
||
215 | 247 | protected function _setValues() |
|
224 | |||
225 | /** |
||
226 | * Fetch the return value for the matching args |
||
227 | * |
||
228 | * @param array $args |
||
229 | * @return mixed |
||
230 | */ |
||
231 | 254 | protected function _getReturnValue(array $args) |
|
245 | |||
246 | /** |
||
247 | * Checks if this expectation is eligible for additional calls |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | 293 | public function isEligible() |
|
260 | |||
261 | /** |
||
262 | * Check if there is a constraint on call count |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function isCallCountConstrained() |
||
270 | |||
271 | /** |
||
272 | * Verify call order |
||
273 | * |
||
274 | * @return void |
||
275 | */ |
||
276 | 256 | public function validateOrder() |
|
286 | |||
287 | /** |
||
288 | * Verify this expectation |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | 148 | public function verify() |
|
298 | |||
299 | /** |
||
300 | * Check if the registered expectation is an ArgumentListMatcher |
||
301 | * @return bool |
||
302 | */ |
||
303 | 300 | private function isArgumentListMatcher() |
|
307 | |||
308 | /** |
||
309 | * Check if passed arguments match an argument expectation |
||
310 | * |
||
311 | * @param array $args |
||
312 | * @return bool |
||
313 | */ |
||
314 | 300 | public function matchArgs(array $args) |
|
332 | |||
333 | /** |
||
334 | * Check if passed argument matches an argument expectation |
||
335 | * |
||
336 | * @param mixed $expected |
||
337 | * @param mixed &$actual |
||
338 | * @return bool |
||
339 | */ |
||
340 | 297 | protected function _matchArg($expected, &$actual) |
|
373 | |||
374 | /** |
||
375 | * Expected argument setter for the expectation |
||
376 | * |
||
377 | * @param mixed ... |
||
378 | * @return self |
||
379 | */ |
||
380 | 129 | public function with() |
|
384 | |||
385 | /** |
||
386 | * Expected arguments for the expectation passed as an array |
||
387 | * |
||
388 | * @param array $arguments |
||
389 | * @return self |
||
390 | */ |
||
391 | 139 | private function withArgsInArray(array $arguments) |
|
399 | |||
400 | /** |
||
401 | * Expected arguments have to be matched by the given closure. |
||
402 | * |
||
403 | * @param Closure $closure |
||
404 | * @return self |
||
405 | */ |
||
406 | 6 | private function withArgsMatchedByClosure(Closure $closure) |
|
411 | |||
412 | /** |
||
413 | * Expected arguments for the expectation passed as an array or a closure that matches each passed argument on |
||
414 | * each function call. |
||
415 | * |
||
416 | * @param array|Closure $argsOrClosure |
||
417 | * @return self |
||
418 | */ |
||
419 | 146 | public function withArgs($argsOrClosure) |
|
431 | |||
432 | /** |
||
433 | * Set with() as no arguments expected |
||
434 | * |
||
435 | * @return self |
||
436 | */ |
||
437 | 7 | public function withNoArgs() |
|
442 | |||
443 | /** |
||
444 | * Set expectation that any arguments are acceptable |
||
445 | * |
||
446 | * @return self |
||
447 | */ |
||
448 | 327 | public function withAnyArgs() |
|
453 | |||
454 | /** |
||
455 | * Set a return value, or sequential queue of return values |
||
456 | * |
||
457 | * @param mixed ... |
||
458 | * @return self |
||
459 | */ |
||
460 | 119 | public function andReturn() |
|
465 | |||
466 | /** |
||
467 | * Set a return value, or sequential queue of return values |
||
468 | * |
||
469 | * @param mixed ... |
||
470 | * @return self |
||
471 | */ |
||
472 | public function andReturns() |
||
476 | |||
477 | /** |
||
478 | * Return this mock, like a fluent interface |
||
479 | * |
||
480 | * @return self |
||
481 | */ |
||
482 | 1 | public function andReturnSelf() |
|
486 | |||
487 | /** |
||
488 | * Set a sequential queue of return values with an array |
||
489 | * |
||
490 | * @param array $values |
||
491 | * @return self |
||
492 | */ |
||
493 | 2 | public function andReturnValues(array $values) |
|
498 | |||
499 | /** |
||
500 | * Set a closure or sequence of closures with which to generate return |
||
501 | * values. The arguments passed to the expected method are passed to the |
||
502 | * closures as parameters. |
||
503 | * |
||
504 | * @param callable ... |
||
505 | * @return self |
||
506 | */ |
||
507 | 1 | public function andReturnUsing() |
|
512 | |||
513 | /** |
||
514 | * Return a self-returning black hole object. |
||
515 | * |
||
516 | * @return self |
||
517 | */ |
||
518 | 1 | public function andReturnUndefined() |
|
523 | |||
524 | /** |
||
525 | * Return null. This is merely a language construct for Mock describing. |
||
526 | * |
||
527 | * @return self |
||
528 | */ |
||
529 | 1 | public function andReturnNull() |
|
533 | |||
534 | 1 | public function andReturnFalse() |
|
538 | |||
539 | 1 | public function andReturnTrue() |
|
543 | |||
544 | /** |
||
545 | * Set Exception class and arguments to that class to be thrown |
||
546 | * |
||
547 | * @param string|\Exception $exception |
||
548 | * @param string $message |
||
549 | * @param int $code |
||
550 | * @param \Exception $previous |
||
551 | * @return self |
||
552 | */ |
||
553 | 7 | public function andThrow($exception, $message = '', $code = 0, \Exception $previous = null) |
|
563 | |||
564 | /** |
||
565 | * Set Exception classes to be thrown |
||
566 | * |
||
567 | * @param array $exceptions |
||
568 | * @return self |
||
569 | */ |
||
570 | 2 | public function andThrowExceptions(array $exceptions) |
|
580 | |||
581 | /** |
||
582 | * Register values to be set to a public property each time this expectation occurs |
||
583 | * |
||
584 | * @param string $name |
||
585 | * @param mixed $value |
||
586 | * @return self |
||
587 | */ |
||
588 | 8 | public function andSet($name, $value) |
|
595 | |||
596 | /** |
||
597 | * Alias to andSet(). Allows the natural English construct |
||
598 | * - set('foo', 'bar')->andReturn('bar') |
||
599 | * |
||
600 | * @param string $name |
||
601 | * @param mixed $value |
||
602 | * @return self |
||
603 | */ |
||
604 | 3 | public function set($name, $value) |
|
608 | |||
609 | /** |
||
610 | * Indicates this expectation should occur zero or more times |
||
611 | * |
||
612 | * @return self |
||
613 | */ |
||
614 | 2 | public function zeroOrMoreTimes() |
|
618 | |||
619 | /** |
||
620 | * Indicates the number of times this expectation should occur |
||
621 | * |
||
622 | * @param int $limit |
||
623 | * @throws InvalidArgumentException |
||
624 | * @return self |
||
625 | */ |
||
626 | 159 | public function times($limit = null) |
|
638 | |||
639 | /** |
||
640 | * Indicates that this expectation is never expected to be called |
||
641 | * |
||
642 | * @return self |
||
643 | */ |
||
644 | 38 | public function never() |
|
648 | |||
649 | /** |
||
650 | * Indicates that this expectation is expected exactly once |
||
651 | * |
||
652 | * @return self |
||
653 | */ |
||
654 | 109 | public function once() |
|
658 | |||
659 | /** |
||
660 | * Indicates that this expectation is expected exactly twice |
||
661 | * |
||
662 | * @return self |
||
663 | */ |
||
664 | 19 | public function twice() |
|
668 | |||
669 | /** |
||
670 | * Sets next count validator to the AtLeast instance |
||
671 | * |
||
672 | * @return self |
||
673 | */ |
||
674 | 15 | public function atLeast() |
|
679 | |||
680 | /** |
||
681 | * Sets next count validator to the AtMost instance |
||
682 | * |
||
683 | * @return self |
||
684 | */ |
||
685 | 7 | public function atMost() |
|
690 | |||
691 | /** |
||
692 | * Shorthand for setting minimum and maximum constraints on call counts |
||
693 | * |
||
694 | * @param int $minimum |
||
695 | * @param int $maximum |
||
696 | */ |
||
697 | public function between($minimum, $maximum) |
||
701 | |||
702 | /** |
||
703 | * Indicates that this expectation must be called in a specific given order |
||
704 | * |
||
705 | * @param string $group Name of the ordered group |
||
706 | * @return self |
||
707 | */ |
||
708 | 17 | public function ordered($group = null) |
|
718 | |||
719 | /** |
||
720 | * Indicates call order should apply globally |
||
721 | * |
||
722 | * @return self |
||
723 | */ |
||
724 | 1 | public function globally() |
|
729 | |||
730 | /** |
||
731 | * Setup the ordering tracking on the mock or mock container |
||
732 | * |
||
733 | * @param string $group |
||
734 | * @param object $ordering |
||
735 | * @return int |
||
736 | */ |
||
737 | 17 | protected function _defineOrdered($group, $ordering) |
|
750 | |||
751 | /** |
||
752 | * Return order number |
||
753 | * |
||
754 | * @return int |
||
755 | */ |
||
756 | 1 | public function getOrderNumber() |
|
760 | |||
761 | /** |
||
762 | * Mark this expectation as being a default |
||
763 | * |
||
764 | * @return self |
||
765 | */ |
||
766 | 35 | public function byDefault() |
|
774 | |||
775 | /** |
||
776 | * Return the parent mock of the expectation |
||
777 | * |
||
778 | * @return \Mockery\MockInterface |
||
779 | */ |
||
780 | 29 | public function getMock() |
|
784 | |||
785 | /** |
||
786 | * Flag this expectation as calling the original class method with the |
||
787 | * any provided arguments instead of using a return value queue. |
||
788 | * |
||
789 | * @return self |
||
790 | */ |
||
791 | 3 | public function passthru() |
|
802 | |||
803 | /** |
||
804 | * Cloning logic |
||
805 | * |
||
806 | */ |
||
807 | 10 | public function __clone() |
|
816 | |||
817 | 7 | public function getName() |
|
821 | } |
||
822 |