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() |
|
247 | |||
248 | public function instanceMock() |
||
251 | |||
252 | 414 | public function getLoader() |
|
256 | |||
257 | 416 | public function getGenerator() |
|
261 | |||
262 | /** |
||
263 | * @param string $method |
||
264 | * @return string|null |
||
265 | */ |
||
266 | 7 | public function getKeyOfDemeterMockFor($method) |
|
278 | |||
279 | /** |
||
280 | * @return array |
||
281 | */ |
||
282 | 5 | public function getMocks() |
|
286 | |||
287 | /** |
||
288 | * Tear down tasks for this container |
||
289 | * |
||
290 | * @throws \Exception |
||
291 | * @return void |
||
292 | */ |
||
293 | 411 | public function mockery_teardown() |
|
302 | |||
303 | /** |
||
304 | * Verify the container mocks |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | 411 | public function mockery_verify() |
|
314 | |||
315 | /** |
||
316 | * Reset the container to its original state |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | 418 | public function mockery_close() |
|
327 | |||
328 | /** |
||
329 | * Fetch the next available allocation order number |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | 1 | public function mockery_allocateOrder() |
|
338 | |||
339 | /** |
||
340 | * Set ordering for a group |
||
341 | * |
||
342 | * @param mixed $group |
||
343 | * @param int $order |
||
344 | */ |
||
345 | public function mockery_setGroup($group, $order) |
||
349 | |||
350 | /** |
||
351 | * Fetch array of ordered groups |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | 1 | public function mockery_getGroups() |
|
359 | |||
360 | /** |
||
361 | * Set current ordered number |
||
362 | * |
||
363 | * @param int $order |
||
364 | * @return int The current order number that was set |
||
365 | */ |
||
366 | 137 | public function mockery_setCurrentOrder($order) |
|
371 | |||
372 | /** |
||
373 | * Get current ordered number |
||
374 | * |
||
375 | * @return int |
||
376 | */ |
||
377 | public function mockery_getCurrentOrder() |
||
381 | |||
382 | /** |
||
383 | * Validate the current mock's ordering |
||
384 | * |
||
385 | * @param string $method |
||
386 | * @param int $order |
||
387 | * @throws \Mockery\Exception |
||
388 | * @return void |
||
389 | */ |
||
390 | 1 | public function mockery_validateOrder($method, $order, \Mockery\MockInterface $mock) |
|
405 | |||
406 | /** |
||
407 | * Gets the count of expectations on the mocks |
||
408 | * |
||
409 | * @return int |
||
410 | */ |
||
411 | 411 | public function mockery_getExpectationCount() |
|
419 | |||
420 | /** |
||
421 | * Store a mock and set its container reference |
||
422 | * |
||
423 | * @param \Mockery\Mock |
||
424 | * @return \Mockery\Mock |
||
425 | */ |
||
426 | 414 | public function rememberMock(\Mockery\MockInterface $mock) |
|
439 | |||
440 | /** |
||
441 | * Retrieve the last remembered mock object, which is the same as saying |
||
442 | * retrieve the current mock being programmed where you have yet to call |
||
443 | * mock() to change it - thus why the method name is "self" since it will be |
||
444 | * be used during the programming of the same mock. |
||
445 | * |
||
446 | * @return \Mockery\Mock |
||
447 | */ |
||
448 | 2 | public function self() |
|
454 | |||
455 | /** |
||
456 | * Return a specific remembered mock according to the array index it |
||
457 | * was stored to in this container instance |
||
458 | * |
||
459 | * @return \Mockery\Mock |
||
460 | */ |
||
461 | 11 | public function fetchMock($reference) |
|
467 | |||
468 | 414 | protected function _getInstance($mockName, $constructorArgs = null) |
|
492 | |||
493 | 416 | protected function checkForNamedMockClashes($config) |
|
513 | |||
514 | /** |
||
515 | * see http://php.net/manual/en/language.oop5.basic.php |
||
516 | * @param string $className |
||
517 | * @return bool |
||
518 | */ |
||
519 | 22 | public function isValidClassName($className) |
|
531 | } |
||
532 |