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 |
||
| 26 | class Mock implements MockInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Stores an array of all expectation directors for this mock |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $_mockery_expectations = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Stores an inital number of expectations that can be manipulated |
||
| 37 | * while using the getter method. |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $_mockery_expectations_count = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Flag to indicate whether we can ignore method calls missing from our |
||
| 45 | * expectations |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $_mockery_ignoreMissing = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Flag to indicate whether we can defer method calls missing from our |
||
| 53 | * expectations |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $_mockery_deferMissing = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Flag to indicate whether this mock was verified |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $_mockery_verified = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Given name of the mock |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $_mockery_name = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Order number of allocation |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $_mockery_allocatedOrder = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Current ordered number |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $_mockery_currentOrder = 0; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Ordered groups |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $_mockery_groups = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Mock container containing this mock object |
||
| 96 | * |
||
| 97 | * @var \Mockery\Container |
||
| 98 | */ |
||
| 99 | protected $_mockery_container = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Instance of a core object on which methods are called in the event |
||
| 103 | * it has been set, and an expectation for one of the object's methods |
||
| 104 | * does not exist. This implements a simple partial mock proxy system. |
||
| 105 | * |
||
| 106 | * @var object |
||
| 107 | */ |
||
| 108 | protected $_mockery_partial = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Flag to indicate we should ignore all expectations temporarily. Used |
||
| 112 | * mainly to prevent expectation matching when in the middle of a mock |
||
| 113 | * object recording session. |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $_mockery_disableExpectationMatching = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Stores all stubbed public methods separate from any on-object public |
||
| 121 | * properties that may exist. |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $_mockery_mockableProperties = array(); |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | protected $_mockery_mockableMethods = array(); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Just a local cache for this mock's target's methods |
||
| 134 | * |
||
| 135 | * @var ReflectionMethod[] |
||
| 136 | */ |
||
| 137 | protected static $_mockery_methods; |
||
| 138 | |||
| 139 | protected $_mockery_allowMockingProtectedMethods = false; |
||
| 140 | |||
| 141 | protected $_mockery_receivedMethodCalls; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
||
| 145 | * @var mixed |
||
| 146 | */ |
||
| 147 | protected $_mockery_defaultReturnValue = null; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * We want to avoid constructors since class is copied to Generator.php |
||
| 151 | * for inclusion on extending class definitions. |
||
| 152 | * |
||
| 153 | * @param \Mockery\Container $container |
||
| 154 | * @param object $partialObject |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set expected method calls |
||
| 178 | * |
||
| 179 | * @param null|string $methodName,... one or many methods that are expected to be called in this mock |
||
| 180 | * @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage |
||
| 181 | */ |
||
| 182 | public function shouldReceive($methodName = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Shortcut method for setting an expectation that a method should not be called. |
||
| 227 | * |
||
| 228 | * @param null|string $methodName,... one or many methods that are expected not to be called in this mock |
||
| 229 | * @return \Mockery\Expectation|\Mockery\HigherOrderMessage |
||
| 230 | */ |
||
| 231 | public function shouldNotReceive($methodName = null) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Allows additional methods to be mocked that do not explicitly exist on mocked class |
||
| 244 | * @param String $method name of the method to be mocked |
||
| 245 | * @return Mock |
||
| 246 | */ |
||
| 247 | public function shouldAllowMockingMethod($method) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Set mock to ignore unexpected methods and return Undefined class |
||
| 255 | * @param mixed $returnValue the default return value for calls to missing functions on this mock |
||
| 256 | * @return Mock |
||
| 257 | */ |
||
| 258 | public function shouldIgnoreMissing($returnValue = null) |
||
| 264 | |||
| 265 | public function asUndefined() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return Mock |
||
| 274 | */ |
||
| 275 | public function shouldAllowMockingProtectedMethods() |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Set mock to defer unexpected methods to it's parent |
||
| 284 | * |
||
| 285 | * This is particularly useless for this class, as it doesn't have a parent, |
||
| 286 | * but included for completeness |
||
| 287 | * |
||
| 288 | * @return Mock |
||
| 289 | */ |
||
| 290 | public function shouldDeferMissing() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Create an obviously worded alias to shouldDeferMissing() |
||
| 298 | * |
||
| 299 | * @return Mock |
||
| 300 | */ |
||
| 301 | public function makePartial() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * In the event shouldReceive() accepting one or more methods/returns, |
||
| 308 | * this method will switch them from normal expectations to default |
||
| 309 | * expectations |
||
| 310 | * |
||
| 311 | * @return self |
||
| 312 | */ |
||
| 313 | public function byDefault() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Capture calls to this mock |
||
| 326 | */ |
||
| 327 | public function __call($method, array $args) |
||
| 331 | |||
| 332 | public static function __callStatic($method, array $args) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Forward calls to this magic method to the __call method |
||
| 339 | */ |
||
| 340 | public function __toString() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Iterate across all expectation directors and validate each |
||
| 347 | * |
||
| 348 | * @throws \Mockery\CountValidator\Exception |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | public function mockery_verify() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Tear down tasks for this mock |
||
| 368 | * |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | public function mockery_teardown() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Fetch the next available allocation order number |
||
| 377 | * |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | public function mockery_allocateOrder() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set ordering for a group |
||
| 388 | * |
||
| 389 | * @param mixed $group |
||
| 390 | * @param int $order |
||
| 391 | */ |
||
| 392 | public function mockery_setGroup($group, $order) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Fetch array of ordered groups |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function mockery_getGroups() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set current ordered number |
||
| 409 | * |
||
| 410 | * @param int $order |
||
| 411 | */ |
||
| 412 | public function mockery_setCurrentOrder($order) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get current ordered number |
||
| 420 | * |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | public function mockery_getCurrentOrder() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Validate the current mock's ordering |
||
| 430 | * |
||
| 431 | * @param string $method |
||
| 432 | * @param int $order |
||
| 433 | * @throws \Mockery\Exception |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | public function mockery_validateOrder($method, $order) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Gets the count of expectations for this mock |
||
| 455 | * |
||
| 456 | * @return int |
||
| 457 | */ |
||
| 458 | public function mockery_getExpectationCount() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Return the expectations director for the given method |
||
| 469 | * |
||
| 470 | * @var string $method |
||
| 471 | * @return \Mockery\ExpectationDirector|null |
||
| 472 | */ |
||
| 473 | public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Return the expectations director for the given method |
||
| 480 | * |
||
| 481 | * @var string $method |
||
| 482 | * @return \Mockery\ExpectationDirector|null |
||
| 483 | */ |
||
| 484 | public function mockery_getExpectationsFor($method) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Find an expectation matching the given method and arguments |
||
| 493 | * |
||
| 494 | * @var string $method |
||
| 495 | * @var array $args |
||
| 496 | * @return \Mockery\Expectation|null |
||
| 497 | */ |
||
| 498 | public function mockery_findExpectation($method, array $args) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Return the container for this mock |
||
| 510 | * |
||
| 511 | * @return \Mockery\Container |
||
| 512 | */ |
||
| 513 | public function mockery_getContainer() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Return the name for this mock |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function mockery_getName() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function mockery_getMockableProperties() |
||
| 535 | |||
| 536 | public function __isset($name) |
||
| 544 | |||
| 545 | public function mockery_getExpectations() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Calls a parent class method and returns the result. Used in a passthru |
||
| 552 | * expectation where a real return value is required while still taking |
||
| 553 | * advantage of expectation matching and call count verification. |
||
| 554 | * |
||
| 555 | * @param string $name |
||
| 556 | * @param array $args |
||
| 557 | * @return mixed |
||
| 558 | */ |
||
| 559 | public function mockery_callSubjectMethod($name, array $args) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return string[] |
||
| 566 | */ |
||
| 567 | public function mockery_getMockableMethods() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public function mockery_isAnonymous() |
||
| 581 | |||
| 582 | public function __wakeup() |
||
| 591 | |||
| 592 | public function __destruct() |
||
| 598 | |||
| 599 | public function mockery_getMethod($name) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param string $name Method name. |
||
| 612 | * |
||
| 613 | * @return mixed Generated return value based on the declared return value of the named method. |
||
| 614 | */ |
||
| 615 | public function mockery_returnValueForMethod($name) |
||
| 656 | |||
| 657 | public function shouldHaveReceived($method = null, $args = null) |
||
| 673 | |||
| 674 | public function shouldNotHaveReceived($method = null, $args = null) |
||
| 690 | |||
| 691 | protected static function _mockery_handleStaticMethodCall($method, array $args) |
||
| 703 | |||
| 704 | protected function _mockery_getReceivedMethodCalls() |
||
| 708 | |||
| 709 | protected function _mockery_handleMethodCall($method, array $args) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Uses reflection to get the list of all |
||
| 781 | * methods within the current mock object |
||
| 782 | * |
||
| 783 | * @return array |
||
| 784 | */ |
||
| 785 | protected function mockery_getMethods() |
||
| 799 | |||
| 800 | private function hasMethodOverloadingInParentClass() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @return array |
||
| 808 | */ |
||
| 809 | private function getNonPublicMethods() |
||
| 820 | } |
||
| 821 |
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: