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 |
||
| 39 | class Mockery |
||
| 40 | { |
||
| 41 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Global container to hold all mocks for the current unit test running. |
||
| 45 | * |
||
| 46 | * @var \Mockery\Container |
||
| 47 | */ |
||
| 48 | protected static $_container = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Global configuration handler containing configuration options. |
||
| 52 | * |
||
| 53 | * @var \Mockery\Configuration |
||
| 54 | */ |
||
| 55 | protected static $_config = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Mockery\Generator\Generator |
||
| 59 | */ |
||
| 60 | protected static $_generator; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \Mockery\Loader\Loader |
||
| 64 | */ |
||
| 65 | protected static $_loader; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private static $_filesToCleanUp = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Defines the global helper functions |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | 3 | public static function globalHelpers() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Static shortcut to \Mockery\Container::mock(). |
||
| 84 | * |
||
| 85 | * @return \Mockery\MockInterface |
||
| 86 | */ |
||
| 87 | 27 | public static function mock() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Static and semantic shortcut for getting a mock from the container |
||
| 96 | * and applying the spy's expected behavior into it. |
||
| 97 | * |
||
| 98 | * @return \Mockery\MockInterface |
||
| 99 | */ |
||
| 100 | 8 | public static function spy() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
| 108 | * |
||
| 109 | * @return \Mockery\MockInterface |
||
| 110 | */ |
||
| 111 | public static function instanceMock() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
| 120 | * |
||
| 121 | * @return \Mockery\MockInterface |
||
| 122 | */ |
||
| 123 | 5 | public static function namedMock() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Static shortcut to \Mockery\Container::self(). |
||
| 138 | * |
||
| 139 | * @throws LogicException |
||
| 140 | * |
||
| 141 | * @return \Mockery\MockInterface |
||
| 142 | */ |
||
| 143 | 2 | public static function self() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Static shortcut to closing up and verifying all mocks in the global |
||
| 154 | * container, and resetting the container static variable to null. |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | 403 | public static function close() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Static fetching of a mock associated with a name or explicit class poser. |
||
| 176 | * |
||
| 177 | * @param $name |
||
| 178 | * |
||
| 179 | * @return \Mockery\Mock |
||
| 180 | */ |
||
| 181 | 11 | public static function fetchMock($name) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Lazy loader and getter for |
||
| 188 | * the container property. |
||
| 189 | * |
||
| 190 | * @return Mockery\Container |
||
| 191 | */ |
||
| 192 | 421 | public static function getContainer() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Setter for the $_generator static propery. |
||
| 203 | * |
||
| 204 | * @param \Mockery\Generator\Generator $generator |
||
| 205 | */ |
||
| 206 | public static function setGenerator(Generator $generator) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Lazy loader method and getter for |
||
| 213 | * the generator property. |
||
| 214 | * |
||
| 215 | * @return Generator |
||
| 216 | */ |
||
| 217 | 398 | public static function getGenerator() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Creates and returns a default generator |
||
| 228 | * used inside this class. |
||
| 229 | * |
||
| 230 | * @return CachingGenerator |
||
| 231 | */ |
||
| 232 | 398 | public static function getDefaultGenerator() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Setter for the $_loader static property. |
||
| 252 | * |
||
| 253 | * @param Loader $loader |
||
| 254 | */ |
||
| 255 | public static function setLoader(Loader $loader) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Lazy loader method and getter for |
||
| 262 | * the $_loader property. |
||
| 263 | * |
||
| 264 | * @return Loader |
||
| 265 | */ |
||
| 266 | 398 | public static function getLoader() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Gets an EvalLoader to be used as default. |
||
| 277 | * |
||
| 278 | * @return EvalLoader |
||
| 279 | */ |
||
| 280 | 272 | public static function getDefaultLoader() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Set the container. |
||
| 287 | * |
||
| 288 | * @param \Mockery\Container $container |
||
| 289 | * |
||
| 290 | * @return \Mockery\Container |
||
| 291 | */ |
||
| 292 | 18 | public static function setContainer(Mockery\Container $container) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Reset the container to null. |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | 15 | public static function resetContainer() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Return instance of ANY matcher. |
||
| 309 | * |
||
| 310 | * @return \Mockery\Matcher\Any |
||
| 311 | */ |
||
| 312 | 5 | public static function any() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Return instance of TYPE matcher. |
||
| 319 | * |
||
| 320 | * @param $expected |
||
| 321 | * |
||
| 322 | * @return \Mockery\Matcher\Type |
||
| 323 | */ |
||
| 324 | 48 | public static function type($expected) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Return instance of DUCKTYPE matcher. |
||
| 331 | * |
||
| 332 | * @return \Mockery\Matcher\Ducktype |
||
| 333 | */ |
||
| 334 | 3 | public static function ducktype() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Return instance of SUBSET matcher. |
||
| 341 | * |
||
| 342 | * @param array $part |
||
| 343 | * |
||
| 344 | * @return \Mockery\Matcher\Subset |
||
| 345 | */ |
||
| 346 | 3 | public static function subset(array $part) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Return instance of CONTAINS matcher. |
||
| 353 | * |
||
| 354 | * @return \Mockery\Matcher\Contains |
||
| 355 | */ |
||
| 356 | 3 | public static function contains() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Return instance of HASKEY matcher. |
||
| 363 | * |
||
| 364 | * @param $key |
||
| 365 | * |
||
| 366 | * @return \Mockery\Matcher\HasKey |
||
| 367 | */ |
||
| 368 | 3 | public static function hasKey($key) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Return instance of HASVALUE matcher. |
||
| 375 | * |
||
| 376 | * @param $val |
||
| 377 | * |
||
| 378 | * @return \Mockery\Matcher\HasValue |
||
| 379 | */ |
||
| 380 | 3 | public static function hasValue($val) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Return instance of CLOSURE matcher. |
||
| 387 | * |
||
| 388 | * @param $closure |
||
| 389 | * |
||
| 390 | * @return \Mockery\Matcher\Closure |
||
| 391 | */ |
||
| 392 | 7 | public static function on($closure) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Return instance of MUSTBE matcher. |
||
| 399 | * |
||
| 400 | * @param $expected |
||
| 401 | * |
||
| 402 | * @return \Mockery\Matcher\MustBe |
||
| 403 | */ |
||
| 404 | 6 | public static function mustBe($expected) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Return instance of NOT matcher. |
||
| 411 | * |
||
| 412 | * @param $expected |
||
| 413 | * |
||
| 414 | * @return \Mockery\Matcher\Not |
||
| 415 | */ |
||
| 416 | 3 | public static function not($expected) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Return instance of ANYOF matcher. |
||
| 423 | * |
||
| 424 | * @return \Mockery\Matcher\AnyOf |
||
| 425 | */ |
||
| 426 | 3 | public static function anyOf() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Return instance of NOTANYOF matcher. |
||
| 433 | * |
||
| 434 | * @return \Mockery\Matcher\NotAnyOf |
||
| 435 | */ |
||
| 436 | 3 | public static function notAnyOf() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Lazy loader and Getter for the global |
||
| 443 | * configuration container. |
||
| 444 | * |
||
| 445 | * @return \Mockery\Configuration |
||
| 446 | */ |
||
| 447 | 414 | public static function getConfiguration() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Utility method to format method name and arguments into a string. |
||
| 458 | * |
||
| 459 | * @param string $method |
||
| 460 | * @param array $arguments |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | 90 | public static function formatArgs($method, array $arguments = null) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Gets the string representation |
||
| 480 | * of any passed argument. |
||
| 481 | * |
||
| 482 | * @param $argument |
||
| 483 | * @param $depth |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | 56 | private static function formatArgument($argument, $depth = 0) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Utility function to format objects to printable arrays. |
||
| 531 | * |
||
| 532 | * @param array $objects |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | 52 | public static function formatObjects(array $objects = null) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
| 567 | * |
||
| 568 | * @param $object |
||
| 569 | * @param int $nesting |
||
| 570 | * |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | 7 | private static function objectToArray($object, $nesting = 3) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Returns all public instance properties. |
||
| 588 | * |
||
| 589 | * @param $object |
||
| 590 | * @param $nesting |
||
| 591 | * |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Returns all object getters. |
||
| 612 | * |
||
| 613 | * @param $object |
||
| 614 | * @param $nesting |
||
| 615 | * |
||
| 616 | * @return array |
||
| 617 | */ |
||
| 618 | 7 | private static function extractGetters($object, $nesting) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Utility method used for recursively generating |
||
| 646 | * an object or array representation. |
||
| 647 | * |
||
| 648 | * @param $argument |
||
| 649 | * @param $nesting |
||
| 650 | * |
||
| 651 | * @return mixed |
||
| 652 | */ |
||
| 653 | 1 | private static function cleanupNesting($argument, $nesting) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Utility method for recursively |
||
| 671 | * gerating a representation |
||
| 672 | * of the given array. |
||
| 673 | * |
||
| 674 | * @param array $argument |
||
| 675 | * @param int $nesting |
||
| 676 | * |
||
| 677 | * @return mixed |
||
| 678 | */ |
||
| 679 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Utility function to parse shouldReceive() arguments and generate |
||
| 698 | * expectations from such as needed. |
||
| 699 | * |
||
| 700 | * @param Mockery\MockInterface $mock |
||
| 701 | * @param array $args |
||
| 702 | * @param callable $add |
||
| 703 | * @return \Mockery\CompositeExpectation |
||
| 704 | */ |
||
| 705 | 298 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Sets up expectations on the members of the CompositeExpectation and |
||
| 726 | * builds up any demeter chain that was passed to shouldReceive. |
||
| 727 | * |
||
| 728 | * @param \Mockery\MockInterface $mock |
||
| 729 | * @param string $arg |
||
| 730 | * @param callable $add |
||
| 731 | * @throws Mockery\Exception |
||
| 732 | * @return \Mockery\ExpectationInterface |
||
| 733 | */ |
||
| 734 | 298 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Gets a new demeter configured |
||
| 788 | * mock from the container. |
||
| 789 | * |
||
| 790 | * @param \Mockery\Container $container |
||
| 791 | * @param string $method |
||
| 792 | * @param Mockery\ExpectationInterface $exp |
||
| 793 | * |
||
| 794 | * @return \Mockery\Mock |
||
| 795 | */ |
||
| 796 | 11 | private static function getNewDemeterMock( |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Gets an specific demeter mock from |
||
| 809 | * the ones kept by the container. |
||
| 810 | * |
||
| 811 | * @param \Mockery\Container $container |
||
| 812 | * @param string $demeterMockKey |
||
| 813 | * |
||
| 814 | * @return mixed |
||
| 815 | */ |
||
| 816 | 5 | private static function getExistingDemeterMock( |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Checks if the passed array representing a demeter |
||
| 828 | * chain with the method names is empty. |
||
| 829 | * |
||
| 830 | * @param array $methodNames |
||
| 831 | * |
||
| 832 | * @return bool |
||
| 833 | */ |
||
| 834 | 292 | private static function noMoreElementsInChain(array $methodNames) |
|
| 838 | |||
| 839 | 17 | public static function declareClass($fqn) |
|
| 843 | |||
| 844 | 2 | public static function declareInterface($fqn) |
|
| 848 | |||
| 849 | 18 | private static function declareType($fqn, $type) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Register a file to be deleted on tearDown. |
||
| 878 | * |
||
| 879 | * @param string $fileName |
||
| 880 | */ |
||
| 881 | 18 | public static function registerFileForCleanUp($fileName) |
|
| 885 | } |
||
| 886 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: