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 |
||
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 | 422 | 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 | * @param array $args |
||
89 | * |
||
90 | * @return Mock |
||
91 | * @throws Exception\RuntimeException |
||
92 | */ |
||
93 | 426 | public function mock(...$args) |
|
94 | { |
||
95 | 426 | $expectationClosure = null; |
|
96 | 426 | $quickdefs = array(); |
|
97 | 426 | $constructorArgs = null; |
|
98 | 426 | $blocks = array(); |
|
99 | |||
100 | 426 | if (count($args) > 1) { |
|
101 | 27 | $finalArg = end($args); |
|
102 | 27 | reset($args); |
|
103 | 27 | if (is_callable($finalArg) && is_object($finalArg)) { |
|
104 | 1 | $expectationClosure = array_pop($args); |
|
105 | 1 | } |
|
106 | 27 | } |
|
107 | |||
108 | 426 | $builder = new MockConfigurationBuilder(); |
|
109 | |||
110 | 426 | foreach ($args as $k => $arg) { |
|
111 | 405 | if ($arg instanceof MockConfigurationBuilder) { |
|
112 | 6 | $builder = $arg; |
|
113 | 6 | unset($args[$k]); |
|
114 | 6 | } |
|
115 | 426 | } |
|
116 | 426 | reset($args); |
|
117 | |||
118 | 426 | $builder->setParameterOverrides(\Mockery::getConfiguration()->getInternalClassMethodParamMaps()); |
|
119 | |||
120 | 426 | while (count($args) > 0) { |
|
121 | 402 | $arg = current($args); |
|
122 | // check for multiple interfaces |
||
123 | 402 | if (is_string($arg) && strpos($arg, ',') && !strpos($arg, ']')) { |
|
124 | 6 | $interfaces = explode(',', str_replace(' ', '', $arg)); |
|
125 | 6 | $builder->addTargets($interfaces); |
|
126 | 6 | array_shift($args); |
|
127 | |||
128 | 6 | continue; |
|
129 | 397 | } elseif (is_string($arg) && substr($arg, 0, 6) == 'alias:') { |
|
130 | 4 | $name = array_shift($args); |
|
131 | 4 | $name = str_replace('alias:', '', $name); |
|
132 | 4 | $builder->addTarget('stdClass'); |
|
133 | 4 | $builder->setName($name); |
|
134 | 4 | continue; |
|
135 | 393 | } elseif (is_string($arg) && substr($arg, 0, 9) == 'overload:') { |
|
136 | 11 | $name = array_shift($args); |
|
137 | 11 | $name = str_replace('overload:', '', $name); |
|
138 | 11 | $builder->setInstanceMock(true); |
|
139 | 11 | $builder->addTarget('stdClass'); |
|
140 | 11 | $builder->setName($name); |
|
141 | 11 | continue; |
|
142 | 382 | } elseif (is_string($arg) && substr($arg, strlen($arg)-1, 1) == ']') { |
|
143 | 17 | $parts = explode('[', $arg); |
|
144 | 17 | if (!class_exists($parts[0], true) && !interface_exists($parts[0], true)) { |
|
145 | 1 | throw new \Mockery\Exception('Can only create a partial mock from' |
|
146 | 1 | . ' an existing class or interface'); |
|
147 | } |
||
148 | 16 | $class = $parts[0]; |
|
149 | 16 | $parts[1] = str_replace(' ', '', $parts[1]); |
|
150 | 16 | $partialMethods = explode(',', strtolower(rtrim($parts[1], ']'))); |
|
151 | 16 | $builder->addTarget($class); |
|
152 | 16 | $builder->setWhiteListedMethods($partialMethods); |
|
153 | 16 | array_shift($args); |
|
154 | 16 | continue; |
|
155 | 371 | } elseif (is_string($arg) && (class_exists($arg, true) || interface_exists($arg, true) || trait_exists($arg, true))) { |
|
156 | 340 | $class = array_shift($args); |
|
157 | 340 | $builder->addTarget($class); |
|
158 | 340 | continue; |
|
159 | 55 | } elseif (is_string($arg) && !\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() && (!class_exists($arg, true) && !interface_exists($arg, true))) { |
|
160 | 1 | throw new \Mockery\Exception("Mockery can't find '$arg' so can't mock it"); |
|
161 | 54 | } elseif (is_string($arg)) { |
|
162 | 17 | if (!$this->isValidClassName($arg)) { |
|
163 | 1 | throw new \Mockery\Exception('Class name contains invalid characters'); |
|
164 | } |
||
165 | 16 | $class = array_shift($args); |
|
166 | 16 | $builder->addTarget($class); |
|
167 | 16 | continue; |
|
168 | 38 | } elseif (is_object($arg)) { |
|
169 | 14 | $partial = array_shift($args); |
|
170 | 14 | $builder->addTarget($partial); |
|
171 | 14 | continue; |
|
172 | 26 | } elseif (is_array($arg) && !empty($arg) && array_keys($arg) !== range(0, count($arg) - 1)) { |
|
173 | // if associative array |
||
174 | 17 | if (array_key_exists(self::BLOCKS, $arg)) { |
|
175 | 2 | $blocks = $arg[self::BLOCKS]; |
|
176 | 2 | } |
|
177 | 17 | unset($arg[self::BLOCKS]); |
|
178 | 17 | $quickdefs = array_shift($args); |
|
179 | 17 | continue; |
|
180 | 10 | } elseif (is_array($arg)) { |
|
181 | 10 | $constructorArgs = array_shift($args); |
|
182 | 10 | continue; |
|
183 | } |
||
184 | |||
185 | throw new \Mockery\Exception( |
||
186 | 'Unable to parse arguments sent to ' |
||
187 | . get_class($this) . '::mock()' |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | 424 | $builder->addBlackListedMethods($blocks); |
|
192 | |||
193 | 424 | if (defined('HHVM_VERSION') |
|
194 | 424 | && isset($class) |
|
195 | 424 | && ($class === 'Exception' || is_subclass_of($class, 'Exception'))) { |
|
196 | $builder->addBlackListedMethod("setTraceOptions"); |
||
197 | $builder->addBlackListedMethod("getTraceOptions"); |
||
198 | } |
||
199 | |||
200 | 424 | if (!is_null($constructorArgs)) { |
|
201 | 10 | $builder->addBlackListedMethod("__construct"); // we need to pass through |
|
202 | 10 | } else { |
|
203 | 414 | $builder->setMockOriginalDestructor(true); |
|
204 | } |
||
205 | |||
206 | 424 | if (!empty($partialMethods) && $constructorArgs === null) { |
|
207 | 11 | $constructorArgs = array(); |
|
208 | 11 | } |
|
209 | |||
210 | 424 | $config = $builder->getMockConfiguration(); |
|
211 | |||
212 | 424 | $this->checkForNamedMockClashes($config); |
|
213 | |||
214 | 424 | $def = $this->getGenerator()->generate($config); |
|
215 | |||
216 | 423 | if (class_exists($def->getClassName(), $attemptAutoload = false)) { |
|
217 | 35 | $rfc = new \ReflectionClass($def->getClassName()); |
|
218 | 35 | if (!$rfc->implementsInterface("Mockery\MockInterface")) { |
|
219 | 1 | throw new \Mockery\Exception\RuntimeException("Could not load mock {$def->getClassName()}, class already exists"); |
|
220 | } |
||
221 | 34 | } |
|
222 | |||
223 | 422 | $this->getLoader()->load($def); |
|
224 | |||
225 | 422 | $mock = $this->_getInstance($def->getClassName(), $constructorArgs); |
|
226 | 422 | $mock->mockery_init($this, $config->getTargetObject()); |
|
227 | |||
228 | 422 | if (!empty($quickdefs)) { |
|
229 | 17 | $mock->shouldReceive($quickdefs)->byDefault(); |
|
230 | 17 | } |
|
231 | 422 | if (!empty($expectationClosure)) { |
|
232 | 1 | $expectationClosure($mock); |
|
233 | 1 | } |
|
234 | 422 | $this->rememberMock($mock); |
|
235 | 422 | return $mock; |
|
236 | } |
||
237 | |||
238 | public function instanceMock() |
||
241 | |||
242 | 422 | public function getLoader() |
|
246 | |||
247 | 424 | 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 | 414 | public function mockery_teardown() |
|
284 | { |
||
285 | try { |
||
286 | 414 | $this->mockery_verify(); |
|
287 | 414 | } catch (\Exception $e) { |
|
288 | 3 | $this->mockery_close(); |
|
289 | 3 | throw $e; |
|
290 | } |
||
291 | 412 | } |
|
292 | |||
293 | /** |
||
294 | * Verify the container mocks |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | 414 | public function mockery_verify() |
|
304 | |||
305 | /** |
||
306 | * Reset the container to its original state |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | 422 | 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 | 142 | 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 | 411 | public function mockery_getExpectationCount() |
|
409 | |||
410 | /** |
||
411 | * Store a mock and set its container reference |
||
412 | * |
||
413 | * @param \Mockery\Mock |
||
414 | * @return \Mockery\MockInterface |
||
415 | */ |
||
416 | 422 | 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 | 422 | protected function _getInstance($mockName, $constructorArgs = null) |
|
482 | |||
483 | 424 | 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 |