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 |
||
| 29 | class Mockery |
||
| 30 | { |
||
| 31 | const BLOCKS = 'Mockery_Forward_Blocks'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Global container to hold all mocks for the current unit test running. |
||
| 35 | * |
||
| 36 | * @var \Mockery\Container |
||
| 37 | */ |
||
| 38 | protected static $_container = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Global configuration handler containing configuration options. |
||
| 42 | * |
||
| 43 | * @var \Mockery\Configuration |
||
| 44 | */ |
||
| 45 | protected static $_config = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Mockery\Generator\Generator |
||
| 49 | */ |
||
| 50 | protected static $_generator; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \Mockery\Loader\Loader |
||
| 54 | */ |
||
| 55 | protected static $_loader; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private static $_filesToCleanUp = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Defines the global helper functions |
||
| 64 | * |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | 3 | public static function globalHelpers() |
|
| 68 | { |
||
| 69 | 3 | require_once __DIR__.'/helpers.php'; |
|
| 70 | 3 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Static shortcut to \Mockery\Container::mock(). |
||
| 74 | * |
||
| 75 | * @param array $args |
||
| 76 | * |
||
| 77 | * @return \Mockery\MockInterface |
||
| 78 | */ |
||
| 79 | 37 | public static function mock(...$args) |
|
| 80 | { |
||
| 81 | 37 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Static and semantic shortcut for getting a mock from the container |
||
| 86 | * and applying the spy's expected behavior into it. |
||
| 87 | * |
||
| 88 | * @param array $args |
||
| 89 | * |
||
| 90 | * @return \Mockery\MockInterface |
||
| 91 | */ |
||
| 92 | 9 | public static function spy(...$args) |
|
| 93 | { |
||
| 94 | 9 | return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing(); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Static and Semantic shortcut to \Mockery\Container::mock(). |
||
| 99 | * |
||
| 100 | * @param array $args |
||
| 101 | * |
||
| 102 | * @return \Mockery\MockInterface |
||
| 103 | */ |
||
| 104 | public static function instanceMock(...$args) |
||
| 105 | { |
||
| 106 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Static shortcut to \Mockery\Container::mock(), first argument names the mock. |
||
| 111 | * |
||
| 112 | * @param array $args |
||
| 113 | * |
||
| 114 | * @return \Mockery\MockInterface |
||
| 115 | */ |
||
| 116 | 5 | public static function namedMock(...$args) |
|
| 117 | { |
||
| 118 | 5 | $name = array_shift($args); |
|
| 119 | |||
| 120 | 5 | $builder = new MockConfigurationBuilder(); |
|
| 121 | 5 | $builder->setName($name); |
|
| 122 | |||
| 123 | 5 | array_unshift($args, $builder); |
|
| 124 | |||
| 125 | 5 | return call_user_func_array(array(self::getContainer(), 'mock'), $args); |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Static shortcut to \Mockery\Container::self(). |
||
| 130 | * |
||
| 131 | * @throws LogicException |
||
| 132 | * |
||
| 133 | * @return \Mockery\MockInterface |
||
| 134 | */ |
||
| 135 | 2 | public static function self() |
|
| 136 | { |
||
| 137 | 2 | if (is_null(self::$_container)) { |
|
| 138 | 1 | throw new \LogicException('You have not declared any mocks yet'); |
|
| 139 | } |
||
| 140 | |||
| 141 | 1 | return self::$_container->self(); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Static shortcut to closing up and verifying all mocks in the global |
||
| 146 | * container, and resetting the container static variable to null. |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | 413 | public static function close() |
|
| 151 | { |
||
| 152 | 413 | foreach (self::$_filesToCleanUp as $fileName) { |
|
| 153 | 18 | @unlink($fileName); |
|
| 154 | 413 | } |
|
| 155 | 413 | self::$_filesToCleanUp = []; |
|
| 156 | |||
| 157 | 413 | if (is_null(self::$_container)) { |
|
| 158 | return; |
||
| 159 | } |
||
| 160 | |||
| 161 | 413 | self::$_container->mockery_teardown(); |
|
| 162 | 411 | self::$_container->mockery_close(); |
|
| 163 | 411 | self::$_container = null; |
|
| 164 | 411 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Static fetching of a mock associated with a name or explicit class poser. |
||
| 168 | * |
||
| 169 | * @param $name |
||
| 170 | * |
||
| 171 | * @return \Mockery\Mock |
||
| 172 | */ |
||
| 173 | 11 | public static function fetchMock($name) |
|
| 174 | { |
||
| 175 | 11 | return self::$_container->fetchMock($name); |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Lazy loader and getter for |
||
| 180 | * the container property. |
||
| 181 | * |
||
| 182 | * @return Mockery\Container |
||
| 183 | */ |
||
| 184 | 437 | public static function getContainer() |
|
| 185 | { |
||
| 186 | 437 | if (is_null(self::$_container)) { |
|
| 187 | 406 | self::$_container = new Mockery\Container(self::getGenerator(), self::getLoader()); |
|
| 188 | 406 | } |
|
| 189 | |||
| 190 | 437 | return self::$_container; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Setter for the $_generator static propery. |
||
| 195 | * |
||
| 196 | * @param \Mockery\Generator\Generator $generator |
||
| 197 | */ |
||
| 198 | public static function setGenerator(Generator $generator) |
||
| 199 | { |
||
| 200 | self::$_generator = $generator; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Lazy loader method and getter for |
||
| 205 | * the generator property. |
||
| 206 | * |
||
| 207 | * @return Generator |
||
| 208 | */ |
||
| 209 | 406 | public static function getGenerator() |
|
| 210 | { |
||
| 211 | 406 | if (is_null(self::$_generator)) { |
|
| 212 | 1 | self::$_generator = self::getDefaultGenerator(); |
|
| 213 | 1 | } |
|
| 214 | |||
| 215 | 406 | return self::$_generator; |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Creates and returns a default generator |
||
| 220 | * used inside this class. |
||
| 221 | * |
||
| 222 | * @return CachingGenerator |
||
| 223 | */ |
||
| 224 | 399 | public static function getDefaultGenerator() |
|
| 225 | { |
||
| 226 | 399 | return new CachingGenerator(StringManipulationGenerator::withDefaultPasses()); |
|
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Setter for the $_loader static property. |
||
| 231 | * |
||
| 232 | * @param Loader $loader |
||
| 233 | */ |
||
| 234 | public static function setLoader(Loader $loader) |
||
| 235 | { |
||
| 236 | self::$_loader = $loader; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Lazy loader method and getter for |
||
| 241 | * the $_loader property. |
||
| 242 | * |
||
| 243 | * @return Loader |
||
| 244 | */ |
||
| 245 | 406 | public static function getLoader() |
|
| 246 | { |
||
| 247 | 406 | if (is_null(self::$_loader)) { |
|
| 248 | 1 | self::$_loader = self::getDefaultLoader(); |
|
| 249 | 1 | } |
|
| 250 | |||
| 251 | 406 | return self::$_loader; |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Gets an EvalLoader to be used as default. |
||
| 256 | * |
||
| 257 | * @return EvalLoader |
||
| 258 | */ |
||
| 259 | 273 | public static function getDefaultLoader() |
|
| 260 | { |
||
| 261 | 273 | return new EvalLoader(); |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set the container. |
||
| 266 | * |
||
| 267 | * @param \Mockery\Container $container |
||
| 268 | * |
||
| 269 | * @return \Mockery\Container |
||
| 270 | */ |
||
| 271 | 18 | public static function setContainer(Mockery\Container $container) |
|
| 272 | { |
||
| 273 | 18 | return self::$_container = $container; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Reset the container to null. |
||
| 278 | * |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | 15 | public static function resetContainer() |
|
| 282 | { |
||
| 283 | 15 | self::$_container = null; |
|
| 284 | 15 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Return instance of ANY matcher. |
||
| 288 | * |
||
| 289 | * @return \Mockery\Matcher\Any |
||
| 290 | */ |
||
| 291 | 5 | public static function any() |
|
| 292 | { |
||
| 293 | 5 | return new \Mockery\Matcher\Any(); |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Return instance of TYPE matcher. |
||
| 298 | * |
||
| 299 | * @param $expected |
||
| 300 | * |
||
| 301 | * @return \Mockery\Matcher\Type |
||
| 302 | */ |
||
| 303 | 48 | public static function type($expected) |
|
| 304 | { |
||
| 305 | 48 | return new \Mockery\Matcher\Type($expected); |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Return instance of DUCKTYPE matcher. |
||
| 310 | * |
||
| 311 | * @param array $args |
||
| 312 | * |
||
| 313 | * @return \Mockery\Matcher\Ducktype |
||
| 314 | */ |
||
| 315 | 3 | public static function ducktype(...$args) |
|
| 316 | { |
||
| 317 | 3 | return new \Mockery\Matcher\Ducktype($args); |
|
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return instance of SUBSET matcher. |
||
| 322 | * |
||
| 323 | * @param array $part |
||
| 324 | * @param bool $strict - (Optional) True for strict comparison, false for loose |
||
| 325 | * |
||
| 326 | * @return \Mockery\Matcher\Subset |
||
| 327 | */ |
||
| 328 | 3 | public static function subset(array $part, $strict = true) |
|
| 329 | { |
||
| 330 | 3 | return new \Mockery\Matcher\Subset($part, $strict); |
|
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Return instance of CONTAINS matcher. |
||
| 335 | * |
||
| 336 | * @param array $args |
||
| 337 | * |
||
| 338 | * @return \Mockery\Matcher\Contains |
||
| 339 | */ |
||
| 340 | 3 | public static function contains(...$args) |
|
| 341 | { |
||
| 342 | 3 | return new \Mockery\Matcher\Contains($args); |
|
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Return instance of HASKEY matcher. |
||
| 347 | * |
||
| 348 | * @param $key |
||
| 349 | * |
||
| 350 | * @return \Mockery\Matcher\HasKey |
||
| 351 | */ |
||
| 352 | 3 | public static function hasKey($key) |
|
| 353 | { |
||
| 354 | 3 | return new \Mockery\Matcher\HasKey($key); |
|
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Return instance of HASVALUE matcher. |
||
| 359 | * |
||
| 360 | * @param $val |
||
| 361 | * |
||
| 362 | * @return \Mockery\Matcher\HasValue |
||
| 363 | */ |
||
| 364 | 3 | public static function hasValue($val) |
|
| 365 | { |
||
| 366 | 3 | return new \Mockery\Matcher\HasValue($val); |
|
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return instance of CLOSURE matcher. |
||
| 371 | * |
||
| 372 | * @param $closure |
||
| 373 | * |
||
| 374 | * @return \Mockery\Matcher\Closure |
||
| 375 | */ |
||
| 376 | 7 | public static function on($closure) |
|
| 377 | { |
||
| 378 | 7 | return new \Mockery\Matcher\Closure($closure); |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Return instance of MUSTBE matcher. |
||
| 383 | * |
||
| 384 | * @param $expected |
||
| 385 | * |
||
| 386 | * @return \Mockery\Matcher\MustBe |
||
| 387 | */ |
||
| 388 | 6 | public static function mustBe($expected) |
|
| 389 | { |
||
| 390 | 6 | return new \Mockery\Matcher\MustBe($expected); |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return instance of NOT matcher. |
||
| 395 | * |
||
| 396 | * @param $expected |
||
| 397 | * |
||
| 398 | * @return \Mockery\Matcher\Not |
||
| 399 | */ |
||
| 400 | 3 | public static function not($expected) |
|
| 401 | { |
||
| 402 | 3 | return new \Mockery\Matcher\Not($expected); |
|
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Return instance of ANYOF matcher. |
||
| 407 | * |
||
| 408 | * @param array $args |
||
| 409 | * |
||
| 410 | * @return \Mockery\Matcher\AnyOf |
||
| 411 | */ |
||
| 412 | 3 | public static function anyOf(...$args) |
|
| 413 | { |
||
| 414 | 3 | return new \Mockery\Matcher\AnyOf($args); |
|
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return instance of NOTANYOF matcher. |
||
| 419 | * |
||
| 420 | * @param array $args |
||
| 421 | * |
||
| 422 | * @return \Mockery\Matcher\NotAnyOf |
||
| 423 | */ |
||
| 424 | 3 | public static function notAnyOf(...$args) |
|
| 425 | { |
||
| 426 | 3 | return new \Mockery\Matcher\NotAnyOf($args); |
|
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Lazy loader and Getter for the global |
||
| 431 | * configuration container. |
||
| 432 | * |
||
| 433 | * @return \Mockery\Configuration |
||
| 434 | */ |
||
| 435 | 425 | public static function getConfiguration() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Utility method to format method name and arguments into a string. |
||
| 446 | * |
||
| 447 | * @param string $method |
||
| 448 | * @param array $arguments |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | 92 | public static function formatArgs($method, array $arguments = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the string representation |
||
| 468 | * of any passed argument. |
||
| 469 | * |
||
| 470 | * @param $argument |
||
| 471 | * @param $depth |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 91 | private static function formatArgument($argument, $depth = 0) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Utility function to format objects to printable arrays. |
||
| 519 | * |
||
| 520 | * @param array $objects |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | 52 | public static function formatObjects(array $objects = null) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Utility function to turn public properties and public get* and is* method values into an array. |
||
| 555 | * |
||
| 556 | * @param $object |
||
| 557 | * @param int $nesting |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | 7 | private static function objectToArray($object, $nesting = 3) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Returns all public instance properties. |
||
| 576 | * |
||
| 577 | * @param $object |
||
| 578 | * @param $nesting |
||
| 579 | * |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | 7 | private static function extractInstancePublicProperties($object, $nesting) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Returns all object getters. |
||
| 600 | * |
||
| 601 | * @param $object |
||
| 602 | * @param $nesting |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 7 | private static function extractGetters($object, $nesting) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Utility method used for recursively generating |
||
| 634 | * an object or array representation. |
||
| 635 | * |
||
| 636 | * @param $argument |
||
| 637 | * @param $nesting |
||
| 638 | * |
||
| 639 | * @return mixed |
||
| 640 | */ |
||
| 641 | 1 | private static function cleanupNesting($argument, $nesting) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Utility method for recursively |
||
| 659 | * gerating a representation |
||
| 660 | * of the given array. |
||
| 661 | * |
||
| 662 | * @param array $argument |
||
| 663 | * @param int $nesting |
||
| 664 | * |
||
| 665 | * @return mixed |
||
| 666 | */ |
||
| 667 | 1 | private static function cleanupArray($argument, $nesting = 3) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Utility function to parse shouldReceive() arguments and generate |
||
| 686 | * expectations from such as needed. |
||
| 687 | * |
||
| 688 | * @param Mockery\MockInterface $mock |
||
| 689 | * @param array $args |
||
| 690 | * @param callable $add |
||
| 691 | * @return \Mockery\CompositeExpectation |
||
| 692 | */ |
||
| 693 | 307 | public static function parseShouldReturnArgs(\Mockery\MockInterface $mock, $args, $add) |
|
| 711 | |||
| 712 | /** |
||
| 713 | * Sets up expectations on the members of the CompositeExpectation and |
||
| 714 | * builds up any demeter chain that was passed to shouldReceive. |
||
| 715 | * |
||
| 716 | * @param \Mockery\MockInterface $mock |
||
| 717 | * @param string $arg |
||
| 718 | * @param callable $add |
||
| 719 | * @throws Mockery\Exception |
||
| 720 | * @return \Mockery\ExpectationInterface |
||
| 721 | */ |
||
| 722 | 307 | protected static function buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Gets a new demeter configured |
||
| 776 | * mock from the container. |
||
| 777 | * |
||
| 778 | * @param \Mockery\Container $container |
||
| 779 | * @param string $method |
||
| 780 | * @param Mockery\ExpectationInterface $exp |
||
| 781 | * |
||
| 782 | * @return \Mockery\Mock |
||
| 783 | */ |
||
| 784 | 11 | private static function getNewDemeterMock( |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Gets an specific demeter mock from |
||
| 797 | * the ones kept by the container. |
||
| 798 | * |
||
| 799 | * @param \Mockery\Container $container |
||
| 800 | * @param string $demeterMockKey |
||
| 801 | * |
||
| 802 | * @return mixed |
||
| 803 | */ |
||
| 804 | 5 | private static function getExistingDemeterMock( |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Checks if the passed array representing a demeter |
||
| 816 | * chain with the method names is empty. |
||
| 817 | * |
||
| 818 | * @param array $methodNames |
||
| 819 | * |
||
| 820 | * @return bool |
||
| 821 | */ |
||
| 822 | 301 | private static function noMoreElementsInChain(array $methodNames) |
|
| 826 | |||
| 827 | 17 | public static function declareClass($fqn) |
|
| 831 | |||
| 832 | 2 | public static function declareInterface($fqn) |
|
| 836 | |||
| 837 | 18 | private static function declareType($fqn, $type) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Register a file to be deleted on tearDown. |
||
| 866 | * |
||
| 867 | * @param string $fileName |
||
| 868 | */ |
||
| 869 | 18 | public static function registerFileForCleanUp($fileName) |
|
| 873 | } |
||
| 874 |
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: