Complex classes like MockConfiguration 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 MockConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class MockConfiguration |
||
| 28 | { |
||
| 29 | protected static $mockCounter = 0; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * A class that we'd like to mock |
||
| 33 | */ |
||
| 34 | protected $targetClass; |
||
| 35 | protected $targetClassName; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A number of interfaces we'd like to mock, keyed by name to attempt to |
||
| 39 | * keep unique |
||
| 40 | */ |
||
| 41 | protected $targetInterfaces = array(); |
||
| 42 | protected $targetInterfaceNames = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * An object we'd like our mock to proxy to |
||
| 46 | */ |
||
| 47 | protected $targetObject; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The class name we'd like to use for a generated mock |
||
| 51 | */ |
||
| 52 | protected $name; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Methods that should specifically not be mocked |
||
| 56 | * |
||
| 57 | * This is currently populated with stuff we don't know how to deal with, |
||
| 58 | * should really be somewhere else |
||
| 59 | */ |
||
| 60 | protected $blackListedMethods = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * If not empty, only these methods will be mocked |
||
| 64 | */ |
||
| 65 | protected $whiteListedMethods = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * An instance mock is where we override the original class before it's |
||
| 69 | * autoloaded |
||
| 70 | */ |
||
| 71 | protected $instanceMock = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Param overrides |
||
| 75 | */ |
||
| 76 | protected $parameterOverrides = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Instance cache of all methods |
||
| 80 | */ |
||
| 81 | protected $allMethods; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * If true, overrides original class destructor |
||
| 85 | */ |
||
| 86 | protected $mockOriginalDestructor = false; |
||
| 87 | |||
| 88 | 436 | public function __construct( |
|
| 89 | array $targets = array(), |
||
| 90 | array $blackListedMethods = array(), |
||
| 91 | array $whiteListedMethods = array(), |
||
| 92 | $name = null, |
||
| 93 | $instanceMock = false, |
||
| 94 | array $parameterOverrides = array(), |
||
| 95 | $mockOriginalDestructor = false |
||
| 96 | ) { |
||
| 97 | 436 | $this->addTargets($targets); |
|
| 98 | 436 | $this->blackListedMethods = $blackListedMethods; |
|
| 99 | 436 | $this->whiteListedMethods = $whiteListedMethods; |
|
| 100 | 436 | $this->name = $name; |
|
| 101 | 436 | $this->instanceMock = $instanceMock; |
|
| 102 | 436 | $this->parameterOverrides = $parameterOverrides; |
|
| 103 | 436 | $this->mockOriginalDestructor = $mockOriginalDestructor; |
|
| 104 | 436 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Attempt to create a hash of the configuration, in order to allow caching |
||
| 108 | * |
||
| 109 | * @TODO workout if this will work |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 416 | public function getHash() |
|
| 114 | { |
||
| 115 | $vars = array( |
||
| 116 | 416 | 'targetClassName' => $this->targetClassName, |
|
| 117 | 416 | 'targetInterfaceNames' => $this->targetInterfaceNames, |
|
| 118 | 416 | 'name' => $this->name, |
|
| 119 | 416 | 'blackListedMethods' => $this->blackListedMethods, |
|
| 120 | 416 | 'whiteListedMethod' => $this->whiteListedMethods, |
|
| 121 | 416 | 'instanceMock' => $this->instanceMock, |
|
| 122 | 416 | 'parameterOverrides' => $this->parameterOverrides, |
|
| 123 | 416 | 'mockOriginalDestructor' => $this->mockOriginalDestructor |
|
| 124 | 416 | ); |
|
| 125 | |||
| 126 | 416 | return md5(serialize($vars)); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Gets a list of methods from the classes, interfaces and objects and |
||
| 131 | * filters them appropriately. Lot's of filtering going on, perhaps we could |
||
| 132 | * have filter classes to iterate through |
||
| 133 | */ |
||
| 134 | 400 | public function getMethodsToMock() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * We declare the __call method to handle undefined stuff, if the class |
||
| 185 | * we're mocking has also defined it, we need to comply with their interface |
||
| 186 | */ |
||
| 187 | 393 | public function requiresCallTypeHintRemoval() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * We declare the __callStatic method to handle undefined stuff, if the class |
||
| 201 | * we're mocking has also defined it, we need to comply with their interface |
||
| 202 | */ |
||
| 203 | 392 | public function requiresCallStaticTypeHintRemoval() |
|
| 214 | |||
| 215 | 392 | public function rename($className) |
|
| 216 | { |
||
| 217 | 392 | $targets = array(); |
|
| 218 | |||
| 219 | 392 | if ($this->targetClassName) { |
|
| 220 | 372 | $targets[] = $this->targetClassName; |
|
| 221 | 372 | } |
|
| 222 | |||
| 223 | 392 | if ($this->targetInterfaceNames) { |
|
| 224 | 19 | $targets = array_merge($targets, $this->targetInterfaceNames); |
|
| 225 | 19 | } |
|
| 226 | |||
| 227 | 392 | if ($this->targetObject) { |
|
| 228 | 14 | $targets[] = $this->targetObject; |
|
| 229 | 14 | } |
|
| 230 | |||
| 231 | 392 | return new self( |
|
| 232 | 392 | $targets, |
|
| 233 | 392 | $this->blackListedMethods, |
|
| 234 | 392 | $this->whiteListedMethods, |
|
| 235 | 392 | $className, |
|
| 236 | 392 | $this->instanceMock, |
|
| 237 | 392 | $this->parameterOverrides, |
|
| 238 | 392 | $this->mockOriginalDestructor |
|
| 239 | 392 | ); |
|
| 240 | } |
||
| 241 | |||
| 242 | 407 | protected function addTarget($target) |
|
| 243 | { |
||
| 244 | 407 | if (is_object($target)) { |
|
| 245 | 14 | $this->setTargetObject($target); |
|
| 246 | 14 | $this->setTargetClassName(get_class($target)); |
|
| 247 | 14 | return $this; |
|
| 248 | } |
||
| 249 | |||
| 250 | 407 | if ($target[0] !== "\\") { |
|
| 251 | 404 | $target = "\\" . $target; |
|
| 252 | 404 | } |
|
| 253 | |||
| 254 | 407 | if (class_exists($target)) { |
|
| 255 | 381 | $this->setTargetClassName($target); |
|
| 256 | 381 | return $this; |
|
| 257 | } |
||
| 258 | |||
| 259 | 40 | if (interface_exists($target)) { |
|
| 260 | 24 | $this->addTargetInterfaceName($target); |
|
| 261 | 24 | return $this; |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Default is to set as class, or interface if class already set |
||
| 266 | * |
||
| 267 | * Don't like this condition, can't remember what the default |
||
| 268 | * targetClass is for |
||
| 269 | */ |
||
| 270 | 17 | if ($this->getTargetClassName()) { |
|
| 271 | 1 | $this->addTargetInterfaceName($target); |
|
| 272 | 1 | return $this; |
|
| 273 | } |
||
| 274 | |||
| 275 | 17 | $this->setTargetClassName($target); |
|
| 276 | 17 | } |
|
| 277 | |||
| 278 | 436 | protected function addTargets($interfaces) |
|
| 284 | |||
| 285 | 17 | public function getTargetClassName() |
|
| 289 | |||
| 290 | 403 | public function getTargetClass() |
|
| 320 | |||
| 321 | 405 | public function getTargetInterfaces() |
|
| 322 | { |
||
| 323 | 405 | if (!empty($this->targetInterfaces)) { |
|
| 324 | 19 | return $this->targetInterfaces; |
|
| 325 | } |
||
| 326 | |||
| 327 | 405 | foreach ($this->targetInterfaceNames as $targetInterface) { |
|
| 328 | 24 | if (!interface_exists($targetInterface)) { |
|
| 329 | 1 | $this->targetInterfaces[] = UndefinedTargetClass::factory($targetInterface); |
|
| 330 | 1 | return; |
|
| 331 | } |
||
| 332 | |||
| 333 | 24 | $dtc = DefinedTargetClass::factory($targetInterface); |
|
| 334 | 24 | $extendedInterfaces = array_keys($dtc->getInterfaces()); |
|
| 335 | 24 | $extendedInterfaces[] = $targetInterface; |
|
| 336 | |||
| 337 | 24 | $traversableFound = false; |
|
| 338 | 24 | $iteratorShiftedToFront = false; |
|
| 339 | 24 | foreach ($extendedInterfaces as $interface) { |
|
| 340 | 24 | if (!$traversableFound && preg_match("/^\\?Iterator(|Aggregate)$/i", $interface)) { |
|
| 341 | break; |
||
| 342 | } |
||
| 343 | |||
| 344 | 24 | if (preg_match("/^\\\\?IteratorAggregate$/i", $interface)) { |
|
| 345 | 3 | $this->targetInterfaces[] = DefinedTargetClass::factory("\\IteratorAggregate"); |
|
| 346 | 3 | $iteratorShiftedToFront = true; |
|
| 347 | 24 | } elseif (preg_match("/^\\\\?Iterator$/i", $interface)) { |
|
| 348 | 3 | $this->targetInterfaces[] = DefinedTargetClass::factory("\\Iterator"); |
|
| 349 | 3 | $iteratorShiftedToFront = true; |
|
| 350 | 24 | } elseif (preg_match("/^\\\\?Traversable$/i", $interface)) { |
|
| 351 | 11 | $traversableFound = true; |
|
| 352 | 11 | } |
|
| 353 | 24 | } |
|
| 354 | |||
| 355 | 24 | if ($traversableFound && !$iteratorShiftedToFront) { |
|
| 356 | 5 | $this->targetInterfaces[] = DefinedTargetClass::factory("\\IteratorAggregate"); |
|
| 357 | 5 | } |
|
| 358 | |||
| 359 | /** |
||
| 360 | * We never straight up implement Traversable |
||
| 361 | */ |
||
| 362 | 24 | if (!preg_match("/^\\\\?Traversable$/i", $targetInterface)) { |
|
| 363 | 23 | $this->targetInterfaces[] = $dtc; |
|
| 364 | 23 | } |
|
| 365 | 405 | } |
|
| 366 | 404 | $this->targetInterfaces = array_unique($this->targetInterfaces); // just in case |
|
| 367 | 404 | return $this->targetInterfaces; |
|
| 368 | } |
||
| 369 | |||
| 370 | 423 | public function getTargetObject() |
|
| 374 | |||
| 375 | 422 | public function getName() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Generate a suitable name based on the config |
||
| 382 | */ |
||
| 383 | 373 | public function generateName() |
|
| 404 | |||
| 405 | 396 | public function getShortName() |
|
| 410 | |||
| 411 | 396 | public function getNamespaceName() |
|
| 422 | |||
| 423 | 382 | public function getBlackListedMethods() |
|
| 427 | |||
| 428 | 400 | public function getWhiteListedMethods() |
|
| 432 | |||
| 433 | 393 | public function isInstanceMock() |
|
| 437 | |||
| 438 | 107 | public function getParameterOverrides() |
|
| 442 | |||
| 443 | 372 | public function isMockOriginalDestructor() |
|
| 444 | { |
||
| 445 | 372 | return $this->mockOriginalDestructor; |
|
| 446 | } |
||
| 447 | |||
| 448 | 391 | protected function setTargetClassName($targetClassName) |
|
| 452 | |||
| 453 | 401 | protected function getAllMethods() |
|
| 482 | |||
| 483 | /** |
||
| 484 | * If we attempt to implement Traversable, we must ensure we are also |
||
| 485 | * implementing either Iterator or IteratorAggregate, and that whichever one |
||
| 486 | * it is comes before Traversable in the list of implements. |
||
| 487 | */ |
||
| 488 | 24 | protected function addTargetInterfaceName($targetInterface) |
|
| 492 | |||
| 493 | |||
| 494 | 14 | protected function setTargetObject($object) |
|
| 498 | } |
||
| 499 |