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 | 413 | 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 | 416 | public function mock() |
|
| 237 | |||
| 238 | public function instanceMock() |
||
| 241 | |||
| 242 | 412 | public function getLoader() |
|
| 246 | |||
| 247 | 414 | public function getGenerator() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $method |
||
| 254 | * @return string|null |
||
| 255 | */ |
||
| 256 | 7 | public function getKeyOfDemeterMockFor($method) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | 5 | public function getMocks() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Tear down tasks for this container |
||
| 279 | * |
||
| 280 | * @throws \Exception |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | 404 | public function mockery_teardown() |
|
| 284 | { |
||
| 285 | try { |
||
| 286 | 404 | $this->mockery_verify(); |
|
| 287 | 404 | } catch (\Exception $e) { |
|
| 288 | 1 | $this->mockery_close(); |
|
| 289 | 1 | throw $e; |
|
| 290 | } |
||
| 291 | 403 | } |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Verify the container mocks |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | 404 | public function mockery_verify() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Reset the container to its original state |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | 411 | public function mockery_close() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Fetch the next available allocation order number |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | 1 | public function mockery_allocateOrder() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set ordering for a group |
||
| 331 | * |
||
| 332 | * @param mixed $group |
||
| 333 | * @param int $order |
||
| 334 | */ |
||
| 335 | public function mockery_setGroup($group, $order) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Fetch array of ordered groups |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 1 | public function mockery_getGroups() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Set current ordered number |
||
| 352 | * |
||
| 353 | * @param int $order |
||
| 354 | * @return int The current order number that was set |
||
| 355 | */ |
||
| 356 | 1 | public function mockery_setCurrentOrder($order) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get current ordered number |
||
| 364 | * |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | 1 | public function mockery_getCurrentOrder() |
|
| 368 | { |
||
| 369 | return $this->_currentOrder; |
||
| 370 | 1 | } |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Validate the current mock's ordering |
||
| 374 | * |
||
| 375 | * @param string $method |
||
| 376 | * @param int $order |
||
| 377 | * @throws \Mockery\Exception |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | 139 | public function mockery_validateOrder($method, $order, \Mockery\MockInterface $mock) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Gets the count of expectations on the mocks |
||
| 398 | * |
||
| 399 | * @return int |
||
| 400 | */ |
||
| 401 | 402 | public function mockery_getExpectationCount() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Store a mock and set its container reference |
||
| 412 | * |
||
| 413 | * @param \Mockery\Mock |
||
| 414 | * @return \Mockery\Mock |
||
| 415 | */ |
||
| 416 | 412 | public function rememberMock(\Mockery\MockInterface $mock) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Retrieve the last remembered mock object, which is the same as saying |
||
| 432 | * retrieve the current mock being programmed where you have yet to call |
||
| 433 | * mock() to change it - thus why the method name is "self" since it will be |
||
| 434 | * be used during the programming of the same mock. |
||
| 435 | * |
||
| 436 | * @return \Mockery\Mock |
||
| 437 | */ |
||
| 438 | 3 | public function self() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Return a specific remembered mock according to the array index it |
||
| 447 | * was stored to in this container instance |
||
| 448 | * |
||
| 449 | * @return \Mockery\Mock |
||
| 450 | */ |
||
| 451 | 11 | public function fetchMock($reference) |
|
| 457 | |||
| 458 | 412 | protected function _getInstance($mockName, $constructorArgs = null) |
|
| 482 | |||
| 483 | 414 | protected function checkForNamedMockClashes($config) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * see http://php.net/manual/en/language.oop5.basic.php |
||
| 506 | * @param string $className |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | 22 | public function isValidClassName($className) |
|
| 521 | } |
||
| 522 |