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 |
||
29 | class Container implements ContainerInterface |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Holds all resolved or resolvable instances into the container. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | |||
38 | protected $collection; |
||
39 | |||
40 | /** |
||
41 | * Class specific defined dependencies. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | |||
46 | protected $dependencies; |
||
47 | |||
48 | /** |
||
49 | * Cache of classes inspector and resolver. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | |||
54 | protected $resolving; |
||
55 | |||
56 | /** |
||
57 | * Cache of classes dependencies in callbacks ready for resolution. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | |||
62 | protected $resolved; |
||
63 | |||
64 | /** |
||
65 | * Call a user function injecting the dependencies. |
||
66 | * |
||
67 | * @param string|Closure $function The function or the user function name. |
||
68 | * @param array $parameters The predefined dependencies. |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | |||
73 | 3 | public function call($function, array $parameters = []) |
|
91 | |||
92 | /** |
||
93 | * Makes an element or class injecting automatically all the dependencies. |
||
94 | * |
||
95 | * @param string $abstract The class name or container element name to make. |
||
96 | * @param array $parameters Specific parameters definition. |
||
97 | * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
||
98 | * |
||
99 | * @throws \Psr\Container\Exception\ContainerException |
||
100 | * @return object|null |
||
101 | */ |
||
102 | |||
103 | 18 | public function make(string $abstract, array $parameters = [], bool $force = false) |
|
115 | |||
116 | /** |
||
117 | * Construct a class and all the dependencies using the reflection library of PHP. |
||
118 | * |
||
119 | * @param string $abstract The class name or container element name to make. |
||
120 | * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
||
121 | * |
||
122 | * @throws ReflectionException |
||
123 | * @return Closure |
||
124 | */ |
||
125 | |||
126 | 18 | protected function construct(string $abstract, bool $force) : Closure |
|
150 | |||
151 | /** |
||
152 | * Resolve all the given class reflected dependencies. |
||
153 | * |
||
154 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
155 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
156 | * @param bool $force Specify if the dependencies must be recalculated. |
||
157 | * |
||
158 | * @return Object |
||
159 | */ |
||
160 | |||
161 | 7 | protected function resolve(string $abstract, ReflectionParameter $dependency, bool $force) |
|
171 | |||
172 | /** |
||
173 | * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
||
174 | * |
||
175 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
176 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
177 | * |
||
178 | * @return Closure |
||
179 | */ |
||
180 | |||
181 | 7 | protected function generate(string $abstract, ReflectionParameter $dependency) : Closure |
|
198 | |||
199 | /** |
||
200 | * Reset the container, removing all the elements, cache and options. |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | |||
205 | 1 | public function flush() : self |
|
214 | |||
215 | /** |
||
216 | * Finds an entry of the container by its identifier and returns it. |
||
217 | * |
||
218 | * @param string $abstract Identifier of the entry to look for. |
||
219 | * |
||
220 | * @throws \Psr\Container\Exception\NotFoundException No entry was found for this identifier. |
||
221 | * @throws \Psr\Container\Exception\ContainerException Error while retrieving the entry. |
||
222 | * |
||
223 | 6 | * @return mixed Entry. |
|
224 | */ |
||
225 | 6 | public function get($abstract) |
|
241 | |||
242 | /** |
||
243 | * Returns true if the container can return an entry for the given identifier. |
||
244 | * Returns false otherwise. |
||
245 | * |
||
246 | * `has($abstract)` returning true does not mean that `get($abstract)` will not throw an exception. |
||
247 | * It does however mean that `get($abstract)` will not throw a `NotFoundException`. |
||
248 | * |
||
249 | * @param string $abstract Identifier of the entry to look for. |
||
250 | * |
||
251 | * @return boolean |
||
252 | 2 | */ |
|
253 | |||
254 | 2 | public function has($abstract) |
|
258 | |||
259 | /** |
||
260 | * Verify if an element has a singleton instance. |
||
261 | * |
||
262 | * @param string The class name or container element name to resolve dependencies. |
||
263 | * @return bool |
||
264 | 5 | */ |
|
265 | |||
266 | 5 | public function isSingleton(string $abstract) : bool |
|
270 | |||
271 | /** |
||
272 | * Verify if an element is a instance of something. |
||
273 | * |
||
274 | * @param string The class name or container element name to resolve dependencies. |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function isInstance(string $abstract) : bool |
||
281 | 12 | ||
282 | /** |
||
283 | 8 | * Bind a new element to the container. |
|
284 | 11 | * |
|
285 | * @param string $abstract The alias name that will be used to call the element. |
||
286 | * @param string|closure|object $concrete The element class name, or an closure that makes the element, or the object itself. |
||
287 | 12 | * @param bool $shared Define if the element will be a singleton instance. |
|
288 | 6 | * |
|
289 | 6 | * @return \Codeburner\Container\Container |
|
290 | */ |
||
291 | 12 | ||
292 | public function set(string $abstract, $concrete, bool $shared = false) : self |
||
310 | 1 | ||
311 | /** |
||
312 | * Bind a new element to the container IF the element name not exists in the container. |
||
313 | * |
||
314 | * @param string $abstract The alias name that will be used to call the element. |
||
315 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
316 | * @param bool $shared Define if the element will be a singleton instance. |
||
317 | * |
||
318 | * @return \Codeburner\Container\Container |
||
319 | */ |
||
320 | |||
321 | public function setIf(string $abstract, $concrete, bool $shared = false) : self |
||
329 | 2 | ||
330 | /** |
||
331 | * Bind an specific instance to a class dependency. |
||
332 | * |
||
333 | * @param string $class The class full name. |
||
334 | * @param string $dependencyName The dependency full name. |
||
335 | * @param string|closure $dependency The specific object class name or a classure that makes the element. |
||
336 | * |
||
337 | 3 | * @return \Codeburner\Container\Container |
|
338 | */ |
||
339 | 3 | ||
340 | public function setTo(string $class, string $dependencyName, $dependency) : self |
||
358 | |||
359 | /** |
||
360 | * Bind an element that will be construct only one time, and every call for the element, |
||
361 | * the same instance will be given. |
||
362 | * |
||
363 | * @param string $abstract The alias name that will be used to call the element. |
||
364 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
365 | * |
||
366 | * @return \Codeburner\Container\Container |
||
367 | */ |
||
368 | |||
369 | 1 | public function singleton(string $abstract, $concrete) : self |
|
375 | 1 | ||
376 | /** |
||
377 | 1 | * Bind an object to the container. |
|
378 | * |
||
379 | * @param string $abstract The alias name that will be used to call the object. |
||
380 | * @param object $instance The object that will be inserted. |
||
381 | * |
||
382 | * @throws \Psr\Container\Exception\ContainerException When $instance is not an object. |
||
383 | * @return \Codeburner\Container\Container |
||
384 | */ |
||
385 | |||
386 | public function instance(string $abstract, $instance) : self |
||
396 | 2 | ||
397 | /** |
||
398 | 2 | * Modify an element with a given function that receive the old element as argument. |
|
399 | 1 | * |
|
400 | 1 | * @param string $abstract The alias name that will be used to call the element. |
|
401 | 1 | * @param closure $extension The function that receives the old element and return a new or modified one. |
|
402 | * |
||
403 | 1 | * @throws \Psr\Container\Exception\NotFoundException When no element was found with $abstract key. |
|
404 | * @return \Codeburner\Container\Container |
||
405 | */ |
||
406 | 2 | ||
407 | public function extend(string $abstract, closure $extension) : self |
||
425 | |||
426 | 1 | /** |
|
427 | * Makes an resolvable element an singleton. |
||
428 | * |
||
429 | * @param string $abstract The alias name that will be used to call the element. |
||
430 | 1 | * |
|
431 | * @throws \Psr\Container\Exception\NotFoundException When no element was found with $abstract key. |
||
432 | 1 | * @throws \Psr\Container\Exception\ContainerException When the element on $abstract key is not resolvable. |
|
433 | * |
||
434 | * @return \Codeburner\Container\Container |
||
435 | */ |
||
436 | |||
437 | public function share(string $abstract) : self |
||
451 | |||
452 | } |
||
453 |