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 |
||
25 | class Container implements ContainerInterface |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Holds all resolved or resolvable instances into the container. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | |||
34 | protected $collection; |
||
35 | |||
36 | /** |
||
37 | * Class specific defined dependencies. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | |||
42 | protected $dependencies; |
||
43 | |||
44 | /** |
||
45 | * Cache of classes inspector and resolver. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | |||
50 | protected $resolving; |
||
51 | |||
52 | /** |
||
53 | * Cache of classes dependencies in callbacks ready for resolution. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | |||
58 | protected $resolved; |
||
59 | |||
60 | /** |
||
61 | * Call a user function injecting the dependencies. |
||
62 | * |
||
63 | * @param string|Closure $function The function or the user function name. |
||
64 | * @param array $parameters The predefined dependencies. |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | |||
69 | 3 | public function call($function, array $parameters = []) |
|
78 | |||
79 | /** |
||
80 | * Makes an element or class injecting automatically all the dependencies. |
||
81 | * |
||
82 | * @param string $abstract The class name or container element name to make. |
||
83 | * @param array $parameters Specific parameters definition. |
||
84 | * |
||
85 | * @throws ContainerException |
||
86 | * @return object|null |
||
87 | */ |
||
88 | |||
89 | 18 | public function make(string $abstract, array $parameters = []) |
|
102 | |||
103 | /** |
||
104 | * Construct a class and all the dependencies using the reflection library of PHP. |
||
105 | * |
||
106 | * @param string $abstract The class name or container element name to make. |
||
107 | * |
||
108 | * @throws ReflectionException |
||
109 | * @return Closure |
||
110 | */ |
||
111 | |||
112 | 18 | protected function construct(string $abstract) : Closure |
|
133 | |||
134 | /** |
||
135 | * Process all dependencies |
||
136 | * |
||
137 | * @param string $abstract The class name or container element name to make |
||
138 | * @param array $parameters User defined parameters that must be used instead of resolved ones |
||
139 | * @param array $dependencies Array of ReflectionParameter |
||
140 | * |
||
141 | * @throws ContainerException When a dependency cannot be solved. |
||
142 | * @return array |
||
143 | */ |
||
144 | |||
145 | 9 | protected function process(string $abstract, array $parameters, array $dependencies) : array |
|
155 | |||
156 | /** |
||
157 | * Resolve all the given class reflected dependencies. |
||
158 | * |
||
159 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
160 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
161 | * |
||
162 | * @throws ContainerException When a dependency cannot be solved. |
||
163 | * @return Object |
||
164 | */ |
||
165 | |||
166 | 7 | protected function resolve(string $abstract, ReflectionParameter $dependency) |
|
176 | |||
177 | /** |
||
178 | * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
||
179 | * |
||
180 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
181 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
182 | * |
||
183 | * @throws ContainerException When a dependency cannot be solved. |
||
184 | * @return Closure |
||
185 | */ |
||
186 | |||
187 | 7 | protected function generate(string $abstract, ReflectionParameter $dependency) : Closure |
|
212 | |||
213 | /** |
||
214 | * Reset the container, removing all the elements, cache and options. |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | |||
219 | 1 | public function flush() : self |
|
228 | |||
229 | /** |
||
230 | * Finds an entry of the container by its identifier and returns it. |
||
231 | * |
||
232 | * @param string $abstract Identifier of the entry to look for. |
||
233 | * |
||
234 | * @throws NotFoundException No entry was found for this identifier. |
||
235 | * @throws ContainerException Error while retrieving the entry. |
||
236 | * |
||
237 | * @return mixed Entry. |
||
238 | */ |
||
239 | 6 | public function get($abstract) |
|
255 | |||
256 | /** |
||
257 | * Returns true if the container can return an entry for the given identifier. |
||
258 | * Returns false otherwise. |
||
259 | * |
||
260 | * `has($abstract)` returning true does not mean that `get($abstract)` will not throw an exception. |
||
261 | * It does however mean that `get($abstract)` will not throw a `NotFoundException`. |
||
262 | * |
||
263 | * @param string $abstract Identifier of the entry to look for. |
||
264 | * |
||
265 | * @return boolean |
||
266 | */ |
||
267 | |||
268 | 2 | public function has($abstract) |
|
272 | |||
273 | /** |
||
274 | * Verify if an element has a singleton instance. |
||
275 | * |
||
276 | * @param string The class name or container element name to resolve dependencies. |
||
277 | * @return bool |
||
278 | */ |
||
279 | |||
280 | 5 | public function isSingleton(string $abstract) : bool |
|
284 | |||
285 | /** |
||
286 | * Verify if an element is a instance of something. |
||
287 | * |
||
288 | * @param string The class name or container element name to resolve dependencies. |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function isInstance(string $abstract) : bool |
||
295 | |||
296 | /** |
||
297 | * Bind a new element to the container. |
||
298 | * |
||
299 | * @param string $abstract The alias name that will be used to call the element. |
||
300 | * @param string|closure|object $concrete The element class name, or an closure that makes the element, or the object itself. |
||
301 | * @param bool $shared Define if the element will be a singleton instance. |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | |||
306 | 12 | public function set(string $abstract, $concrete, bool $shared = false) : self |
|
324 | |||
325 | /** |
||
326 | * Bind a new element to the container IF the element name not exists in the container. |
||
327 | * |
||
328 | * @param string $abstract The alias name that will be used to call the element. |
||
329 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
330 | * @param bool $shared Define if the element will be a singleton instance. |
||
331 | * |
||
332 | * @return self |
||
333 | */ |
||
334 | |||
335 | 1 | public function setIf(string $abstract, $concrete, bool $shared = false) : self |
|
343 | |||
344 | /** |
||
345 | * Bind an specific instance to a class dependency. |
||
346 | * |
||
347 | * @param string $class The class full name. |
||
348 | * @param string $dependencyName The dependency full name. |
||
349 | * @param string|closure $dependency The specific object class name or a classure that makes the element. |
||
350 | * |
||
351 | * @return self |
||
352 | */ |
||
353 | |||
354 | 3 | public function setTo(string $class, string $dependencyName, $dependency) : self |
|
372 | |||
373 | /** |
||
374 | * Bind an element that will be construct only one time, and every call for the element, |
||
375 | * the same instance will be given. |
||
376 | * |
||
377 | * @param string $abstract The alias name that will be used to call the element. |
||
378 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
379 | * |
||
380 | * @return self |
||
381 | */ |
||
382 | |||
383 | 3 | public function singleton(string $abstract, $concrete) : self |
|
389 | |||
390 | /** |
||
391 | * Bind an object to the container. |
||
392 | * |
||
393 | * @param string $abstract The alias name that will be used to call the object. |
||
394 | * @param object $instance The object that will be inserted. |
||
395 | * |
||
396 | * @throws ContainerException When $instance is not an object. |
||
397 | * @return self |
||
398 | */ |
||
399 | |||
400 | 2 | public function instance(string $abstract, $instance) : self |
|
410 | |||
411 | /** |
||
412 | * Modify an element with a given function that receive the old element as argument. |
||
413 | * |
||
414 | * @param string $abstract The alias name that will be used to call the element. |
||
415 | * @param closure $extension The function that receives the old element and return a new or modified one. |
||
416 | * |
||
417 | * @throws NotFoundException When no element was found with $abstract key. |
||
418 | * @return self |
||
419 | */ |
||
420 | |||
421 | 2 | public function extend(string $abstract, closure $extension) : self |
|
439 | |||
440 | /** |
||
441 | * Makes an resolvable element an singleton. |
||
442 | * |
||
443 | * @param string $abstract The alias name that will be used to call the element. |
||
444 | * |
||
445 | * @throws NotFoundException When no element was found with $abstract key. |
||
446 | * @throws ContainerException When the element on $abstract key is not resolvable. |
||
447 | * |
||
448 | * @return self |
||
449 | */ |
||
450 | |||
451 | 1 | public function share(string $abstract) : self |
|
465 | |||
466 | } |
||
467 |