Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Container |
||
| 28 | { |
||
| 29 | const BLOCKS = \Mockery::BLOCKS; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Store of mock objects |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $_mocks = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Order number of allocation |
||
| 40 | * |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $_allocatedOrder = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Current ordered number |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $_currentOrder = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Ordered groups |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $_groups = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Generator\Generator |
||
| 61 | */ |
||
| 62 | protected $_generator; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var LoaderInterface |
||
| 66 | */ |
||
| 67 | protected $_loader; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $_namedMocks = array(); |
||
| 73 | |||
| 74 | 420 | public function __construct(Generator $generator = null, LoaderInterface $loader = null) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Generates a new mock object for this container |
||
| 82 | * |
||
| 83 | * I apologies in advance for this. A God Method just fits the API which |
||
| 84 | * doesn't require differentiating between classes, interfaces, abstracts, |
||
| 85 | * names or partials - just so long as it's something that can be mocked. |
||
| 86 | * I'll refactor it one day so it's easier to follow. |
||
| 87 | * |
||
| 88 | * @throws Exception\RuntimeException |
||
| 89 | * @throws Exception |
||
| 90 | * @return \Mockery\Mock |
||
| 91 | */ |
||
| 92 | 419 | public function mock() |
|
| 93 | { |
||
| 94 | 419 | $expectationClosure = null; |
|
| 95 | 419 | $quickdefs = array(); |
|
| 96 | 419 | $constructorArgs = null; |
|
| 97 | 419 | $blocks = array(); |
|
| 98 | 419 | $args = func_get_args(); |
|
| 99 | |||
| 100 | 419 | if (count($args) > 1) { |
|
| 101 | 25 | $finalArg = end($args); |
|
| 102 | 25 | reset($args); |
|
| 103 | 25 | if (is_callable($finalArg) && is_object($finalArg)) { |
|
| 104 | 1 | $expectationClosure = array_pop($args); |
|
| 105 | 1 | } |
|
| 106 | 25 | } |
|
| 107 | |||
| 108 | 419 | $builder = new MockConfigurationBuilder(); |
|
| 109 | |||
| 110 | 419 | foreach ($args as $k => $arg) { |
|
| 111 | 408 | if ($arg instanceof MockConfigurationBuilder) { |
|
| 112 | 5 | $builder = $arg; |
|
| 113 | 5 | unset($args[$k]); |
|
| 114 | 5 | } |
|
| 115 | 419 | } |
|
| 116 | 419 | reset($args); |
|
| 117 | |||
| 118 | 419 | $builder->setParameterOverrides(\Mockery::getConfiguration()->getInternalClassMethodParamMaps()); |
|
| 119 | |||
| 120 | 419 | while (count($args) > 0) { |
|
| 121 | 406 | $arg = current($args); |
|
| 122 | // check for multiple interfaces |
||
| 123 | 406 | if (is_string($arg) && strpos($arg, ',') && !strpos($arg, ']')) { |
|
| 124 | 6 | $interfaces = explode(',', str_replace(' ', '', $arg)); |
|
| 125 | 6 | foreach ($interfaces as $i) { |
|
| 126 | 6 | if (!interface_exists($i, true) && !class_exists($i, true)) { |
|
| 127 | 1 | throw new \Mockery\Exception( |
|
| 128 | 'Class name follows the format for defining multiple' |
||
| 129 | . ' interfaces, however one or more of the interfaces' |
||
| 130 | 1 | . ' do not exist or are not included, or the base class' |
|
| 131 | 1 | . ' (which you may omit from the mock definition) does not exist' |
|
| 132 | 1 | ); |
|
| 133 | } |
||
| 134 | 5 | } |
|
| 135 | 5 | $builder->addTargets($interfaces); |
|
| 136 | 5 | array_shift($args); |
|
| 137 | |||
| 138 | 5 | continue; |
|
| 139 | 401 | } elseif (is_string($arg) && substr($arg, 0, 6) == 'alias:') { |
|
| 140 | 4 | $name = array_shift($args); |
|
| 141 | 4 | $name = str_replace('alias:', '', $name); |
|
| 142 | 4 | $builder->addTarget('stdClass'); |
|
| 143 | 4 | $builder->setName($name); |
|
| 144 | 4 | continue; |
|
| 145 | 397 | } elseif (is_string($arg) && substr($arg, 0, 9) == 'overload:') { |
|
| 146 | 11 | $name = array_shift($args); |
|
| 147 | 11 | $name = str_replace('overload:', '', $name); |
|
| 148 | 11 | $builder->setInstanceMock(true); |
|
| 149 | 11 | $builder->addTarget('stdClass'); |
|
| 150 | 11 | $builder->setName($name); |
|
| 151 | 11 | continue; |
|
| 152 | 386 | } elseif (is_string($arg) && substr($arg, strlen($arg)-1, 1) == ']') { |
|
| 153 | 17 | $parts = explode('[', $arg); |
|
| 154 | 17 | if (!class_exists($parts[0], true) && !interface_exists($parts[0], true)) { |
|
| 155 | 1 | throw new \Mockery\Exception('Can only create a partial mock from' |
|
| 156 | 1 | . ' an existing class or interface'); |
|
| 157 | } |
||
| 158 | 17 | $class = $parts[0]; |
|
| 159 | 16 | $parts[1] = str_replace(' ', '', $parts[1]); |
|
| 160 | 16 | $partialMethods = explode(',', strtolower(rtrim($parts[1], ']'))); |
|
| 161 | 16 | $builder->addTarget($class); |
|
| 162 | 16 | $builder->setWhiteListedMethods($partialMethods); |
|
| 163 | 16 | array_shift($args); |
|
| 164 | 16 | continue; |
|
| 165 | 375 | } elseif (is_string($arg) && (class_exists($arg, true) || interface_exists($arg, true))) { |
|
| 166 | 334 | $class = array_shift($args); |
|
| 167 | 334 | $builder->addTarget($class); |
|
| 168 | 334 | continue; |
|
| 169 | 63 | } elseif (is_string($arg)) { |
|
| 170 | 18 | if (!$this->isValidClassName($arg)) { |
|
| 171 | 1 | throw new \Mockery\Exception('Class name contains invalid characters'); |
|
| 172 | } |
||
| 173 | 17 | $class = array_shift($args); |
|
| 174 | 17 | $builder->addTarget($class); |
|
| 175 | 17 | continue; |
|
| 176 | 46 | } elseif (is_object($arg)) { |
|
| 177 | 24 | $partial = array_shift($args); |
|
| 178 | 24 | $builder->addTarget($partial); |
|
| 179 | 24 | continue; |
|
| 180 | 24 | } elseif (is_array($arg) && !empty($arg) && array_keys($arg) !== range(0, count($arg) - 1)) { |
|
| 181 | // if associative array |
||
| 182 | 15 | if (array_key_exists(self::BLOCKS, $arg)) { |
|
| 183 | 2 | $blocks = $arg[self::BLOCKS]; |
|
| 184 | 2 | } |
|
| 185 | 15 | unset($arg[self::BLOCKS]); |
|
| 186 | 15 | $quickdefs = array_shift($args); |
|
| 187 | 15 | continue; |
|
| 188 | 10 | } elseif (is_array($arg)) { |
|
| 189 | 10 | $constructorArgs = array_shift($args); |
|
| 190 | 10 | continue; |
|
| 191 | } |
||
| 192 | |||
| 193 | throw new \Mockery\Exception( |
||
| 194 | 'Unable to parse arguments sent to ' |
||
| 195 | . get_class($this) . '::mock()' |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | 416 | $builder->addBlackListedMethods($blocks); |
|
| 200 | |||
| 201 | 416 | if (defined('HHVM_VERSION') |
|
| 202 | 416 | && isset($class) |
|
| 203 | 416 | && ($class === 'Exception' || is_subclass_of($class, 'Exception'))) { |
|
| 204 | $builder->addBlackListedMethod("setTraceOptions"); |
||
| 205 | $builder->addBlackListedMethod("getTraceOptions"); |
||
| 206 | } |
||
| 207 | |||
| 208 | 416 | if (!is_null($constructorArgs)) { |
|
| 209 | 10 | $builder->addBlackListedMethod("__construct"); // we need to pass through |
|
| 210 | 10 | } else { |
|
| 211 | 406 | $builder->setMockOriginalDestructor(true); |
|
| 212 | } |
||
| 213 | |||
| 214 | 416 | if (!empty($partialMethods) && $constructorArgs === null) { |
|
| 215 | 11 | $constructorArgs = array(); |
|
| 216 | 11 | } |
|
| 217 | |||
| 218 | 416 | $config = $builder->getMockConfiguration(); |
|
| 219 | |||
| 220 | 416 | $this->checkForNamedMockClashes($config); |
|
| 221 | |||
| 222 | 416 | $def = $this->getGenerator()->generate($config); |
|
| 223 | |||
| 224 | 415 | if (class_exists($def->getClassName(), $attemptAutoload = false)) { |
|
| 225 | 25 | $rfc = new \ReflectionClass($def->getClassName()); |
|
| 226 | 25 | if (!$rfc->implementsInterface("Mockery\MockInterface")) { |
|
| 227 | 1 | throw new \Mockery\Exception\RuntimeException("Could not load mock {$def->getClassName()}, class already exists"); |
|
| 228 | } |
||
| 229 | 24 | } |
|
| 230 | |||
| 231 | 414 | $this->getLoader()->load($def); |
|
| 232 | |||
| 233 | 414 | $mock = $this->_getInstance($def->getClassName(), $constructorArgs); |
|
| 234 | 414 | $mock->mockery_init($this, $config->getTargetObject()); |
|
| 235 | |||
| 236 | 414 | if (!empty($quickdefs)) { |
|
| 237 | 15 | $mock->shouldReceive($quickdefs)->byDefault(); |
|
| 238 | 15 | } |
|
| 239 | 414 | if (!empty($expectationClosure)) { |
|
| 240 | 1 | $expectationClosure($mock); |
|
| 241 | 1 | } |
|
| 242 | 414 | $this->rememberMock($mock); |
|
| 243 | 414 | return $mock; |
|
| 244 | } |
||
| 245 | |||
| 246 | public function instanceMock() |
||
| 249 | |||
| 250 | 414 | public function getLoader() |
|
| 254 | |||
| 255 | 416 | public function getGenerator() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $method |
||
| 262 | * @return string|null |
||
| 263 | */ |
||
| 264 | 7 | public function getKeyOfDemeterMockFor($method) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | 5 | public function getMocks() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Tear down tasks for this container |
||
| 287 | * |
||
| 288 | * @throws \Exception |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 411 | public function mockery_teardown() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Verify the container mocks |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | 411 | public function mockery_verify() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Reset the container to its original state |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | 418 | public function mockery_close() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Fetch the next available allocation order number |
||
| 328 | * |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | 1 | public function mockery_allocateOrder() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Set ordering for a group |
||
| 339 | * |
||
| 340 | * @param mixed $group |
||
| 341 | * @param int $order |
||
| 342 | */ |
||
| 343 | public function mockery_setGroup($group, $order) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Fetch array of ordered groups |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | 1 | public function mockery_getGroups() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Set current ordered number |
||
| 360 | * |
||
| 361 | * @param int $order |
||
| 362 | * @return int The current order number that was set |
||
| 363 | */ |
||
| 364 | 6 | public function mockery_setCurrentOrder($order) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get current ordered number |
||
| 372 | * |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public function mockery_getCurrentOrder() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Validate the current mock's ordering |
||
| 382 | * |
||
| 383 | * @param string $method |
||
| 384 | * @param int $order |
||
| 385 | * @throws \Mockery\Exception |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 1 | public function mockery_validateOrder($method, $order, \Mockery\MockInterface $mock) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Gets the count of expectations on the mocks |
||
| 406 | * |
||
| 407 | * @return int |
||
| 408 | */ |
||
| 409 | 411 | public function mockery_getExpectationCount() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Store a mock and set its container reference |
||
| 420 | * |
||
| 421 | * @param \Mockery\Mock |
||
| 422 | * @return \Mockery\Mock |
||
| 423 | */ |
||
| 424 | 414 | public function rememberMock(\Mockery\MockInterface $mock) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Retrieve the last remembered mock object, which is the same as saying |
||
| 440 | * retrieve the current mock being programmed where you have yet to call |
||
| 441 | * mock() to change it - thus why the method name is "self" since it will be |
||
| 442 | * be used during the programming of the same mock. |
||
| 443 | * |
||
| 444 | * @return \Mockery\Mock |
||
| 445 | */ |
||
| 446 | 2 | public function self() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Return a specific remembered mock according to the array index it |
||
| 455 | * was stored to in this container instance |
||
| 456 | * |
||
| 457 | * @return \Mockery\Mock |
||
| 458 | */ |
||
| 459 | 11 | public function fetchMock($reference) |
|
| 465 | |||
| 466 | 414 | protected function _getInstance($mockName, $constructorArgs = null) |
|
| 490 | |||
| 491 | 416 | protected function checkForNamedMockClashes($config) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * see http://php.net/manual/en/language.oop5.basic.php |
||
| 514 | * @param string $className |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | 23 | public function isValidClassName($className) |
|
| 529 | } |
||
| 530 |