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 |
||
31 | class Container implements ContainerInterface |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Holds all resolved or resolvable instances into the container. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | |||
40 | protected $collection; |
||
41 | |||
42 | /** |
||
43 | * Class specific defined dependencies. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | |||
48 | protected $dependencies; |
||
49 | |||
50 | /** |
||
51 | * Cache of classes inspector and resolver. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | |||
56 | protected $resolving; |
||
57 | |||
58 | /** |
||
59 | * Cache of classes dependencies in callbacks ready for resolution. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | |||
64 | protected $resolved; |
||
65 | |||
66 | /** |
||
67 | * Call a user function injecting the dependencies. |
||
68 | * |
||
69 | * @param string|Closure $function The function or the user function name. |
||
70 | * @param array $parameters The predefined dependencies. |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | |||
75 | 3 | public function call($function, array $parameters = []) |
|
84 | |||
85 | /** |
||
86 | * Makes an element or class injecting automatically all the dependencies. |
||
87 | * |
||
88 | * @param string $abstract The class name or container element name to make. |
||
89 | * @param array $parameters Specific parameters definition. |
||
90 | * |
||
91 | * @throws ContainerException |
||
92 | * @return object|null |
||
93 | */ |
||
94 | |||
95 | 22 | public function make(string $abstract, array $parameters = []) |
|
108 | |||
109 | /** |
||
110 | * Construct a class and all the dependencies using the reflection library of PHP. |
||
111 | * |
||
112 | * @param string $abstract The class name or container element name to make. |
||
113 | * |
||
114 | * @throws ReflectionException |
||
115 | * @return Closure |
||
116 | */ |
||
117 | |||
118 | 22 | protected function construct(string $abstract) : Closure |
|
139 | |||
140 | /** |
||
141 | * Process all dependencies |
||
142 | * |
||
143 | * @param string $abstract The class name or container element name to make |
||
144 | * @param array $parameters User defined parameters that must be used instead of resolved ones |
||
145 | * @param array $dependencies Array of ReflectionParameter |
||
146 | * |
||
147 | * @throws ContainerException When a dependency cannot be solved. |
||
148 | * @return array |
||
149 | */ |
||
150 | |||
151 | 13 | protected function process(string $abstract, array $parameters, array $dependencies) : array |
|
161 | |||
162 | /** |
||
163 | * Resolve all the given class reflected dependencies. |
||
164 | * |
||
165 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
166 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
167 | * |
||
168 | * @throws ContainerException When a dependency cannot be solved. |
||
169 | * @return Object |
||
170 | */ |
||
171 | |||
172 | 11 | protected function resolve(string $abstract, ReflectionParameter $dependency) |
|
182 | |||
183 | /** |
||
184 | * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
||
185 | * |
||
186 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
187 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
188 | * |
||
189 | * @throws ContainerException When a dependency cannot be solved. |
||
190 | * @return Closure |
||
191 | */ |
||
192 | |||
193 | 11 | protected function generate(string $abstract, ReflectionParameter $dependency) : Closure |
|
194 | { |
||
195 | 11 | if ($class = $dependency->getClass()) { |
|
196 | 10 | return $this->build($class->name, "{$abstract}{$class->name}"); |
|
197 | } |
||
198 | |||
199 | try { |
||
200 | 2 | $value = $dependency->getDefaultValue(); |
|
201 | |||
202 | return function () use ($value) { |
||
203 | 1 | return $value; |
|
204 | 1 | }; |
|
205 | 1 | } catch (ReflectionException $e) { |
|
206 | 1 | throw new ContainerException("Cannot resolve '$dependency->name' of '$abstract'", 0, $e); |
|
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Create a build closure for a given class |
||
212 | * |
||
213 | * @param string $classname The class that need to be build |
||
214 | * @param string $entry Cache entry to search |
||
215 | * |
||
216 | * @return Closure |
||
217 | */ |
||
218 | |||
219 | 10 | protected function build(string $classname, string $entry) : Closure |
|
229 | |||
230 | /** |
||
231 | * Reset the container, removing all the elements, cache and options. |
||
232 | * |
||
233 | * @return ContainerInterface |
||
234 | */ |
||
235 | |||
236 | 1 | public function flush() : ContainerInterface |
|
245 | |||
246 | /** |
||
247 | * Finds an entry of the container by its identifier and returns it. |
||
248 | * |
||
249 | * @param string $abstract Identifier of the entry to look for. |
||
250 | * |
||
251 | * @throws NotFoundException No entry was found for this identifier. |
||
252 | * @throws ContainerException Error while retrieving the entry. |
||
253 | * |
||
254 | * @return mixed Entry. |
||
255 | */ |
||
256 | 8 | public function get($abstract) |
|
257 | { |
||
258 | 8 | if (! isset($this->collection[$abstract])) { |
|
259 | 1 | throw new NotFoundException("Element '$abstract' not found"); |
|
260 | } |
||
261 | |||
262 | 7 | if ($this->collection[$abstract] instanceof Closure) { |
|
263 | try { |
||
264 | 4 | return $this->collection[$abstract]($this); |
|
265 | 1 | } catch (Exception $e) { |
|
266 | 1 | throw new ContainerException("An exception was thrown while attempt to make $abstract", 0, $e); |
|
267 | } |
||
268 | } |
||
269 | |||
270 | 3 | return $this->collection[$abstract]; |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Returns true if the container can return an entry for the given identifier. |
||
275 | * Returns false otherwise. |
||
276 | * |
||
277 | * `has($abstract)` returning true does not mean that `get($abstract)` will not throw an exception. |
||
278 | * It does however mean that `get($abstract)` will not throw a `NotFoundException`. |
||
279 | * |
||
280 | * @param string $abstract Identifier of the entry to look for. |
||
281 | * |
||
282 | * @return boolean |
||
283 | */ |
||
284 | |||
285 | 2 | public function has($abstract) |
|
289 | |||
290 | /** |
||
291 | * Verify if an element has a singleton instance. |
||
292 | * |
||
293 | * @param string The class name or container element name to resolve dependencies. |
||
294 | * @return bool |
||
295 | */ |
||
296 | |||
297 | 5 | public function isSingleton(string $abstract) : bool |
|
301 | |||
302 | /** |
||
303 | * Verify if an element is a instance of something. |
||
304 | * |
||
305 | * @param string The class name or container element name to resolve dependencies. |
||
306 | * @return bool |
||
307 | */ |
||
308 | 1 | public function isInstance(string $abstract) : bool |
|
309 | { |
||
310 | 1 | return isset($this->collection[$abstract]) && is_object($this->collection[$abstract]); |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * Bind a new element to the container. |
||
315 | * |
||
316 | * @param string $abstract The alias name that will be used to call the element. |
||
317 | * @param string|closure|object $concrete The element class name, or an closure that makes the element, or the object itself. |
||
318 | * @param bool $shared Define if the element will be a singleton instance. |
||
319 | * |
||
320 | * @return ContainerInterface |
||
321 | */ |
||
322 | |||
323 | 13 | public function set(string $abstract, $concrete, bool $shared = false) : ContainerInterface |
|
341 | |||
342 | /** |
||
343 | * Bind a new element to the container IF the element name not exists in the container. |
||
344 | * |
||
345 | * @param string $abstract The alias name that will be used to call the element. |
||
346 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
347 | * @param bool $shared Define if the element will be a singleton instance. |
||
348 | * |
||
349 | * @return ContainerInterface |
||
350 | */ |
||
351 | |||
352 | 1 | public function setIf(string $abstract, $concrete, bool $shared = false) : ContainerInterface |
|
360 | |||
361 | /** |
||
362 | * Bind an specific instance to a class dependency. |
||
363 | * |
||
364 | * @param string $class The class full name. |
||
365 | * @param string $dependencyName The dependency full name. |
||
366 | * @param string|closure $dependency The specific object class name or a classure that makes the element. |
||
367 | * |
||
368 | * @return ContainerInterface |
||
369 | */ |
||
370 | |||
371 | 3 | public function setTo(string $class, string $dependencyName, $dependency) : ContainerInterface |
|
389 | |||
390 | /** |
||
391 | * Bind an element that will be construct only one time, and every call for the element, |
||
392 | * the same instance will be given. |
||
393 | * |
||
394 | * @param string $abstract The alias name that will be used to call the element. |
||
395 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
396 | * |
||
397 | * @return ContainerInterface |
||
398 | */ |
||
399 | |||
400 | 3 | public function singleton(string $abstract, $concrete) : ContainerInterface |
|
406 | |||
407 | /** |
||
408 | * Bind an object to the container. |
||
409 | * |
||
410 | * @param string $abstract The alias name that will be used to call the object. |
||
411 | * @param object $instance The object that will be inserted. |
||
412 | * |
||
413 | * @throws ContainerException When $instance is not an object. |
||
414 | * @return ContainerInterface |
||
415 | */ |
||
416 | |||
417 | 6 | public function instance(string $abstract, $instance) : ContainerInterface |
|
427 | |||
428 | /** |
||
429 | * Modify an element with a given function that receive the old element as argument. |
||
430 | * |
||
431 | * @param string $abstract The alias name that will be used to call the element. |
||
432 | * @param closure $extension The function that receives the old element and return a new or modified one. |
||
433 | * |
||
434 | * @throws NotFoundException When no element was found with $abstract key. |
||
435 | * @return ContainerInterface |
||
436 | */ |
||
437 | |||
438 | 3 | public function extend(string $abstract, closure $extension) : ContainerInterface |
|
456 | |||
457 | /** |
||
458 | * Makes an resolvable element an singleton. |
||
459 | * |
||
460 | * @param string $abstract The alias name that will be used to call the element. |
||
461 | * |
||
462 | * @throws NotFoundException When no element was found with $abstract key. |
||
463 | * @throws ContainerException When the element on $abstract key is not resolvable. |
||
464 | * |
||
465 | * @return ContainerInterface |
||
466 | */ |
||
467 | |||
468 | 3 | public function share(string $abstract) : ContainerInterface |
|
482 | |||
483 | } |
||
484 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.