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 $instances; |
||
18 | private $rules; |
||
19 | private $factories; |
||
20 | |||
21 | /** |
||
22 | * Construct a new instance of the {@link Container} class. |
||
23 | */ |
||
24 | 43 | public function __construct() { |
|
30 | |||
31 | /** |
||
32 | * Normalize a container entry ID. |
||
33 | * |
||
34 | * @param string $id The ID to normalize. |
||
35 | * @return string Returns a normalized ID as a string. |
||
36 | */ |
||
37 | 41 | private function normalizeID($id) { |
|
40 | |||
41 | /** |
||
42 | * Set the current rule to the default rule. |
||
43 | * |
||
44 | * @return $this |
||
45 | */ |
||
46 | 1 | public function defaultRule() { |
|
50 | |||
51 | /** |
||
52 | * Set the current rule. |
||
53 | * |
||
54 | * @param string $id The ID of the rule. |
||
55 | * @return $this |
||
56 | */ |
||
57 | 14 | public function rule($id) { |
|
66 | |||
67 | /** |
||
68 | * Get the class name of the current rule. |
||
69 | * |
||
70 | * @return string Returns a class name. |
||
71 | */ |
||
72 | 2 | public function getClass() { |
|
75 | |||
76 | /** |
||
77 | * Set the name of the class for the current rule. |
||
78 | * |
||
79 | * @param string $value A valid class name. |
||
80 | * @return $this |
||
81 | */ |
||
82 | 4 | public function setClass($value) { |
|
86 | |||
87 | /** |
||
88 | * Get the factory callback for the current rule. |
||
89 | * |
||
90 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
91 | */ |
||
92 | 2 | public function getFactory() { |
|
95 | |||
96 | /** |
||
97 | * Set the factory that will be used to create the instance for the current rule. |
||
98 | * |
||
99 | * @param callable $value This callback will be called to create the instance for the rule. |
||
100 | * @return $this |
||
101 | */ |
||
102 | 10 | public function setFactory(callable $value) { |
|
106 | |||
107 | /** |
||
108 | * Whether or not the current rule is shared. |
||
109 | * |
||
110 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
111 | */ |
||
112 | 2 | public function isShared() { |
|
115 | |||
116 | /** |
||
117 | * Set whether or not the current rule is shared. |
||
118 | * |
||
119 | * @param bool $value Whether or not the current rule is shared. |
||
120 | * @return $this |
||
121 | */ |
||
122 | 15 | public function setShared($value) { |
|
126 | |||
127 | /** |
||
128 | * Whether or not the current rule will inherit to subclasses. |
||
129 | * |
||
130 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
131 | */ |
||
132 | 2 | public function getInherit() { |
|
135 | |||
136 | /** |
||
137 | * Set whether or not the current rule extends to subclasses. |
||
138 | * |
||
139 | * @param bool $value Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
140 | * @return $this |
||
141 | */ |
||
142 | 2 | public function setInherit($value) { |
|
146 | |||
147 | /** |
||
148 | * Get the constructor arguments for the current rule. |
||
149 | * |
||
150 | * @return array Returns the constructor arguments for the current rule. |
||
151 | */ |
||
152 | 2 | public function getConstructorArgs() { |
|
155 | |||
156 | /** |
||
157 | * Set the constructor arguments for the current rule. |
||
158 | * |
||
159 | * @param array $args An array of constructor arguments. |
||
160 | * @return $this |
||
161 | */ |
||
162 | 11 | public function setConstructorArgs(array $args) { |
|
166 | |||
167 | /** |
||
168 | * Set a specific shared instance into the container. |
||
169 | * |
||
170 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
171 | * rule is configured that says that instances should not be shared. |
||
172 | * |
||
173 | * @param string $name The name of the container entry. |
||
174 | * @param mixed $instance This instance. |
||
175 | * @return $this |
||
176 | */ |
||
177 | 5 | public function setInstance($name, $instance) { |
|
181 | |||
182 | /** |
||
183 | * Add a method call to a rule. |
||
184 | * |
||
185 | * @param string $method The name of the method to call. |
||
186 | * @param array $args The arguments to pass to the method. |
||
187 | * @return $this |
||
188 | */ |
||
189 | 6 | public function addCall($method, array $args = []) { |
|
194 | |||
195 | /** |
||
196 | * Finds an entry of the container by its identifier and returns it. |
||
197 | * |
||
198 | * @param string $id Identifier of the entry to look for. |
||
199 | * @param array $args Additional arguments to pass to the constructor. |
||
200 | * |
||
201 | * @throws NotFoundException No entry was found for this identifier. |
||
202 | * @throws ContainerException Error while retrieving the entry. |
||
203 | * |
||
204 | * @return mixed Entry. |
||
205 | */ |
||
206 | 37 | public function getArgs($id, array $args = []) { |
|
223 | |||
224 | /** |
||
225 | * Make a rule based on an ID. |
||
226 | * |
||
227 | * @param string $nid A normalized ID. |
||
228 | * @return array Returns an array representing a rule. |
||
229 | */ |
||
230 | 33 | private function makeRule($nid) { |
|
267 | |||
268 | /** |
||
269 | * Make a function that creates objects from a rule. |
||
270 | * |
||
271 | * @param string $nid The normalized ID of the container item. |
||
272 | * @param array $rule The resolved rule for the ID. |
||
273 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
274 | * @throws NotFoundException No entry was found for this identifier. |
||
275 | */ |
||
276 | 25 | private function makeFactory($nid, array $rule) { |
|
346 | |||
347 | /** |
||
348 | * Create a shared instance of a class from a rule. |
||
349 | * |
||
350 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
351 | * |
||
352 | * @param string $nid The normalized ID of the container item. |
||
353 | * @param array $rule The resolved rule for the ID. |
||
354 | * @param array $args Additional arguments passed during creation. |
||
355 | * @return object Returns the the new instance. |
||
356 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
357 | */ |
||
358 | 9 | private function createSharedInstance($nid, array $rule, array $args) { |
|
421 | |||
422 | /** |
||
423 | * Make an array of default arguments for a given function. |
||
424 | * |
||
425 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
426 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
427 | * @param array $rule The entire rule. |
||
428 | * @return array Returns an array in the form `name => defaultValue`. |
||
429 | */ |
||
430 | 26 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
456 | |||
457 | /** |
||
458 | * Replace an array of default args with called args. |
||
459 | * |
||
460 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
461 | * @param array $args The arguments passed into a creation. |
||
462 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
463 | * @return array Returns an array suitable to be applied to a function call. |
||
464 | */ |
||
465 | 26 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
490 | |||
491 | /** |
||
492 | * Create an instance of a container item. |
||
493 | * |
||
494 | * This method either creates a new instance or returns an already created shared instance. |
||
495 | * |
||
496 | * @param string $nid The normalized ID of the container item. |
||
497 | * @param array $args Additional arguments to pass to the constructor. |
||
498 | * @return object Returns an object instance. |
||
499 | */ |
||
500 | 33 | private function createInstance($nid, array $args) { |
|
513 | |||
514 | /** |
||
515 | * Call a callback with argument injection. |
||
516 | * |
||
517 | * @param callable $callback The callback to call. |
||
518 | * @param array $args Additional arguments to pass to the callback. |
||
519 | * @return mixed Returns the result of the callback. |
||
520 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
521 | */ |
||
522 | 1 | public function call(callable $callback, array $args = []) { |
|
540 | |||
541 | /** |
||
542 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
543 | * |
||
544 | * @param string $id Identifier of the entry to look for. |
||
545 | * |
||
546 | * @return boolean |
||
547 | */ |
||
548 | public function has($id) { |
||
553 | |||
554 | /** |
||
555 | * Determines whether a rule has been defined at a given ID. |
||
556 | * |
||
557 | * @param string $id Identifier of the entry to look for. |
||
558 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
559 | */ |
||
560 | 4 | public function hasRule($id) { |
|
564 | |||
565 | /** |
||
566 | * Finds an entry of the container by its identifier and returns it. |
||
567 | * |
||
568 | * @param string $id Identifier of the entry to look for. |
||
569 | * |
||
570 | * @throws NotFoundException No entry was found for this identifier. |
||
571 | * @throws ContainerException Error while retrieving the entry. |
||
572 | * |
||
573 | * @return mixed Entry. |
||
574 | */ |
||
575 | 32 | public function get($id) { |
|
578 | |||
579 | /** |
||
580 | * Determine the reflection information for a callback. |
||
581 | * |
||
582 | * @param callable $callback The callback to reflect. |
||
583 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
584 | */ |
||
585 | 9 | private function reflectCallback(callable $callback) { |
|
592 | } |
||
593 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.