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 |
||
| 27 | class Mock implements MockInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Stores an array of all expectation directors for this mock |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $_mockery_expectations = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Stores an inital number of expectations that can be manipulated |
||
| 38 | * while using the getter method. |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $_mockery_expectations_count = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Flag to indicate whether we can ignore method calls missing from our |
||
| 46 | * expectations |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $_mockery_ignoreMissing = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Flag to indicate whether we can defer method calls missing from our |
||
| 54 | * expectations |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $_mockery_deferMissing = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Flag to indicate whether this mock was verified |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $_mockery_verified = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Given name of the mock |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $_mockery_name = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Order number of allocation |
||
| 76 | * |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | protected $_mockery_allocatedOrder = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Current ordered number |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $_mockery_currentOrder = 0; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Ordered groups |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $_mockery_groups = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Mock container containing this mock object |
||
| 97 | * |
||
| 98 | * @var \Mockery\Container |
||
| 99 | */ |
||
| 100 | protected $_mockery_container = null; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instance of a core object on which methods are called in the event |
||
| 104 | * it has been set, and an expectation for one of the object's methods |
||
| 105 | * does not exist. This implements a simple partial mock proxy system. |
||
| 106 | * |
||
| 107 | * @var object |
||
| 108 | */ |
||
| 109 | protected $_mockery_partial = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Flag to indicate we should ignore all expectations temporarily. Used |
||
| 113 | * mainly to prevent expectation matching when in the middle of a mock |
||
| 114 | * object recording session. |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | protected $_mockery_disableExpectationMatching = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Stores all stubbed public methods separate from any on-object public |
||
| 122 | * properties that may exist. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $_mockery_mockableProperties = array(); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | protected $_mockery_mockableMethods = array(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Just a local cache for this mock's target's methods |
||
| 135 | * |
||
| 136 | * @var ReflectionMethod[] |
||
| 137 | */ |
||
| 138 | protected static $_mockery_methods; |
||
| 139 | |||
| 140 | protected $_mockery_allowMockingProtectedMethods = false; |
||
| 141 | |||
| 142 | protected $_mockery_receivedMethodCalls; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
||
| 146 | * @var mixed |
||
| 147 | */ |
||
| 148 | protected $_mockery_defaultReturnValue = null; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * We want to avoid constructors since class is copied to Generator.php |
||
| 152 | * for inclusion on extending class definitions. |
||
| 153 | * |
||
| 154 | * @param \Mockery\Container $container |
||
| 155 | * @param object $partialObject |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set expected method calls |
||
| 179 | * |
||
| 180 | * @param null|string $methodName,... one or many methods that are expected to be called in this mock |
||
| 181 | * @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage |
||
| 182 | */ |
||
| 183 | public function shouldReceive($methodName = null) |
||
| 225 | |||
| 226 | // start method allows |
||
| 227 | /** |
||
| 228 | * @return HigherOrderMessage |
||
| 229 | */ |
||
| 230 | public function allows(array $stubs = []) |
||
| 242 | // end method allows |
||
| 243 | |||
| 244 | // start method expects |
||
| 245 | /** |
||
| 246 | * @return ExpectsHigherOrderMessage |
||
| 247 | */ |
||
| 248 | public function expects() |
||
| 252 | // end method expects |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Shortcut method for setting an expectation that a method should not be called. |
||
| 257 | * |
||
| 258 | * @param null|string $methodName,... one or many methods that are expected not to be called in this mock |
||
| 259 | * @return \Mockery\Expectation|\Mockery\HigherOrderMessage |
||
| 260 | */ |
||
| 261 | public function shouldNotReceive($methodName = null) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Allows additional methods to be mocked that do not explicitly exist on mocked class |
||
| 274 | * @param String $method name of the method to be mocked |
||
| 275 | * @return Mock |
||
| 276 | */ |
||
| 277 | public function shouldAllowMockingMethod($method) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set mock to ignore unexpected methods and return Undefined class |
||
| 285 | * @param mixed $returnValue the default return value for calls to missing functions on this mock |
||
| 286 | * @return Mock |
||
| 287 | */ |
||
| 288 | public function shouldIgnoreMissing($returnValue = null) |
||
| 294 | |||
| 295 | public function asUndefined() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return Mock |
||
| 304 | */ |
||
| 305 | public function shouldAllowMockingProtectedMethods() |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Set mock to defer unexpected methods to it's parent |
||
| 314 | * |
||
| 315 | * This is particularly useless for this class, as it doesn't have a parent, |
||
| 316 | * but included for completeness |
||
| 317 | * |
||
| 318 | * @return Mock |
||
| 319 | */ |
||
| 320 | public function shouldDeferMissing() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Create an obviously worded alias to shouldDeferMissing() |
||
| 328 | * |
||
| 329 | * @return Mock |
||
| 330 | */ |
||
| 331 | public function makePartial() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * In the event shouldReceive() accepting one or more methods/returns, |
||
| 338 | * this method will switch them from normal expectations to default |
||
| 339 | * expectations |
||
| 340 | * |
||
| 341 | * @return self |
||
| 342 | */ |
||
| 343 | public function byDefault() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Capture calls to this mock |
||
| 356 | */ |
||
| 357 | public function __call($method, array $args) |
||
| 361 | |||
| 362 | public static function __callStatic($method, array $args) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Forward calls to this magic method to the __call method |
||
| 369 | */ |
||
| 370 | public function __toString() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Iterate across all expectation directors and validate each |
||
| 377 | * |
||
| 378 | * @throws \Mockery\CountValidator\Exception |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | public function mockery_verify() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Tear down tasks for this mock |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function mockery_teardown() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Fetch the next available allocation order number |
||
| 407 | * |
||
| 408 | * @return int |
||
| 409 | */ |
||
| 410 | public function mockery_allocateOrder() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set ordering for a group |
||
| 418 | * |
||
| 419 | * @param mixed $group |
||
| 420 | * @param int $order |
||
| 421 | */ |
||
| 422 | public function mockery_setGroup($group, $order) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Fetch array of ordered groups |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | public function mockery_getGroups() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set current ordered number |
||
| 439 | * |
||
| 440 | * @param int $order |
||
| 441 | */ |
||
| 442 | public function mockery_setCurrentOrder($order) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get current ordered number |
||
| 450 | * |
||
| 451 | * @return int |
||
| 452 | */ |
||
| 453 | public function mockery_getCurrentOrder() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Validate the current mock's ordering |
||
| 460 | * |
||
| 461 | * @param string $method |
||
| 462 | * @param int $order |
||
| 463 | * @throws \Mockery\Exception |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | public function mockery_validateOrder($method, $order) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Gets the count of expectations for this mock |
||
| 485 | * |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | public function mockery_getExpectationCount() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Return the expectations director for the given method |
||
| 499 | * |
||
| 500 | * @var string $method |
||
| 501 | * @return \Mockery\ExpectationDirector|null |
||
| 502 | */ |
||
| 503 | public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Return the expectations director for the given method |
||
| 510 | * |
||
| 511 | * @var string $method |
||
| 512 | * @return \Mockery\ExpectationDirector|null |
||
| 513 | */ |
||
| 514 | public function mockery_getExpectationsFor($method) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Find an expectation matching the given method and arguments |
||
| 523 | * |
||
| 524 | * @var string $method |
||
| 525 | * @var array $args |
||
| 526 | * @return \Mockery\Expectation|null |
||
| 527 | */ |
||
| 528 | public function mockery_findExpectation($method, array $args) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Return the container for this mock |
||
| 540 | * |
||
| 541 | * @return \Mockery\Container |
||
| 542 | */ |
||
| 543 | public function mockery_getContainer() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Return the name for this mock |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function mockery_getName() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | public function mockery_getMockableProperties() |
||
| 565 | |||
| 566 | public function __isset($name) |
||
| 574 | |||
| 575 | public function mockery_getExpectations() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Calls a parent class method and returns the result. Used in a passthru |
||
| 582 | * expectation where a real return value is required while still taking |
||
| 583 | * advantage of expectation matching and call count verification. |
||
| 584 | * |
||
| 585 | * @param string $name |
||
| 586 | * @param array $args |
||
| 587 | * @return mixed |
||
| 588 | */ |
||
| 589 | public function mockery_callSubjectMethod($name, array $args) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @return string[] |
||
| 596 | */ |
||
| 597 | public function mockery_getMockableMethods() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @return bool |
||
| 604 | */ |
||
| 605 | public function mockery_isAnonymous() |
||
| 611 | |||
| 612 | public function __wakeup() |
||
| 621 | |||
| 622 | public function __destruct() |
||
| 628 | |||
| 629 | public function mockery_getMethod($name) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param string $name Method name. |
||
| 642 | * |
||
| 643 | * @return mixed Generated return value based on the declared return value of the named method. |
||
| 644 | */ |
||
| 645 | public function mockery_returnValueForMethod($name) |
||
| 686 | |||
| 687 | public function shouldHaveReceived($method = null, $args = null) |
||
| 703 | |||
| 704 | public function shouldNotHaveReceived($method = null, $args = null) |
||
| 720 | |||
| 721 | protected static function _mockery_handleStaticMethodCall($method, array $args) |
||
| 733 | |||
| 734 | protected function _mockery_getReceivedMethodCalls() |
||
| 738 | |||
| 739 | protected function _mockery_handleMethodCall($method, array $args) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Uses reflection to get the list of all |
||
| 811 | * methods within the current mock object |
||
| 812 | * |
||
| 813 | * @return array |
||
| 814 | */ |
||
| 815 | protected function mockery_getMethods() |
||
| 829 | |||
| 830 | private function hasMethodOverloadingInParentClass() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * @return array |
||
| 838 | */ |
||
| 839 | private function getNonPublicMethods() |
||
| 850 | } |
||
| 851 |