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 |
||
| 30 | class Mockery |
||
| 31 | { |
||
| 32 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Global container to hold all mocks for the current unit test running. |
||
| 36 | * |
||
| 37 | * @var \Mockery\Container |
||
| 38 | */ |
||
| 39 | protected static $_container = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Global configuration handler containing configuration options. |
||
| 43 | * |
||
| 44 | * @var \Mockery\Configuration |
||
| 45 | */ |
||
| 46 | protected static $_config = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \Mockery\Generator\Generator |
||
| 50 | */ |
||
| 51 | protected static $_generator; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Mockery\Loader\Loader |
||
| 55 | */ |
||
| 56 | protected static $_loader; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private static $_filesToCleanUp = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Defines the global helper functions |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | 3 | public static function globalHelpers() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Static shortcut to \Mockery\Container::mock(). |
||
| 75 | * |
||
| 76 | * @param array $args |
||
| 77 | * |
||
| 78 | * @return \Mockery\MockInterface |
||
| 79 | */ |
||
| 80 | 38 | public static function mock(...$args) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Static and semantic shortcut for getting a mock from the container |
||
| 87 | * and applying the spy's expected behavior into it. |
||
| 88 | * |
||
| 89 | * @param array $args |
||
| 90 | * |
||
| 91 | * @return \Mockery\MockInterface |
||
| 92 | */ |
||
| 93 | 9 | public static function spy(...$args) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
| 100 | * |
||
| 101 | * @param array $args |
||
| 102 | * |
||
| 103 | * @return \Mockery\MockInterface |
||
| 104 | */ |
||
| 105 | public static function instanceMock(...$args) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
| 112 | * |
||
| 113 | * @param array $args |
||
| 114 | * |
||
| 115 | * @return \Mockery\MockInterface |
||
| 116 | */ |
||
| 117 | 5 | public static function namedMock(...$args) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Static shortcut to \Mockery\Container::self(). |
||
| 131 | * |
||
| 132 | * @throws LogicException |
||
| 133 | * |
||
| 134 | * @return \Mockery\MockInterface |
||
| 135 | */ |
||
| 136 | 2 | public static function self() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Static shortcut to closing up and verifying all mocks in the global |
||
| 147 | * container, and resetting the container static variable to null. |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 414 | public static function close() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Static fetching of a mock associated with a name or explicit class poser. |
||
| 169 | * |
||
| 170 | * @param $name |
||
| 171 | * |
||
| 172 | * @return \Mockery\Mock |
||
| 173 | */ |
||
| 174 | 11 | public static function fetchMock($name) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Lazy loader and getter for |
||
| 181 | * the container property. |
||
| 182 | * |
||
| 183 | * @return Mockery\Container |
||
| 184 | */ |
||
| 185 | 438 | public static function getContainer() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Setter for the $_generator static propery. |
||
| 196 | * |
||
| 197 | * @param \Mockery\Generator\Generator $generator |
||
| 198 | */ |
||
| 199 | public static function setGenerator(Generator $generator) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Lazy loader method and getter for |
||
| 206 | * the generator property. |
||
| 207 | * |
||
| 208 | * @return Generator |
||
| 209 | */ |
||
| 210 | 407 | public static function getGenerator() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Creates and returns a default generator |
||
| 221 | * used inside this class. |
||
| 222 | * |
||
| 223 | * @return CachingGenerator |
||
| 224 | */ |
||
| 225 | 400 | public static function getDefaultGenerator() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Setter for the $_loader static property. |
||
| 232 | * |
||
| 233 | * @param Loader $loader |
||
| 234 | */ |
||
| 235 | public static function setLoader(Loader $loader) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Lazy loader method and getter for |
||
| 242 | * the $_loader property. |
||
| 243 | * |
||
| 244 | * @return Loader |
||
| 245 | */ |
||
| 246 | 407 | public static function getLoader() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Gets an EvalLoader to be used as default. |
||
| 257 | * |
||
| 258 | * @return EvalLoader |
||
| 259 | */ |
||
| 260 | 274 | public static function getDefaultLoader() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Set the container. |
||
| 267 | * |
||
| 268 | * @param \Mockery\Container $container |
||
| 269 | * |
||
| 270 | * @return \Mockery\Container |
||
| 271 | */ |
||
| 272 | 18 | public static function setContainer(Mockery\Container $container) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Reset the container to null. |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | 15 | public static function resetContainer() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Return instance of ANY matcher. |
||
| 289 | * |
||
| 290 | * @return \Mockery\Matcher\Any |
||
| 291 | */ |
||
| 292 | 5 | public static function any() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Return instance of TYPE matcher. |
||
| 299 | * |
||
| 300 | * @param $expected |
||
| 301 | * |
||
| 302 | * @return \Mockery\Matcher\Type |
||
| 303 | */ |
||
| 304 | 48 | public static function type($expected) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Return instance of DUCKTYPE matcher. |
||
| 311 | * |
||
| 312 | * @param array $args |
||
| 313 | * |
||
| 314 | * @return \Mockery\Matcher\Ducktype |
||
| 315 | */ |
||
| 316 | 3 | public static function ducktype(...$args) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Return instance of SUBSET matcher. |
||
| 323 | * |
||
| 324 | * @param array $part |
||
| 325 | * @param bool $strict - (Optional) True for strict comparison, false for loose |
||
| 326 | * |
||
| 327 | * @return \Mockery\Matcher\Subset |
||
| 328 | */ |
||
| 329 | 3 | public static function subset(array $part, $strict = true) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Return instance of CONTAINS matcher. |
||
| 336 | * |
||
| 337 | * @param array $args |
||
| 338 | * |
||
| 339 | * @return \Mockery\Matcher\Contains |
||
| 340 | */ |
||
| 341 | 3 | public static function contains(...$args) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Return instance of HASKEY matcher. |
||
| 348 | * |
||
| 349 | * @param $key |
||
| 350 | * |
||
| 351 | * @return \Mockery\Matcher\HasKey |
||
| 352 | */ |
||
| 353 | 4 | public static function hasKey($key) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Return instance of HASVALUE matcher. |
||
| 360 | * |
||
| 361 | * @param $val |
||
| 362 | * |
||
| 363 | * @return \Mockery\Matcher\HasValue |
||
| 364 | */ |
||
| 365 | 3 | public static function hasValue($val) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Return instance of CLOSURE matcher. |
||
| 372 | * |
||
| 373 | * @param $closure |
||
| 374 | * |
||
| 375 | * @return \Mockery\Matcher\Closure |
||
| 376 | */ |
||
| 377 | 7 | public static function on($closure) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Return instance of MUSTBE matcher. |
||
| 384 | * |
||
| 385 | * @param $expected |
||
| 386 | * |
||
| 387 | * @return \Mockery\Matcher\MustBe |
||
| 388 | */ |
||
| 389 | 6 | public static function mustBe($expected) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Return instance of NOT matcher. |
||
| 396 | * |
||
| 397 | * @param $expected |
||
| 398 | * |
||
| 399 | * @return \Mockery\Matcher\Not |
||
| 400 | */ |
||
| 401 | 3 | public static function not($expected) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Return instance of ANYOF matcher. |
||
| 408 | * |
||
| 409 | * @param array $args |
||
| 410 | * |
||
| 411 | * @return \Mockery\Matcher\AnyOf |
||
| 412 | */ |
||
| 413 | 3 | public static function anyOf(...$args) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Return instance of NOTANYOF matcher. |
||
| 420 | * |
||
| 421 | * @param array $args |
||
| 422 | * |
||
| 423 | * @return \Mockery\Matcher\NotAnyOf |
||
| 424 | */ |
||
| 425 | 3 | public static function notAnyOf(...$args) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Lazy loader and Getter for the global |
||
| 432 | * configuration container. |
||
| 433 | * |
||
| 434 | * @return \Mockery\Configuration |
||
| 435 | */ |
||
| 436 | 426 | public static function getConfiguration() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Utility method to format method name and arguments into a string. |
||
| 447 | * |
||
| 448 | * @param string $method |
||
| 449 | * @param array $arguments |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | 93 | public static function formatArgs($method, array $arguments = null) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Gets the string representation |
||
| 469 | * of any passed argument. |
||
| 470 | * |
||
| 471 | * @param $argument |
||
| 472 | * @param $depth |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 92 | private static function formatArgument($argument, $depth = 0) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Utility function to format objects to printable arrays. |
||
| 524 | * |
||
| 525 | * @param array $objects |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | 52 | public static function formatObjects(array $objects = null) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
| 560 | * |
||
| 561 | * @param $object |
||
| 562 | * @param int $nesting |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | 7 | private static function objectToArray($object, $nesting = 3) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Returns all public instance properties. |
||
| 581 | * |
||
| 582 | * @param $object |
||
| 583 | * @param $nesting |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Returns all object getters. |
||
| 605 | * |
||
| 606 | * @param $object |
||
| 607 | * @param $nesting |
||
| 608 | * |
||
| 609 | * @return array |
||
| 610 | */ |
||
| 611 | 7 | private static function extractGetters($object, $nesting) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Utility method used for recursively generating |
||
| 639 | * an object or array representation. |
||
| 640 | * |
||
| 641 | * @param $argument |
||
| 642 | * @param $nesting |
||
| 643 | * |
||
| 644 | * @return mixed |
||
| 645 | */ |
||
| 646 | 1 | private static function cleanupNesting($argument, $nesting) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Utility method for recursively |
||
| 664 | * gerating a representation |
||
| 665 | * of the given array. |
||
| 666 | * |
||
| 667 | * @param array $argument |
||
| 668 | * @param int $nesting |
||
| 669 | * |
||
| 670 | * @return mixed |
||
| 671 | */ |
||
| 672 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Utility function to parse shouldReceive() arguments and generate |
||
| 691 | * expectations from such as needed. |
||
| 692 | * |
||
| 693 | * @param Mockery\MockInterface $mock |
||
| 694 | * @param array $args |
||
| 695 | * @param callable $add |
||
| 696 | * @return \Mockery\CompositeExpectation |
||
| 697 | */ |
||
| 698 | 308 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Sets up expectations on the members of the CompositeExpectation and |
||
| 719 | * builds up any demeter chain that was passed to shouldReceive. |
||
| 720 | * |
||
| 721 | * @param \Mockery\MockInterface $mock |
||
| 722 | * @param string $arg |
||
| 723 | * @param callable $add |
||
| 724 | * @throws Mockery\Exception |
||
| 725 | * @return \Mockery\ExpectationInterface |
||
| 726 | */ |
||
| 727 | 308 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Gets a new demeter configured |
||
| 781 | * mock from the container. |
||
| 782 | * |
||
| 783 | * @param \Mockery\Container $container |
||
| 784 | * @param string $method |
||
| 785 | * @param Mockery\ExpectationInterface $exp |
||
| 786 | * |
||
| 787 | * @return \Mockery\Mock |
||
| 788 | */ |
||
| 789 | 11 | private static function getNewDemeterMock( |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Gets an specific demeter mock from |
||
| 802 | * the ones kept by the container. |
||
| 803 | * |
||
| 804 | * @param \Mockery\Container $container |
||
| 805 | * @param string $demeterMockKey |
||
| 806 | * |
||
| 807 | * @return mixed |
||
| 808 | */ |
||
| 809 | 5 | private static function getExistingDemeterMock( |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Checks if the passed array representing a demeter |
||
| 821 | * chain with the method names is empty. |
||
| 822 | * |
||
| 823 | * @param array $methodNames |
||
| 824 | * |
||
| 825 | * @return bool |
||
| 826 | */ |
||
| 827 | 302 | private static function noMoreElementsInChain(array $methodNames) |
|
| 831 | |||
| 832 | 17 | public static function declareClass($fqn) |
|
| 836 | |||
| 837 | 2 | public static function declareInterface($fqn) |
|
| 841 | |||
| 842 | 18 | private static function declareType($fqn, $type) |
|
| 868 | |||
| 869 | /** |
||
| 870 | * Register a file to be deleted on tearDown. |
||
| 871 | * |
||
| 872 | * @param string $fileName |
||
| 873 | */ |
||
| 874 | 18 | public static function registerFileForCleanUp($fileName) |
|
| 878 | } |
||
| 879 |
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: