Complex classes like Mockery 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 Mockery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Mockery |
||
| 39 | { |
||
| 40 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Global container to hold all mocks for the current unit test running. |
||
| 44 | * |
||
| 45 | * @var \Mockery\Container |
||
| 46 | */ |
||
| 47 | protected static $_container = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Global configuration handler containing configuration options. |
||
| 51 | * |
||
| 52 | * @var \Mockery\Configuration |
||
| 53 | */ |
||
| 54 | protected static $_config = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Mockery\Generator\Generator |
||
| 58 | */ |
||
| 59 | protected static $_generator; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \Mockery\Loader\Loader |
||
| 63 | */ |
||
| 64 | protected static $_loader; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private static $_filesToCleanUp = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Static shortcut to \Mockery\Container::mock(). |
||
| 73 | * |
||
| 74 | * @return \Mockery\MockInterface |
||
| 75 | */ |
||
| 76 | 24 | public static function mock() |
|
| 77 | { |
||
| 78 | 24 | $args = func_get_args(); |
|
| 79 | |||
| 80 | 24 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return \Mockery\MockInterface |
||
| 85 | */ |
||
| 86 | 7 | public static function spy() |
|
| 87 | { |
||
| 88 | 7 | $args = func_get_args(); |
|
| 89 | 7 | return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing(); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return \Mockery\MockInterface |
||
| 94 | */ |
||
| 95 | public static function instanceMock() |
||
| 96 | { |
||
| 97 | $args = func_get_args(); |
||
| 98 | |||
| 99 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
| 104 | * |
||
| 105 | * @return \Mockery\MockInterface |
||
| 106 | */ |
||
| 107 | 4 | public static function namedMock() |
|
| 108 | { |
||
| 109 | 4 | $args = func_get_args(); |
|
| 110 | 4 | $name = array_shift($args); |
|
| 111 | |||
| 112 | 4 | $builder = new MockConfigurationBuilder(); |
|
| 113 | 4 | $builder->setName($name); |
|
| 114 | |||
| 115 | 4 | array_unshift($args, $builder); |
|
| 116 | |||
| 117 | 4 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Static shortcut to \Mockery\Container::self(). |
||
| 122 | * |
||
| 123 | * @throws LogicException |
||
| 124 | * |
||
| 125 | * @return \Mockery\MockInterface |
||
| 126 | */ |
||
| 127 | public static function self() |
||
| 128 | { |
||
| 129 | if (is_null(self::$_container)) { |
||
| 130 | throw new \LogicException('You have not declared any mocks yet'); |
||
| 131 | } |
||
| 132 | |||
| 133 | return self::$_container->self(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Static shortcut to closing up and verifying all mocks in the global |
||
| 138 | * container, and resetting the container static variable to null. |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | 404 | public static function close() |
|
| 143 | { |
||
| 144 | 404 | foreach (self::$_filesToCleanUp as $fileName) { |
|
| 145 | 402 | @unlink($fileName); |
|
| 146 | 404 | } |
|
| 147 | |||
| 148 | 404 | if (is_null(self::$_container)) { |
|
| 149 | return; |
||
| 150 | } |
||
| 151 | |||
| 152 | 404 | self::$_container->mockery_teardown(); |
|
| 153 | 404 | self::$_container->mockery_close(); |
|
| 154 | 404 | self::$_container = null; |
|
| 155 | 404 | } |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Static fetching of a mock associated with a name or explicit class poser. |
||
| 159 | * |
||
| 160 | * @param $name |
||
| 161 | * |
||
| 162 | * @return \Mockery\Mock |
||
| 163 | */ |
||
| 164 | 11 | public static function fetchMock($name) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Lazy loader and getter for |
||
| 171 | * the container property. |
||
| 172 | * |
||
| 173 | * @return Mockery\Container |
||
| 174 | */ |
||
| 175 | 419 | public static function getContainer() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param \Mockery\Generator\Generator $generator |
||
| 186 | */ |
||
| 187 | public static function setGenerator(Generator $generator) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Lazy loader method and getter for |
||
| 194 | * the generator property. |
||
| 195 | * |
||
| 196 | * @return Generator |
||
| 197 | */ |
||
| 198 | 413 | public static function getGenerator() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Creates and returns a default generator |
||
| 209 | * used inside this class. |
||
| 210 | * |
||
| 211 | * @return CachingGenerator |
||
| 212 | */ |
||
| 213 | 400 | public static function getDefaultGenerator() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param Loader $loader |
||
| 232 | */ |
||
| 233 | public static function setLoader(Loader $loader) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return Loader |
||
| 240 | */ |
||
| 241 | 413 | public static function getLoader() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @return EvalLoader |
||
| 252 | */ |
||
| 253 | 274 | public static function getDefaultLoader() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set the container. |
||
| 260 | * |
||
| 261 | * @param \Mockery\Container $container |
||
| 262 | * |
||
| 263 | * @return \Mockery\Container |
||
| 264 | */ |
||
| 265 | 18 | public static function setContainer(Mockery\Container $container) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Reset the container to null. |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | 15 | public static function resetContainer() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Return instance of ANY matcher. |
||
| 282 | * |
||
| 283 | * @return \Mockery\Matcher\Any |
||
| 284 | */ |
||
| 285 | 5 | public static function any() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Return instance of TYPE matcher. |
||
| 292 | * |
||
| 293 | * @param $expected |
||
| 294 | * |
||
| 295 | * @return \Mockery\Matcher\Type |
||
| 296 | */ |
||
| 297 | 48 | public static function type($expected) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Return instance of DUCKTYPE matcher. |
||
| 304 | * |
||
| 305 | * @return \Mockery\Matcher\Ducktype |
||
| 306 | */ |
||
| 307 | 3 | public static function ducktype() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Return instance of SUBSET matcher. |
||
| 314 | * |
||
| 315 | * @param array $part |
||
| 316 | * |
||
| 317 | * @return \Mockery\Matcher\Subset |
||
| 318 | */ |
||
| 319 | 3 | public static function subset(array $part) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Return instance of CONTAINS matcher. |
||
| 326 | * |
||
| 327 | * @return \Mockery\Matcher\Contains |
||
| 328 | */ |
||
| 329 | 3 | public static function contains() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Return instance of HASKEY matcher. |
||
| 336 | * |
||
| 337 | * @param $key |
||
| 338 | * |
||
| 339 | * @return \Mockery\Matcher\HasKey |
||
| 340 | */ |
||
| 341 | 3 | public static function hasKey($key) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Return instance of HASVALUE matcher. |
||
| 348 | * |
||
| 349 | * @param $val |
||
| 350 | * |
||
| 351 | * @return \Mockery\Matcher\HasValue |
||
| 352 | */ |
||
| 353 | 3 | public static function hasValue($val) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Return instance of CLOSURE matcher. |
||
| 360 | * |
||
| 361 | * @param $closure |
||
| 362 | * |
||
| 363 | * @return \Mockery\Matcher\Closure |
||
| 364 | */ |
||
| 365 | 7 | public static function on($closure) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Return instance of MUSTBE matcher. |
||
| 372 | * |
||
| 373 | * @param $expected |
||
| 374 | * |
||
| 375 | * @return \Mockery\Matcher\MustBe |
||
| 376 | */ |
||
| 377 | 9 | public static function mustBe($expected) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Return instance of NOT matcher. |
||
| 384 | * |
||
| 385 | * @param $expected |
||
| 386 | * |
||
| 387 | * @return \Mockery\Matcher\Not |
||
| 388 | */ |
||
| 389 | 3 | public static function not($expected) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Return instance of ANYOF matcher. |
||
| 396 | * |
||
| 397 | * @return \Mockery\Matcher\AnyOf |
||
| 398 | */ |
||
| 399 | 3 | public static function anyOf() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Return instance of NOTANYOF matcher. |
||
| 406 | * |
||
| 407 | * @return \Mockery\Matcher\NotAnyOf |
||
| 408 | */ |
||
| 409 | 3 | public static function notAnyOf() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Lazy loader and Getter for the global |
||
| 416 | * configuration container. |
||
| 417 | * |
||
| 418 | * @return \Mockery\Configuration |
||
| 419 | */ |
||
| 420 | 412 | public static function getConfiguration() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Utility method to format method name and arguments into a string. |
||
| 431 | * |
||
| 432 | * @param string $method |
||
| 433 | * @param array $arguments |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 94 | public static function formatArgs($method, array $arguments = null) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Gets the string representation |
||
| 453 | * of any passed argument. |
||
| 454 | * |
||
| 455 | * @param $argument |
||
| 456 | * @param $depth |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 60 | private static function formatArgument($argument, $depth = 0) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Utility function to format objects to printable arrays. |
||
| 504 | * |
||
| 505 | * @param array $objects |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | 54 | public static function formatObjects(array $objects = null) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
| 540 | * |
||
| 541 | * @param $object |
||
| 542 | * @param int $nesting |
||
| 543 | * |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | 7 | private static function objectToArray($object, $nesting = 3) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Returns all public instance properties. |
||
| 561 | * |
||
| 562 | * @param $object |
||
| 563 | * @param $nesting |
||
| 564 | * |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Returns all object getters. |
||
| 585 | * |
||
| 586 | * @param $object |
||
| 587 | * @param $nesting |
||
| 588 | * |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | 7 | private static function extractGetters($object, $nesting) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Utility method used for recursively generating |
||
| 619 | * an object or array representation. |
||
| 620 | * |
||
| 621 | * @param $argument |
||
| 622 | * @param $nesting |
||
| 623 | * |
||
| 624 | * @return mixed |
||
| 625 | */ |
||
| 626 | 1 | private static function cleanupNesting($argument, $nesting) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Utility method for recursively |
||
| 644 | * gerating a representation |
||
| 645 | * of the given array. |
||
| 646 | * |
||
| 647 | * @param array $argument |
||
| 648 | * @param int $nesting |
||
| 649 | * |
||
| 650 | * @return mixed |
||
| 651 | */ |
||
| 652 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Utility function to parse shouldReceive() arguments and generate |
||
| 671 | * expectations from such as needed. |
||
| 672 | * |
||
| 673 | * @param Mockery\MockInterface $mock |
||
| 674 | * @param array $args |
||
| 675 | * @param callable $add |
||
| 676 | * @return \Mockery\CompositeExpectation |
||
| 677 | */ |
||
| 678 | 307 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
| 696 | |||
| 697 | /** |
||
| 698 | * Sets up expectations on the members of the CompositeExpectation and |
||
| 699 | * builds up any demeter chain that was passed to shouldReceive. |
||
| 700 | * |
||
| 701 | * @param \Mockery\MockInterface $mock |
||
| 702 | * @param string $arg |
||
| 703 | * @param callable $add |
||
| 704 | * @throws Mockery\Exception |
||
| 705 | * @return \Mockery\ExpectationDirector |
||
| 706 | */ |
||
| 707 | 307 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * @param \Mockery\Container $container |
||
| 761 | * @param string $method |
||
| 762 | * @param Mockery\ExpectationInterface $exp |
||
| 763 | * |
||
| 764 | * @return \Mockery\Mock |
||
| 765 | */ |
||
| 766 | 11 | private static function getNewDemeterMock(Mockery\Container $container, |
|
| 775 | |||
| 776 | /** |
||
| 777 | * @param \Mockery\Container $container |
||
| 778 | * @param string $demeterMockKey |
||
| 779 | * |
||
| 780 | * @return mixed |
||
| 781 | */ |
||
| 782 | 5 | private static function getExistingDemeterMock(Mockery\Container $container, $demeterMockKey) |
|
| 789 | |||
| 790 | /** |
||
| 791 | * @param array $methodNames |
||
| 792 | * |
||
| 793 | * @return bool |
||
| 794 | */ |
||
| 795 | 301 | private static function noMoreElementsInChain(array $methodNames) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Register a file to be deleted on tearDown. |
||
| 802 | * |
||
| 803 | * @param string $fileName |
||
| 804 | */ |
||
| 805 | 17 | public static function registerFileForCleanUp($fileName) |
|
| 809 | } |
||
| 810 |