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 |
||
| 25 | class Mock implements MockInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Stores an array of all expectation directors for this mock |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $_mockery_expectations = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Stores an inital number of expectations that can be manipulated |
||
| 36 | * while using the getter method. |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $_mockery_expectations_count = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Flag to indicate whether we can ignore method calls missing from our |
||
| 44 | * expectations |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $_mockery_ignoreMissing = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Flag to indicate whether we can defer method calls missing from our |
||
| 52 | * expectations |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $_mockery_deferMissing = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Flag to indicate whether this mock was verified |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $_mockery_verified = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Given name of the mock |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $_mockery_name = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Order number of allocation |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $_mockery_allocatedOrder = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Current ordered number |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | protected $_mockery_currentOrder = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Ordered groups |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $_mockery_groups = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Mock container containing this mock object |
||
| 95 | * |
||
| 96 | * @var \Mockery\Container |
||
| 97 | */ |
||
| 98 | protected $_mockery_container = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Instance of a core object on which methods are called in the event |
||
| 102 | * it has been set, and an expectation for one of the object's methods |
||
| 103 | * does not exist. This implements a simple partial mock proxy system. |
||
| 104 | * |
||
| 105 | * @var object |
||
| 106 | */ |
||
| 107 | protected $_mockery_partial = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Flag to indicate we should ignore all expectations temporarily. Used |
||
| 111 | * mainly to prevent expectation matching when in the middle of a mock |
||
| 112 | * object recording session. |
||
| 113 | * |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $_mockery_disableExpectationMatching = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Stores all stubbed public methods separate from any on-object public |
||
| 120 | * properties that may exist. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $_mockery_mockableProperties = array(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $_mockery_mockableMethods = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Just a local cache for this mock's target's methods |
||
| 133 | * |
||
| 134 | * @var ReflectionMethod[] |
||
| 135 | */ |
||
| 136 | protected static $_mockery_methods; |
||
| 137 | |||
| 138 | protected $_mockery_allowMockingProtectedMethods = false; |
||
| 139 | |||
| 140 | protected $_mockery_receivedMethodCalls; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
||
| 144 | * @var mixed |
||
| 145 | */ |
||
| 146 | protected $_mockery_defaultReturnValue = null; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * We want to avoid constructors since class is copied to Generator.php |
||
| 150 | * for inclusion on extending class definitions. |
||
| 151 | * |
||
| 152 | * @param \Mockery\Container $container |
||
| 153 | * @param object $partialObject |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set expected method calls |
||
| 177 | * |
||
| 178 | * @param string $methodName,... one or many methods that are expected to be called in this mock |
||
| 179 | * @return \Mockery\Expectation |
||
| 180 | */ |
||
| 181 | public function shouldReceive($methodName) |
||
| 182 | { |
||
| 183 | if (func_num_args() < 1) { |
||
| 184 | throw new \InvalidArgumentException("At least one method name is required"); |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach (func_get_args() as $method) { |
||
| 188 | if ("" == $method) { |
||
| 189 | throw new \InvalidArgumentException("Received empty method name"); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** @var array $nonPublicMethods */ |
||
| 194 | $nonPublicMethods = $this->getNonPublicMethods(); |
||
| 195 | |||
| 196 | $self = $this; |
||
| 197 | $allowMockingProtectedMethods = $this->_mockery_allowMockingProtectedMethods; |
||
| 198 | |||
| 199 | $lastExpectation = \Mockery::parseShouldReturnArgs( |
||
| 200 | $this, func_get_args(), function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) { |
||
| 201 | $rm = $self->mockery_getMethod($method); |
||
| 202 | if ($rm) { |
||
| 203 | if ($rm->isPrivate()) { |
||
| 204 | throw new \InvalidArgumentException("$method() cannot be mocked as it is a private method"); |
||
| 205 | } |
||
| 206 | if (!$allowMockingProtectedMethods && $rm->isProtected()) { |
||
| 207 | throw new \InvalidArgumentException("$method() cannot be mocked as it is a protected method and mocking protected methods is not enabled for the currently used mock object."); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $director = $self->mockery_getExpectationsFor($method); |
||
| 212 | if (!$director) { |
||
| 213 | $director = new \Mockery\ExpectationDirector($method, $self); |
||
| 214 | $self->mockery_setExpectationsFor($method, $director); |
||
| 215 | } |
||
| 216 | $expectation = new \Mockery\Expectation($self, $method); |
||
| 217 | $director->addExpectation($expectation); |
||
| 218 | return $expectation; |
||
| 219 | } |
||
| 220 | ); |
||
| 221 | return $lastExpectation; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Shortcut method for setting an expectation that a method should not be called. |
||
| 226 | * |
||
| 227 | * @param string $methodName,... one or many methods that are expected not to be called in this mock |
||
| 228 | * @return \Mockery\Expectation |
||
| 229 | */ |
||
| 230 | public function shouldNotReceive($methodName) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Allows additional methods to be mocked that do not explicitly exist on mocked class |
||
| 239 | * @param String $method name of the method to be mocked |
||
| 240 | * @return Mock |
||
| 241 | */ |
||
| 242 | public function shouldAllowMockingMethod($method) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set mock to ignore unexpected methods and return Undefined class |
||
| 250 | * @param mixed $returnValue the default return value for calls to missing functions on this mock |
||
| 251 | * @return Mock |
||
| 252 | */ |
||
| 253 | public function shouldIgnoreMissing($returnValue = null) |
||
| 259 | |||
| 260 | public function asUndefined() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return Mock |
||
| 269 | */ |
||
| 270 | public function shouldAllowMockingProtectedMethods() |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Set mock to defer unexpected methods to it's parent |
||
| 279 | * |
||
| 280 | * This is particularly useless for this class, as it doesn't have a parent, |
||
| 281 | * but included for completeness |
||
| 282 | * |
||
| 283 | * @return Mock |
||
| 284 | */ |
||
| 285 | public function shouldDeferMissing() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Create an obviously worded alias to shouldDeferMissing() |
||
| 293 | * |
||
| 294 | * @return Mock |
||
| 295 | */ |
||
| 296 | public function makePartial() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * In the event shouldReceive() accepting one or more methods/returns, |
||
| 303 | * this method will switch them from normal expectations to default |
||
| 304 | * expectations |
||
| 305 | * |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function byDefault() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Capture calls to this mock |
||
| 321 | */ |
||
| 322 | public function __call($method, array $args) |
||
| 326 | |||
| 327 | public static function __callStatic($method, array $args) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Forward calls to this magic method to the __call method |
||
| 334 | */ |
||
| 335 | public function __toString() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Iterate across all expectation directors and validate each |
||
| 342 | * |
||
| 343 | * @throws \Mockery\CountValidator\Exception |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function mockery_verify() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Tear down tasks for this mock |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | public function mockery_teardown() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Fetch the next available allocation order number |
||
| 372 | * |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public function mockery_allocateOrder() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set ordering for a group |
||
| 383 | * |
||
| 384 | * @param mixed $group |
||
| 385 | * @param int $order |
||
| 386 | */ |
||
| 387 | public function mockery_setGroup($group, $order) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Fetch array of ordered groups |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function mockery_getGroups() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set current ordered number |
||
| 404 | * |
||
| 405 | * @param int $order |
||
| 406 | */ |
||
| 407 | public function mockery_setCurrentOrder($order) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get current ordered number |
||
| 415 | * |
||
| 416 | * @return int |
||
| 417 | */ |
||
| 418 | public function mockery_getCurrentOrder() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Validate the current mock's ordering |
||
| 425 | * |
||
| 426 | * @param string $method |
||
| 427 | * @param int $order |
||
| 428 | * @throws \Mockery\Exception |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | public function mockery_validateOrder($method, $order) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Gets the count of expectations for this mock |
||
| 450 | * |
||
| 451 | * @return int |
||
| 452 | */ |
||
| 453 | public function mockery_getExpectationCount() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Return the expectations director for the given method |
||
| 464 | * |
||
| 465 | * @var string $method |
||
| 466 | * @return \Mockery\ExpectationDirector|null |
||
| 467 | */ |
||
| 468 | public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Return the expectations director for the given method |
||
| 475 | * |
||
| 476 | * @var string $method |
||
| 477 | * @return \Mockery\ExpectationDirector|null |
||
| 478 | */ |
||
| 479 | public function mockery_getExpectationsFor($method) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Find an expectation matching the given method and arguments |
||
| 488 | * |
||
| 489 | * @var string $method |
||
| 490 | * @var array $args |
||
| 491 | * @return \Mockery\Expectation|null |
||
| 492 | */ |
||
| 493 | public function mockery_findExpectation($method, array $args) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Return the container for this mock |
||
| 505 | * |
||
| 506 | * @return \Mockery\Container |
||
| 507 | */ |
||
| 508 | public function mockery_getContainer() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Return the name for this mock |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function mockery_getName() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function mockery_getMockableProperties() |
||
| 530 | |||
| 531 | public function __isset($name) |
||
| 539 | |||
| 540 | public function mockery_getExpectations() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Calls a parent class method and returns the result. Used in a passthru |
||
| 547 | * expectation where a real return value is required while still taking |
||
| 548 | * advantage of expectation matching and call count verification. |
||
| 549 | * |
||
| 550 | * @param string $name |
||
| 551 | * @param array $args |
||
| 552 | * @return mixed |
||
| 553 | */ |
||
| 554 | public function mockery_callSubjectMethod($name, array $args) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return string[] |
||
| 561 | */ |
||
| 562 | public function mockery_getMockableMethods() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | public function mockery_isAnonymous() |
||
| 576 | |||
| 577 | public function __wakeup() |
||
| 586 | |||
| 587 | public function __destruct() |
||
| 593 | |||
| 594 | public function mockery_getMethod($name) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param string $name Method name. |
||
| 607 | * |
||
| 608 | * @return mixed Generated return value based on the declared return value of the named method. |
||
| 609 | */ |
||
| 610 | public function mockery_returnValueForMethod($name) |
||
| 650 | |||
| 651 | public function shouldHaveReceived($method, $args = null) |
||
| 663 | |||
| 664 | public function shouldNotHaveReceived($method, $args = null) |
||
| 676 | |||
| 677 | protected static function _mockery_handleStaticMethodCall($method, array $args) |
||
| 689 | |||
| 690 | protected function _mockery_getReceivedMethodCalls() |
||
| 694 | |||
| 695 | protected function _mockery_handleMethodCall($method, array $args) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Uses reflection to get the list of all |
||
| 767 | * methods within the current mock object |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | protected function mockery_getMethods() |
||
| 785 | |||
| 786 | private function hasMethodOverloadingInParentClass() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @return array |
||
| 794 | */ |
||
| 795 | private function getNonPublicMethods() |
||
| 806 | } |
||
| 807 |
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: