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 | 56 | public function __construct() { |
|
32 | |||
33 | /** |
||
34 | * Normalize a container entry ID. |
||
35 | * |
||
36 | * @param string $id The ID to normalize. |
||
37 | * @return string Returns a normalized ID as a string. |
||
38 | */ |
||
39 | 57 | private function normalizeID($id) { |
|
42 | |||
43 | /** |
||
44 | * Set the current rule to the default rule. |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | 1 | public function defaultRule() { |
|
51 | |||
52 | /** |
||
53 | * Set the current rule. |
||
54 | * |
||
55 | * @param string $id The ID of the rule. |
||
56 | * @return $this |
||
57 | */ |
||
58 | 56 | public function rule($id) { |
|
69 | |||
70 | /** |
||
71 | * Get the class name of the current rule. |
||
72 | * |
||
73 | * @return string Returns a class name. |
||
74 | */ |
||
75 | 2 | public function getClass() { |
|
78 | |||
79 | /** |
||
80 | * Set the name of the class for the current rule. |
||
81 | * |
||
82 | * @param string $className A valid class name. |
||
83 | * @return $this |
||
84 | */ |
||
85 | 4 | public function setClass($className) { |
|
89 | |||
90 | /** |
||
91 | * Get the rule that the current rule references. |
||
92 | * |
||
93 | * @return string Returns a reference name or an empty string if there is no reference. |
||
94 | */ |
||
95 | 3 | public function getAliasOf() { |
|
98 | |||
99 | /** |
||
100 | * Set the rule that the current rule is an alias of. |
||
101 | * |
||
102 | * @param string $alias The name of an entry in the container to point to. |
||
103 | * @return $this |
||
104 | */ |
||
105 | 4 | public function setAliasOf($alias) { |
|
116 | |||
117 | /** |
||
118 | * Add an alias of the current rule. |
||
119 | * |
||
120 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
121 | * with the current rule. If the current rule is shared then the same shared instance will be returned. |
||
122 | * |
||
123 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
124 | * level error is raised and the alias is not added. |
||
125 | * |
||
126 | * @param string $alias The alias to set. |
||
127 | * @return $this |
||
128 | */ |
||
129 | 6 | public function addAlias($alias) { |
|
140 | |||
141 | /** |
||
142 | * Remove an alias of the current rule. |
||
143 | * |
||
144 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
145 | * level error is raised, but the alias is still removed. |
||
146 | * |
||
147 | * @param string $alias The alias to remove. |
||
148 | * @return $this |
||
149 | */ |
||
150 | 1 | public function removeAlias($alias) { |
|
160 | |||
161 | /** |
||
162 | * Get all of the aliases of the current rule. |
||
163 | * |
||
164 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
165 | * |
||
166 | * @return array Returns an array of strings representing aliases. |
||
167 | */ |
||
168 | 5 | public function getAliases() { |
|
179 | |||
180 | /** |
||
181 | * Get the factory callback for the current rule. |
||
182 | * |
||
183 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
184 | */ |
||
185 | 2 | public function getFactory() { |
|
188 | |||
189 | /** |
||
190 | * Set the factory that will be used to create the instance for the current rule. |
||
191 | * |
||
192 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
193 | * @return $this |
||
194 | */ |
||
195 | 10 | public function setFactory(callable $factory) { |
|
199 | |||
200 | /** |
||
201 | * Whether or not the current rule is shared. |
||
202 | * |
||
203 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
204 | */ |
||
205 | 2 | public function isShared() { |
|
208 | |||
209 | /** |
||
210 | * Set whether or not the current rule is shared. |
||
211 | * |
||
212 | * @param bool $shared Whether or not the current rule is shared. |
||
213 | * @return $this |
||
214 | */ |
||
215 | 18 | public function setShared($shared) { |
|
219 | |||
220 | /** |
||
221 | * Whether or not the current rule will inherit to subclasses. |
||
222 | * |
||
223 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
224 | */ |
||
225 | 2 | public function getInherit() { |
|
228 | |||
229 | /** |
||
230 | * Set whether or not the current rule extends to subclasses. |
||
231 | * |
||
232 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
233 | * @return $this |
||
234 | */ |
||
235 | 3 | public function setInherit($inherit) { |
|
239 | |||
240 | /** |
||
241 | * Get the constructor arguments for the current rule. |
||
242 | * |
||
243 | * @return array Returns the constructor arguments for the current rule. |
||
244 | */ |
||
245 | 2 | public function getConstructorArgs() { |
|
248 | |||
249 | /** |
||
250 | * Set the constructor arguments for the current rule. |
||
251 | * |
||
252 | * @param array $args An array of constructor arguments. |
||
253 | * @return $this |
||
254 | */ |
||
255 | 15 | public function setConstructorArgs(array $args) { |
|
259 | |||
260 | /** |
||
261 | * Set a specific shared instance into the container. |
||
262 | * |
||
263 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
264 | * rule is configured that says that instances should not be shared. |
||
265 | * |
||
266 | * @param string $name The name of the container entry. |
||
267 | * @param mixed $instance This instance. |
||
268 | * @return $this |
||
269 | */ |
||
270 | 5 | public function setInstance($name, $instance) { |
|
274 | |||
275 | /** |
||
276 | * Add a method call to a rule. |
||
277 | * |
||
278 | * @param string $method The name of the method to call. |
||
279 | * @param array $args The arguments to pass to the method. |
||
280 | * @return $this |
||
281 | */ |
||
282 | 6 | public function addCall($method, array $args = []) { |
|
287 | |||
288 | /** |
||
289 | * Finds an entry of the container by its identifier and returns it. |
||
290 | * |
||
291 | * @param string $id Identifier of the entry to look for. |
||
292 | * @param array $args Additional arguments to pass to the constructor. |
||
293 | * |
||
294 | * @throws NotFoundException No entry was found for this identifier. |
||
295 | * @throws ContainerException Error while retrieving the entry. |
||
296 | * |
||
297 | * @return mixed Entry. |
||
298 | */ |
||
299 | 43 | public function getArgs($id, array $args = []) { |
|
321 | |||
322 | /** |
||
323 | * Make a rule based on an ID. |
||
324 | * |
||
325 | * @param string $nid A normalized ID. |
||
326 | * @return array Returns an array representing a rule. |
||
327 | */ |
||
328 | 39 | private function makeRule($nid) { |
|
374 | |||
375 | /** |
||
376 | * Make a function that creates objects from a rule. |
||
377 | * |
||
378 | * @param string $nid The normalized ID of the container item. |
||
379 | * @param array $rule The resolved rule for the ID. |
||
380 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
381 | * @throws NotFoundException No entry was found for this identifier. |
||
382 | */ |
||
383 | 30 | private function makeFactory($nid, array $rule) { |
|
453 | |||
454 | /** |
||
455 | * Create a shared instance of a class from a rule. |
||
456 | * |
||
457 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
458 | * |
||
459 | * @param string $nid The normalized ID of the container item. |
||
460 | * @param array $rule The resolved rule for the ID. |
||
461 | * @param array $args Additional arguments passed during creation. |
||
462 | * @return object Returns the the new instance. |
||
463 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
464 | */ |
||
465 | 10 | private function createSharedInstance($nid, array $rule, array $args) { |
|
528 | |||
529 | /** |
||
530 | * Make an array of default arguments for a given function. |
||
531 | * |
||
532 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
533 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
534 | * @param array $rule The entire rule. |
||
535 | * @return array Returns an array in the form `name => defaultValue`. |
||
536 | */ |
||
537 | 32 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
563 | |||
564 | /** |
||
565 | * Replace an array of default args with called args. |
||
566 | * |
||
567 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
568 | * @param array $args The arguments passed into a creation. |
||
569 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
570 | * @return array Returns an array suitable to be applied to a function call. |
||
571 | */ |
||
572 | 32 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
597 | |||
598 | /** |
||
599 | * Create an instance of a container item. |
||
600 | * |
||
601 | * This method either creates a new instance or returns an already created shared instance. |
||
602 | * |
||
603 | * @param string $nid The normalized ID of the container item. |
||
604 | * @param array $args Additional arguments to pass to the constructor. |
||
605 | * @return object Returns an object instance. |
||
606 | */ |
||
607 | 39 | private function createInstance($nid, array $args) { |
|
620 | |||
621 | /** |
||
622 | * Call a callback with argument injection. |
||
623 | * |
||
624 | * @param callable $callback The callback to call. |
||
625 | * @param array $args Additional arguments to pass to the callback. |
||
626 | * @return mixed Returns the result of the callback. |
||
627 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
628 | */ |
||
629 | 1 | public function call(callable $callback, array $args = []) { |
|
647 | |||
648 | /** |
||
649 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
650 | * |
||
651 | * @param string $id Identifier of the entry to look for. |
||
652 | * |
||
653 | * @return boolean |
||
654 | */ |
||
655 | public function has($id) { |
||
660 | |||
661 | /** |
||
662 | * Determines whether a rule has been defined at a given ID. |
||
663 | * |
||
664 | * @param string $id Identifier of the entry to look for. |
||
665 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
666 | */ |
||
667 | 4 | public function hasRule($id) { |
|
671 | |||
672 | /** |
||
673 | * Finds an entry of the container by its identifier and returns it. |
||
674 | * |
||
675 | * @param string $id Identifier of the entry to look for. |
||
676 | * |
||
677 | * @throws NotFoundException No entry was found for this identifier. |
||
678 | * @throws ContainerException Error while retrieving the entry. |
||
679 | * |
||
680 | * @return mixed Entry. |
||
681 | */ |
||
682 | 38 | public function get($id) { |
|
685 | |||
686 | /** |
||
687 | * Determine the reflection information for a callback. |
||
688 | * |
||
689 | * @param callable $callback The callback to reflect. |
||
690 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
691 | */ |
||
692 | 9 | private function reflectCallback(callable $callback) { |
|
699 | } |
||
700 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.