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 |
||
15 | class Container implements ContainerInterface { |
||
16 | private $currentRule; |
||
17 | private $currentRuleName; |
||
18 | private $instances; |
||
19 | private $rules; |
||
20 | private $factories; |
||
21 | |||
22 | /** |
||
23 | * Construct a new instance of the {@link Container} class. |
||
24 | */ |
||
25 | 85 | public function __construct() { |
|
32 | |||
33 | /** |
||
34 | * Deep clone rules. |
||
35 | */ |
||
36 | 1 | public function __clone() { |
|
40 | |||
41 | /** |
||
42 | * Deep clone an array. |
||
43 | * |
||
44 | * @param array $array The array to clone. |
||
45 | * @return array Returns the cloned array. |
||
46 | * @see http://stackoverflow.com/a/17729234 |
||
47 | */ |
||
48 | 2 | private function arrayClone(array $array) { |
|
59 | |||
60 | /** |
||
61 | * Normalize a container entry ID. |
||
62 | * |
||
63 | * @param string $id The ID to normalize. |
||
64 | * @return string Returns a normalized ID as a string. |
||
65 | */ |
||
66 | 86 | private function normalizeID($id) { |
|
69 | |||
70 | /** |
||
71 | * Set the current rule to the default rule. |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | 1 | public function defaultRule() { |
|
78 | |||
79 | /** |
||
80 | * Set the current rule. |
||
81 | * |
||
82 | * @param string $id The ID of the rule. |
||
83 | * @return $this |
||
84 | */ |
||
85 | 85 | public function rule($id) { |
|
96 | |||
97 | /** |
||
98 | * Get the class name of the current rule. |
||
99 | * |
||
100 | * @return string Returns a class name. |
||
101 | */ |
||
102 | 2 | public function getClass() { |
|
105 | |||
106 | /** |
||
107 | * Set the name of the class for the current rule. |
||
108 | * |
||
109 | * @param string $className A valid class name. |
||
110 | * @return $this |
||
111 | */ |
||
112 | 6 | public function setClass($className) { |
|
116 | |||
117 | /** |
||
118 | * Get the rule that the current rule references. |
||
119 | * |
||
120 | * @return string Returns a reference name or an empty string if there is no reference. |
||
121 | */ |
||
122 | 3 | public function getAliasOf() { |
|
125 | |||
126 | /** |
||
127 | * Set the rule that the current rule is an alias of. |
||
128 | * |
||
129 | * @param string $alias The name of an entry in the container to point to. |
||
130 | * @return $this |
||
131 | */ |
||
132 | 4 | public function setAliasOf($alias) { |
|
142 | |||
143 | /** |
||
144 | * Add an alias of the current rule. |
||
145 | * |
||
146 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
147 | * with the current rule. If the current rule is shared then the same shared instance will be returned. |
||
148 | * |
||
149 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
150 | * level error is raised and the alias is not added. |
||
151 | * |
||
152 | * @param string $alias The alias to set. |
||
153 | * @return $this |
||
154 | */ |
||
155 | 7 | public function addAlias($alias) { |
|
165 | |||
166 | /** |
||
167 | * Remove an alias of the current rule. |
||
168 | * |
||
169 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
170 | * level error is raised, but the alias is still removed. |
||
171 | * |
||
172 | * @param string $alias The alias to remove. |
||
173 | * @return $this |
||
174 | */ |
||
175 | 2 | public function removeAlias($alias) { |
|
185 | |||
186 | /** |
||
187 | * Get all of the aliases of the current rule. |
||
188 | * |
||
189 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
190 | * |
||
191 | * @return array Returns an array of strings representing aliases. |
||
192 | */ |
||
193 | 6 | public function getAliases() { |
|
204 | |||
205 | /** |
||
206 | * Get the factory callback for the current rule. |
||
207 | * |
||
208 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
209 | */ |
||
210 | 2 | public function getFactory() { |
|
213 | |||
214 | /** |
||
215 | * Set the factory that will be used to create the instance for the current rule. |
||
216 | * |
||
217 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
218 | * @return $this |
||
219 | */ |
||
220 | 10 | public function setFactory(callable $factory) { |
|
224 | |||
225 | /** |
||
226 | * Whether or not the current rule is shared. |
||
227 | * |
||
228 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
229 | */ |
||
230 | 2 | public function isShared() { |
|
233 | |||
234 | /** |
||
235 | * Set whether or not the current rule is shared. |
||
236 | * |
||
237 | * @param bool $shared Whether or not the current rule is shared. |
||
238 | * @return $this |
||
239 | */ |
||
240 | 38 | public function setShared($shared) { |
|
244 | |||
245 | /** |
||
246 | * Whether or not the current rule will inherit to subclasses. |
||
247 | * |
||
248 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
249 | */ |
||
250 | 2 | public function getInherit() { |
|
253 | |||
254 | /** |
||
255 | * Set whether or not the current rule extends to subclasses. |
||
256 | * |
||
257 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
258 | * @return $this |
||
259 | */ |
||
260 | 3 | public function setInherit($inherit) { |
|
264 | |||
265 | /** |
||
266 | * Get the constructor arguments for the current rule. |
||
267 | * |
||
268 | * @return array Returns the constructor arguments for the current rule. |
||
269 | */ |
||
270 | 2 | public function getConstructorArgs() { |
|
273 | |||
274 | /** |
||
275 | * Set the constructor arguments for the current rule. |
||
276 | * |
||
277 | * @param array $args An array of constructor arguments. |
||
278 | * @return $this |
||
279 | */ |
||
280 | 24 | public function setConstructorArgs(array $args) { |
|
284 | |||
285 | /** |
||
286 | * Set a specific shared instance into the container. |
||
287 | * |
||
288 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
289 | * rule is configured that says that instances should not be shared. |
||
290 | * |
||
291 | * @param string $name The name of the container entry. |
||
292 | * @param mixed $instance This instance. |
||
293 | * @return $this |
||
294 | */ |
||
295 | 9 | public function setInstance($name, $instance) { |
|
299 | |||
300 | /** |
||
301 | * Add a method call to a rule. |
||
302 | * |
||
303 | * @param string $method The name of the method to call. |
||
304 | * @param array $args The arguments to pass to the method. |
||
305 | * @return $this |
||
306 | */ |
||
307 | 6 | public function addCall($method, array $args = []) { |
|
312 | |||
313 | /** |
||
314 | * Finds an entry of the container by its identifier and returns it. |
||
315 | * |
||
316 | * @param string $id Identifier of the entry to look for. |
||
317 | * @param array $args Additional arguments to pass to the constructor. |
||
318 | * |
||
319 | * @throws NotFoundException No entry was found for this identifier. |
||
320 | * @throws ContainerException Error while retrieving the entry. |
||
321 | * |
||
322 | * @return mixed Entry. |
||
323 | */ |
||
324 | 66 | public function getArgs($id, array $args = []) { |
|
346 | |||
347 | /** |
||
348 | * Make a rule based on an ID. |
||
349 | * |
||
350 | * @param string $nid A normalized ID. |
||
351 | * @return array Returns an array representing a rule. |
||
352 | */ |
||
353 | 62 | private function makeRule($nid) { |
|
403 | |||
404 | /** |
||
405 | * Make a function that creates objects from a rule. |
||
406 | * |
||
407 | * @param string $nid The normalized ID of the container item. |
||
408 | * @param array $rule The resolved rule for the ID. |
||
409 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
410 | * @throws NotFoundException No entry was found for this identifier. |
||
411 | */ |
||
412 | 40 | private function makeFactory($nid, array $rule) { |
|
413 | 40 | $className = empty($rule['class']) ? $nid : $rule['class']; |
|
414 | |||
415 | 40 | if (!empty($rule['factory'])) { |
|
416 | // The instance is created with a user-supplied factory function. |
||
417 | 6 | $callback = $rule['factory']; |
|
418 | 6 | $function = $this->reflectCallback($callback); |
|
419 | |||
420 | 6 | if ($function->getNumberOfParameters() > 0) { |
|
421 | 3 | $callbackArgs = $this->makeDefaultArgs($function, (array)$rule['constructorArgs'], $rule); |
|
422 | $factory = function ($args) use ($callback, $callbackArgs) { |
||
423 | 3 | return call_user_func_array($callback, $this->resolveArgs($callbackArgs, $args)); |
|
424 | 3 | }; |
|
425 | 3 | } else { |
|
426 | 3 | $factory = $callback; |
|
427 | } |
||
428 | |||
429 | // If a class is specified then still reflect on it so that calls can be made against it. |
||
430 | 6 | if (class_exists($className)) { |
|
431 | 1 | $class = new \ReflectionClass($className); |
|
432 | 1 | } |
|
433 | 6 | } else { |
|
434 | // The instance is created by newing up a class. |
||
435 | 34 | if (!class_exists($className)) { |
|
436 | 1 | throw new NotFoundException("Class $className does not exist.", 404); |
|
437 | } |
||
438 | 33 | $class = new \ReflectionClass($className); |
|
439 | 33 | $constructor = $class->getConstructor(); |
|
440 | |||
441 | 33 | if ($constructor && $constructor->getNumberOfParameters() > 0) { |
|
442 | 30 | $constructorArgs = $this->makeDefaultArgs($constructor, (array)$rule['constructorArgs'], $rule); |
|
443 | |||
444 | $factory = function ($args) use ($className, $constructorArgs) { |
||
445 | 30 | return new $className(...array_values($this->resolveArgs($constructorArgs, $args))); |
|
446 | 30 | }; |
|
447 | 30 | } else { |
|
448 | $factory = function () use ($className) { |
||
449 | 5 | return new $className; |
|
450 | 5 | }; |
|
451 | } |
||
452 | } |
||
453 | |||
454 | // Add calls to the factory. |
||
455 | 39 | if (isset($class) && !empty($rule['calls'])) { |
|
456 | 4 | $calls = []; |
|
457 | |||
458 | // Generate the calls array. |
||
459 | 4 | foreach ($rule['calls'] as $call) { |
|
460 | 4 | list($methodName, $args) = $call; |
|
461 | 4 | $method = $class->getMethod($methodName); |
|
462 | 4 | $calls[] = [$methodName, $this->makeDefaultArgs($method, $args, $rule)]; |
|
463 | 4 | } |
|
464 | |||
465 | // Wrap the factory in one that makes the calls. |
||
466 | $factory = function ($args) use ($factory, $calls) { |
||
467 | 4 | $instance = $factory($args); |
|
468 | |||
469 | 4 | foreach ($calls as $call) { |
|
470 | 4 | call_user_func_array( |
|
471 | 4 | [$instance, $call[0]], |
|
472 | 4 | $this->resolveArgs($call[1], [], $instance) |
|
473 | 4 | ); |
|
474 | 4 | } |
|
475 | |||
476 | 4 | return $instance; |
|
477 | 4 | }; |
|
478 | 4 | } |
|
479 | |||
480 | 39 | return $factory; |
|
481 | } |
||
482 | |||
483 | /** |
||
484 | * Create a shared instance of a class from a rule. |
||
485 | * |
||
486 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
487 | * |
||
488 | * @param string $nid The normalized ID of the container item. |
||
489 | * @param array $rule The resolved rule for the ID. |
||
490 | * @param array $args Additional arguments passed during creation. |
||
491 | * @return object Returns the the new instance. |
||
|
|||
492 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
493 | */ |
||
494 | 23 | private function createSharedInstance($nid, array $rule, array $args) { |
|
556 | |||
557 | /** |
||
558 | * Make an array of default arguments for a given function. |
||
559 | * |
||
560 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
561 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
562 | * @param array $rule The entire rule. |
||
563 | * @return array Returns an array in the form `name => defaultValue`. |
||
564 | */ |
||
565 | 55 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
594 | |||
595 | /** |
||
596 | * Replace an array of default args with called args. |
||
597 | * |
||
598 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
599 | * @param array $args The arguments passed into a creation. |
||
600 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
601 | * @return array Returns an array suitable to be applied to a function call. |
||
602 | * @throws MissingArgumentException Throws an exception when a required parameter is missing. |
||
603 | */ |
||
604 | 55 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
636 | |||
637 | /** |
||
638 | * Create an instance of a container item. |
||
639 | * |
||
640 | * This method either creates a new instance or returns an already created shared instance. |
||
641 | * |
||
642 | * @param string $nid The normalized ID of the container item. |
||
643 | * @param array $args Additional arguments to pass to the constructor. |
||
644 | * @return object Returns an object instance. |
||
645 | */ |
||
646 | 62 | private function createInstance($nid, array $args) { |
|
659 | |||
660 | /** |
||
661 | * Call a callback with argument injection. |
||
662 | * |
||
663 | * @param callable $callback The callback to call. |
||
664 | * @param array $args Additional arguments to pass to the callback. |
||
665 | * @return mixed Returns the result of the callback. |
||
666 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
667 | */ |
||
668 | 3 | public function call(callable $callback, array $args = []) { |
|
685 | |||
686 | /** |
||
687 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
688 | * |
||
689 | * @param string $id Identifier of the entry to look for. |
||
690 | * |
||
691 | * @return boolean |
||
692 | */ |
||
693 | 5 | public function has($id) { |
|
698 | |||
699 | /** |
||
700 | * Determines whether a rule has been defined at a given ID. |
||
701 | * |
||
702 | * @param string $id Identifier of the entry to look for. |
||
703 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
704 | */ |
||
705 | 4 | public function hasRule($id) { |
|
709 | |||
710 | /** |
||
711 | * Finds an entry of the container by its identifier and returns it. |
||
712 | * |
||
713 | * @param string $id Identifier of the entry to look for. |
||
714 | * |
||
715 | * @throws NotFoundException No entry was found for this identifier. |
||
716 | * @throws ContainerException Error while retrieving the entry. |
||
717 | * |
||
718 | * @return mixed Entry. |
||
719 | */ |
||
720 | 57 | public function get($id) { |
|
723 | |||
724 | /** |
||
725 | * Determine the reflection information for a callback. |
||
726 | * |
||
727 | * @param callable $callback The callback to reflect. |
||
728 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
729 | */ |
||
730 | 9 | private function reflectCallback(callable $callback) { |
|
737 | } |
||
738 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.