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 | 64 | 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 | 65 | 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 | 64 | 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 | 5 | 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) { |
|
115 | |||
116 | /** |
||
117 | * Add an alias of the current rule. |
||
118 | * |
||
119 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
120 | * with the current rule. If the current rule is shared then the same shared instance will be returned. |
||
121 | * |
||
122 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
123 | * level error is raised and the alias is not added. |
||
124 | * |
||
125 | * @param string $alias The alias to set. |
||
126 | * @return $this |
||
127 | */ |
||
128 | 7 | public function addAlias($alias) { |
|
138 | |||
139 | /** |
||
140 | * Remove an alias of the current rule. |
||
141 | * |
||
142 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
143 | * level error is raised, but the alias is still removed. |
||
144 | * |
||
145 | * @param string $alias The alias to remove. |
||
146 | * @return $this |
||
147 | */ |
||
148 | 2 | public function removeAlias($alias) { |
|
158 | |||
159 | /** |
||
160 | * Get all of the aliases of the current rule. |
||
161 | * |
||
162 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
163 | * |
||
164 | * @return array Returns an array of strings representing aliases. |
||
165 | */ |
||
166 | 6 | public function getAliases() { |
|
177 | |||
178 | /** |
||
179 | * Get the factory callback for the current rule. |
||
180 | * |
||
181 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
182 | */ |
||
183 | 2 | public function getFactory() { |
|
186 | |||
187 | /** |
||
188 | * Set the factory that will be used to create the instance for the current rule. |
||
189 | * |
||
190 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
191 | * @return $this |
||
192 | */ |
||
193 | 10 | public function setFactory(callable $factory) { |
|
197 | |||
198 | /** |
||
199 | * Whether or not the current rule is shared. |
||
200 | * |
||
201 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
202 | */ |
||
203 | 2 | public function isShared() { |
|
206 | |||
207 | /** |
||
208 | * Set whether or not the current rule is shared. |
||
209 | * |
||
210 | * @param bool $shared Whether or not the current rule is shared. |
||
211 | * @return $this |
||
212 | */ |
||
213 | 19 | public function setShared($shared) { |
|
217 | |||
218 | /** |
||
219 | * Whether or not the current rule will inherit to subclasses. |
||
220 | * |
||
221 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
222 | */ |
||
223 | 2 | public function getInherit() { |
|
226 | |||
227 | /** |
||
228 | * Set whether or not the current rule extends to subclasses. |
||
229 | * |
||
230 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
231 | * @return $this |
||
232 | */ |
||
233 | 3 | public function setInherit($inherit) { |
|
237 | |||
238 | /** |
||
239 | * Get the constructor arguments for the current rule. |
||
240 | * |
||
241 | * @return array Returns the constructor arguments for the current rule. |
||
242 | */ |
||
243 | 2 | public function getConstructorArgs() { |
|
246 | |||
247 | /** |
||
248 | * Set the constructor arguments for the current rule. |
||
249 | * |
||
250 | * @param array $args An array of constructor arguments. |
||
251 | * @return $this |
||
252 | */ |
||
253 | 15 | public function setConstructorArgs(array $args) { |
|
257 | |||
258 | /** |
||
259 | * Set a specific shared instance into the container. |
||
260 | * |
||
261 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
262 | * rule is configured that says that instances should not be shared. |
||
263 | * |
||
264 | * @param string $name The name of the container entry. |
||
265 | * @param mixed $instance This instance. |
||
266 | * @return $this |
||
267 | */ |
||
268 | 6 | public function setInstance($name, $instance) { |
|
272 | |||
273 | /** |
||
274 | * Add a method call to a rule. |
||
275 | * |
||
276 | * @param string $method The name of the method to call. |
||
277 | * @param array $args The arguments to pass to the method. |
||
278 | * @return $this |
||
279 | */ |
||
280 | 6 | public function addCall($method, array $args = []) { |
|
285 | |||
286 | /** |
||
287 | * Finds an entry of the container by its identifier and returns it. |
||
288 | * |
||
289 | * @param string $id Identifier of the entry to look for. |
||
290 | * @param array $args Additional arguments to pass to the constructor. |
||
291 | * |
||
292 | * @throws NotFoundException No entry was found for this identifier. |
||
293 | * @throws ContainerException Error while retrieving the entry. |
||
294 | * |
||
295 | * @return mixed Entry. |
||
296 | */ |
||
297 | 46 | public function getArgs($id, array $args = []) { |
|
319 | |||
320 | /** |
||
321 | * Make a rule based on an ID. |
||
322 | * |
||
323 | * @param string $nid A normalized ID. |
||
324 | * @return array Returns an array representing a rule. |
||
325 | */ |
||
326 | 42 | private function makeRule($nid) { |
|
372 | |||
373 | /** |
||
374 | * Make a function that creates objects from a rule. |
||
375 | * |
||
376 | * @param string $nid The normalized ID of the container item. |
||
377 | * @param array $rule The resolved rule for the ID. |
||
378 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
379 | * @throws NotFoundException No entry was found for this identifier. |
||
380 | */ |
||
381 | 32 | private function makeFactory($nid, array $rule) { |
|
451 | |||
452 | /** |
||
453 | * Create a shared instance of a class from a rule. |
||
454 | * |
||
455 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
456 | * |
||
457 | * @param string $nid The normalized ID of the container item. |
||
458 | * @param array $rule The resolved rule for the ID. |
||
459 | * @param array $args Additional arguments passed during creation. |
||
460 | * @return object Returns the the new instance. |
||
461 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
462 | */ |
||
463 | 11 | private function createSharedInstance($nid, array $rule, array $args) { |
|
526 | |||
527 | /** |
||
528 | * Make an array of default arguments for a given function. |
||
529 | * |
||
530 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
531 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
532 | * @param array $rule The entire rule. |
||
533 | * @return array Returns an array in the form `name => defaultValue`. |
||
534 | */ |
||
535 | 35 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
561 | |||
562 | /** |
||
563 | * Replace an array of default args with called args. |
||
564 | * |
||
565 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
566 | * @param array $args The arguments passed into a creation. |
||
567 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
568 | * @return array Returns an array suitable to be applied to a function call. |
||
569 | */ |
||
570 | 35 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
595 | |||
596 | /** |
||
597 | * Create an instance of a container item. |
||
598 | * |
||
599 | * This method either creates a new instance or returns an already created shared instance. |
||
600 | * |
||
601 | * @param string $nid The normalized ID of the container item. |
||
602 | * @param array $args Additional arguments to pass to the constructor. |
||
603 | * @return object Returns an object instance. |
||
604 | */ |
||
605 | 42 | private function createInstance($nid, array $args) { |
|
618 | |||
619 | /** |
||
620 | * Call a callback with argument injection. |
||
621 | * |
||
622 | * @param callable $callback The callback to call. |
||
623 | * @param array $args Additional arguments to pass to the callback. |
||
624 | * @return mixed Returns the result of the callback. |
||
625 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
626 | */ |
||
627 | 3 | public function call(callable $callback, array $args = []) { |
|
644 | |||
645 | /** |
||
646 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
647 | * |
||
648 | * @param string $id Identifier of the entry to look for. |
||
649 | * |
||
650 | * @return boolean |
||
651 | */ |
||
652 | 4 | public function has($id) { |
|
657 | |||
658 | /** |
||
659 | * Determines whether a rule has been defined at a given ID. |
||
660 | * |
||
661 | * @param string $id Identifier of the entry to look for. |
||
662 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
663 | */ |
||
664 | 4 | public function hasRule($id) { |
|
668 | |||
669 | /** |
||
670 | * Finds an entry of the container by its identifier and returns it. |
||
671 | * |
||
672 | * @param string $id Identifier of the entry to look for. |
||
673 | * |
||
674 | * @throws NotFoundException No entry was found for this identifier. |
||
675 | * @throws ContainerException Error while retrieving the entry. |
||
676 | * |
||
677 | * @return mixed Entry. |
||
678 | */ |
||
679 | 41 | public function get($id) { |
|
682 | |||
683 | /** |
||
684 | * Determine the reflection information for a callback. |
||
685 | * |
||
686 | * @param callable $callback The callback to reflect. |
||
687 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
688 | */ |
||
689 | 9 | private function reflectCallback(callable $callback) { |
|
696 | } |
||
697 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.