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 | 88 | public function __construct() { |
|
32 | |||
33 | /** |
||
34 | * Deep clone rules. |
||
35 | */ |
||
36 | 1 | public function __clone() { |
|
40 | |||
41 | /** |
||
42 | * Clear all instances |
||
43 | * |
||
44 | */ |
||
45 | 1 | public function clearInstances() { |
|
46 | 1 | $this->instances = []; |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Deep clone an array. |
||
51 | * |
||
52 | * @param array $array The array to clone. |
||
53 | * @return array Returns the cloned array. |
||
54 | * @see http://stackoverflow.com/a/17729234 |
||
55 | */ |
||
56 | 1 | private function arrayClone(array $array) { |
|
67 | |||
68 | /** |
||
69 | * Normalize a container entry ID. |
||
70 | * |
||
71 | * @param string $id The ID to normalize. |
||
72 | * @return string Returns a normalized ID as a string. |
||
73 | */ |
||
74 | 89 | private function normalizeID($id) { |
|
77 | |||
78 | /** |
||
79 | * Set the current rule to the default rule. |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | 1 | public function defaultRule() { |
|
86 | |||
87 | /** |
||
88 | * Set the current rule. |
||
89 | * |
||
90 | * @param string $id The ID of the rule. |
||
91 | * @return $this |
||
92 | */ |
||
93 | 88 | public function rule($id) { |
|
104 | |||
105 | /** |
||
106 | * Get the class name of the current rule. |
||
107 | * |
||
108 | * @return string Returns a class name. |
||
109 | */ |
||
110 | 2 | public function getClass() { |
|
113 | |||
114 | /** |
||
115 | * Set the name of the class for the current rule. |
||
116 | * |
||
117 | * @param string $className A valid class name. |
||
118 | * @return $this |
||
119 | */ |
||
120 | 6 | public function setClass($className) { |
|
124 | |||
125 | /** |
||
126 | * Get the rule that the current rule references. |
||
127 | * |
||
128 | * @return string Returns a reference name or an empty string if there is no reference. |
||
129 | */ |
||
130 | 3 | public function getAliasOf() { |
|
133 | |||
134 | /** |
||
135 | * Set the rule that the current rule is an alias of. |
||
136 | * |
||
137 | * @param string $alias The name of an entry in the container to point to. |
||
138 | * @return $this |
||
139 | */ |
||
140 | 4 | public function setAliasOf($alias) { |
|
150 | |||
151 | /** |
||
152 | * Add an alias of the current rule. |
||
153 | * |
||
154 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
155 | * with the current rule. If the current rule is shared then the same shared instance will be returned. You can add |
||
156 | * multiple aliases by passing additional arguments to this method. |
||
157 | * |
||
158 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
159 | * level error is raised and the alias is not added. |
||
160 | * |
||
161 | * @param string ...$alias The alias to set. |
||
162 | * @return $this |
||
163 | * @since 1.4 Added the ability to pass multiple aliases. |
||
164 | */ |
||
|
|||
165 | 8 | public function addAlias(...$alias) { |
|
177 | |||
178 | /** |
||
179 | * Remove an alias of the current rule. |
||
180 | * |
||
181 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
182 | * level error is raised, but the alias is still removed. |
||
183 | * |
||
184 | * @param string $alias The alias to remove. |
||
185 | * @return $this |
||
186 | */ |
||
187 | 2 | public function removeAlias($alias) { |
|
197 | |||
198 | /** |
||
199 | * Get all of the aliases of the current rule. |
||
200 | * |
||
201 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
202 | * |
||
203 | * @return array Returns an array of strings representing aliases. |
||
204 | */ |
||
205 | 6 | public function getAliases() { |
|
216 | |||
217 | /** |
||
218 | * Get the factory callback for the current rule. |
||
219 | * |
||
220 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
221 | */ |
||
222 | 2 | public function getFactory() { |
|
225 | |||
226 | /** |
||
227 | * Set the factory that will be used to create the instance for the current rule. |
||
228 | * |
||
229 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
230 | * @return $this |
||
231 | */ |
||
232 | 10 | public function setFactory(callable $factory) { |
|
236 | |||
237 | /** |
||
238 | * Whether or not the current rule is shared. |
||
239 | * |
||
240 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
241 | */ |
||
242 | 2 | public function isShared() { |
|
245 | |||
246 | /** |
||
247 | * Set whether or not the current rule is shared. |
||
248 | * |
||
249 | * @param bool $shared Whether or not the current rule is shared. |
||
250 | * @return $this |
||
251 | */ |
||
252 | 38 | public function setShared($shared) { |
|
256 | |||
257 | /** |
||
258 | * Whether or not the current rule will inherit to subclasses. |
||
259 | * |
||
260 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
261 | */ |
||
262 | 2 | public function getInherit() { |
|
265 | |||
266 | /** |
||
267 | * Set whether or not the current rule extends to subclasses. |
||
268 | * |
||
269 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
270 | * @return $this |
||
271 | */ |
||
272 | 3 | public function setInherit($inherit) { |
|
276 | |||
277 | /** |
||
278 | * Get the constructor arguments for the current rule. |
||
279 | * |
||
280 | * @return array Returns the constructor arguments for the current rule. |
||
281 | */ |
||
282 | 2 | public function getConstructorArgs() { |
|
285 | |||
286 | /** |
||
287 | * Set the constructor arguments for the current rule. |
||
288 | * |
||
289 | * @param array $args An array of constructor arguments. |
||
290 | * @return $this |
||
291 | */ |
||
292 | 25 | public function setConstructorArgs(array $args) { |
|
296 | |||
297 | /** |
||
298 | * Set a specific shared instance into the container. |
||
299 | * |
||
300 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
301 | * rule is configured that says that instances should not be shared. |
||
302 | * |
||
303 | * @param string $name The name of the container entry. |
||
304 | * @param mixed $instance This instance. |
||
305 | * @return $this |
||
306 | */ |
||
307 | 9 | public function setInstance($name, $instance) { |
|
311 | |||
312 | /** |
||
313 | * Add a method call to a rule. |
||
314 | * |
||
315 | * @param string $method The name of the method to call. |
||
316 | * @param array $args The arguments to pass to the method. |
||
317 | * @return $this |
||
318 | */ |
||
319 | 6 | public function addCall($method, array $args = []) { |
|
324 | |||
325 | /** |
||
326 | * Finds an entry of the container by its identifier and returns it. |
||
327 | * |
||
328 | * @param string $id Identifier of the entry to look for. |
||
329 | * @param array $args Additional arguments to pass to the constructor. |
||
330 | * |
||
331 | * @throws NotFoundException No entry was found for this identifier. |
||
332 | * @throws ContainerException Error while retrieving the entry. |
||
333 | * |
||
334 | * @return mixed Entry. |
||
335 | */ |
||
336 | 69 | public function getArgs($id, array $args = []) { |
|
358 | |||
359 | /** |
||
360 | * Make a rule based on an ID. |
||
361 | * |
||
362 | * @param string $nid A normalized ID. |
||
363 | * @return array Returns an array representing a rule. |
||
364 | */ |
||
365 | 65 | private function makeRule($nid) { |
|
415 | |||
416 | /** |
||
417 | * Make a function that creates objects from a rule. |
||
418 | * |
||
419 | * @param string $nid The normalized ID of the container item. |
||
420 | * @param array $rule The resolved rule for the ID. |
||
421 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
422 | * @throws NotFoundException No entry was found for this identifier. |
||
423 | */ |
||
424 | 43 | private function makeFactory($nid, array $rule) { |
|
494 | |||
495 | /** |
||
496 | * Create a shared instance of a class from a rule. |
||
497 | * |
||
498 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
499 | * |
||
500 | * @param string $nid The normalized ID of the container item. |
||
501 | * @param array $rule The resolved rule for the ID. |
||
502 | * @param array $args Additional arguments passed during creation. |
||
503 | * @return object Returns the the new instance. |
||
504 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
505 | */ |
||
506 | 23 | private function createSharedInstance($nid, array $rule, array $args) { |
|
568 | |||
569 | /** |
||
570 | * Make an array of default arguments for a given function. |
||
571 | * |
||
572 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
573 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
574 | * @param array $rule The entire rule. |
||
575 | * @return array Returns an array in the form `name => defaultValue`. |
||
576 | */ |
||
577 | 58 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
606 | |||
607 | /** |
||
608 | * Replace an array of default args with called args. |
||
609 | * |
||
610 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
611 | * @param array $args The arguments passed into a creation. |
||
612 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
613 | * @return array Returns an array suitable to be applied to a function call. |
||
614 | * @throws MissingArgumentException Throws an exception when a required parameter is missing. |
||
615 | */ |
||
616 | 58 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
648 | |||
649 | /** |
||
650 | * Create an instance of a container item. |
||
651 | * |
||
652 | * This method either creates a new instance or returns an already created shared instance. |
||
653 | * |
||
654 | * @param string $nid The normalized ID of the container item. |
||
655 | * @param array $args Additional arguments to pass to the constructor. |
||
656 | * @return object Returns an object instance. |
||
657 | */ |
||
658 | 65 | private function createInstance($nid, array $args) { |
|
671 | |||
672 | /** |
||
673 | * Call a callback with argument injection. |
||
674 | * |
||
675 | * @param callable $callback The callback to call. |
||
676 | * @param array $args Additional arguments to pass to the callback. |
||
677 | * @return mixed Returns the result of the callback. |
||
678 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
679 | */ |
||
680 | 3 | public function call(callable $callback, array $args = []) { |
|
697 | |||
698 | /** |
||
699 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
700 | * |
||
701 | * @param string $id Identifier of the entry to look for. |
||
702 | * |
||
703 | * @return boolean |
||
704 | */ |
||
705 | 5 | public function has($id) { |
|
710 | |||
711 | /** |
||
712 | * Determines whether a rule has been defined at a given ID. |
||
713 | * |
||
714 | * @param string $id Identifier of the entry to look for. |
||
715 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
716 | */ |
||
717 | 4 | public function hasRule($id) { |
|
721 | |||
722 | /** |
||
723 | * Returns true if the container already has an instance for the given identifier. Returns false otherwise. |
||
724 | * |
||
725 | * @param string $id Identifier of the entry to look for. |
||
726 | * |
||
727 | * @return bool |
||
728 | */ |
||
729 | 1 | public function hasInstance($id) { |
|
734 | |||
735 | /** |
||
736 | * Finds an entry of the container by its identifier and returns it. |
||
737 | * |
||
738 | * @param string $id Identifier of the entry to look for. |
||
739 | * |
||
740 | * @throws NotFoundException No entry was found for this identifier. |
||
741 | * @throws ContainerException Error while retrieving the entry. |
||
742 | * |
||
743 | * @return mixed Entry. |
||
744 | */ |
||
745 | 57 | public function get($id) { |
|
748 | |||
749 | /** |
||
750 | * Determine the reflection information for a callback. |
||
751 | * |
||
752 | * @param callable $callback The callback to reflect. |
||
753 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
754 | */ |
||
755 | 9 | private function reflectCallback(callable $callback) { |
|
762 | } |
||
763 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.