Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
13 | class Container implements \ArrayAccess, ContainerInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $bindings = []; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $resolved = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $aliases = []; |
||
29 | |||
30 | /** |
||
31 | * Resolving all dependencies in the supplied class or object instance constructor. |
||
32 | * |
||
33 | * @param string $instance The class name. |
||
34 | * @param array $parameters List of needed class dependency. |
||
35 | * @return object |
||
36 | */ |
||
37 | public function make($instance, $parameters = []) |
||
42 | |||
43 | /** |
||
44 | * Register a service alias. |
||
45 | * |
||
46 | * @param string $alias The alias name. |
||
47 | * @param string $abstract The class name. |
||
48 | */ |
||
49 | public function register($alias, $abstract) |
||
63 | |||
64 | /** |
||
65 | * Determine if registered alias were exists. |
||
66 | * |
||
67 | * @param string $alias The alias name. |
||
68 | */ |
||
69 | public function isAliasExists($alias) |
||
73 | |||
74 | /** |
||
75 | * Finds an entry of the container by its identifier and returns it. |
||
76 | * |
||
77 | * @param string $id Identifier of the entry to look for. |
||
78 | * |
||
79 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
||
80 | * @throws ContainerExceptionInterface Error while retrieving the entry. |
||
81 | * |
||
82 | * @return mixed Entry. |
||
83 | */ |
||
84 | public function get($id) |
||
94 | |||
95 | /** |
||
96 | * Returns true if the container can return an entry for the given identifier. |
||
97 | * Returns false otherwise. |
||
98 | * |
||
99 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
100 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
101 | * |
||
102 | * @param string $id Identifier of the entry to look for. |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function has($id) |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function offsetExists($offset) |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function offsetGet($offset) |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function offsetSet($offset, $value) |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function offsetUnset($offset) |
||
142 | |||
143 | /** |
||
144 | * Determine if defined abstract class name were in resolved concrete stack and it was a |
||
145 | * singleton. |
||
146 | * |
||
147 | * @param string $abstract The resolved abstract class name. |
||
148 | */ |
||
149 | public function hasResolvedSingleton($abstract) |
||
155 | |||
156 | /** |
||
157 | * Get singleton resolved concrete from defined abstract class name. |
||
158 | * |
||
159 | * @param string $abstract The resolved abstract class name. |
||
160 | */ |
||
161 | public function getResolvedSingleton($abstract) |
||
167 | |||
168 | /** |
||
169 | * Determine if defined abstract class name were in resolved concrete stack. |
||
170 | * |
||
171 | * @param string $abstract The resolved abstract class name. |
||
172 | */ |
||
173 | public function hasResolvedConcrete($abstract) |
||
177 | |||
178 | /** |
||
179 | * Get flag of resolved concrete behavior on abstract class name. |
||
180 | * |
||
181 | * @param string $abstract The resolved abstract class name. |
||
182 | */ |
||
183 | public function getResolvedConcreteFlag($abstract) |
||
196 | |||
197 | /** |
||
198 | * Get resolved concrete from defined abstract class name. |
||
199 | * |
||
200 | * @param string $abstract The resolved abstract class name. |
||
201 | */ |
||
202 | public function getResolvedConcrete($abstract) |
||
208 | |||
209 | /** |
||
210 | * Get list of unresolved class name from class binding stack. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function getAbstracts() |
||
218 | |||
219 | /** |
||
220 | * Determine if unresolved class name is exists. |
||
221 | * |
||
222 | * @param string $abstract The unresolved class name. |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isAbstractExists($abstract) |
||
229 | |||
230 | /** |
||
231 | * Determine if unresolved abstract is an interface. |
||
232 | * |
||
233 | * @param string $abstract The unresolved abstract name. |
||
234 | */ |
||
235 | public function isInterface($abstract) |
||
241 | |||
242 | /** |
||
243 | * Get concrete list of dependencies based on supplied class name. |
||
244 | * |
||
245 | * @param string $abstract The unresolved class name. |
||
246 | * @return array |
||
247 | */ |
||
248 | public function getAbstractDependencies($abstract) |
||
252 | |||
253 | /** |
||
254 | * Resolve class dependencies in the supplied class name. |
||
255 | * |
||
256 | * @param string $instance The class name. |
||
257 | * @param array $parameters The needed class dependency. |
||
258 | * @return object |
||
259 | */ |
||
260 | protected function resolve($instance, $parameters = []) |
||
292 | |||
293 | protected function build($instance, $parameters = []) |
||
319 | |||
320 | /** |
||
321 | * Resolve method parameters. |
||
322 | * |
||
323 | * @param array $params The unresolvable method. |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function resolveMethodParameters($params = []) |
||
361 | |||
362 | /** |
||
363 | * Recursively resolving class dependency. |
||
364 | * |
||
365 | * @param string $class The valid class name. |
||
366 | * @return object |
||
367 | */ |
||
368 | protected function circularDependencyResolver($class) |
||
402 | |||
403 | /** |
||
404 | * Get concrete implementation from given abstract. |
||
405 | * |
||
406 | * @param string $abstract |
||
407 | * @return \Closure|string|null |
||
408 | */ |
||
409 | public function getConcrete($abstract) |
||
415 | |||
416 | /** |
||
417 | * Get concrete implementation from abstract. |
||
418 | * |
||
419 | * @param string $interface The interface name. |
||
420 | * @return object |
||
421 | */ |
||
422 | protected function getConcreteFromInterface($interface) |
||
447 | |||
448 | /** |
||
449 | * Determine if current reflection object has constructor. |
||
450 | * |
||
451 | * @param \ReflectionClass $refl The current reflection class object. |
||
452 | * @return boolean |
||
453 | */ |
||
454 | protected function hasConstructor(Internal\ReflectionClassFactory $refl) |
||
458 | |||
459 | /** |
||
460 | * Determine if unresolvable class name has cloneable. |
||
461 | * |
||
462 | * @param \ReflectionClass $refl The current reflection class object. |
||
463 | * @return boolean |
||
464 | */ |
||
465 | protected function isCloneable(Internal\ReflectionClassFactory $refl) |
||
469 | |||
470 | /** |
||
471 | * Determine if unresolvable class name has serializable. |
||
472 | * |
||
473 | * @param \ReflectionClass $refl The current reflection class object. |
||
474 | * @return boolean |
||
475 | */ |
||
476 | protected function isSerializable(Internal\ReflectionClassFactory $refl) |
||
480 | |||
481 | /** |
||
482 | * Resolving class name without constructor. |
||
483 | * |
||
484 | * @param \ReflectionClass $refl An instance of \ReflectionClass |
||
485 | */ |
||
486 | protected function resolveInstanceWithoutConstructor(Internal\ReflectionClassFactory $refl) |
||
490 | |||
491 | /** |
||
492 | * Get method parameters. |
||
493 | * |
||
494 | * @param \ReflectionClass $refl An reflection class instance. |
||
495 | * @param string $method The method name. |
||
496 | * @return array |
||
497 | */ |
||
498 | protected function getMethodParameters(Internal\ReflectionClassFactory $refl, $method) |
||
502 | |||
503 | /** |
||
504 | * Mark resolved class name to true. |
||
505 | * |
||
506 | * @param string $abstract The resolved class name. |
||
507 | * @param object $resolvedInstance The object instance of resolved abstract. |
||
508 | * @param mixed $flag The concrete-resolving behavior. |
||
509 | * @return void |
||
510 | */ |
||
511 | protected function markAsResolved($abstract, $resolvedInstance, $flag = []) |
||
525 | |||
526 | /** |
||
527 | * Register binding into container stack. |
||
528 | * |
||
529 | * @param string $abstract The unresolvable class name. |
||
530 | * @param \Closure|string $concrete Closure or class name being bound to the class name. |
||
531 | */ |
||
532 | public function bind($abstract, $concrete = null, $shared = false) |
||
544 | |||
545 | /** |
||
546 | * Register shared binding into container stack. |
||
547 | * |
||
548 | * @param string $abstract The unresolvable abstract |
||
549 | * @param \Closure|string|null The concrete form of supplied abstract. |
||
550 | */ |
||
551 | public function singleton($abstract, $concrete = null) |
||
555 | |||
556 | /** |
||
557 | * Bind service into binding container stack if supplied class name |
||
558 | * not being bound. |
||
559 | * |
||
560 | * @param string $abstract The unresolvable class name. |
||
561 | * @param \Closure|string $concrete Closure or class name begin bound to the class name. |
||
562 | */ |
||
563 | public function bindIf($abstract, $concrete) |
||
569 | |||
570 | /** |
||
571 | * Call defined instance. |
||
572 | * |
||
573 | * @param string $instance The class name to invoke/call. |
||
574 | * @param array $args The class name __invoke method argument. |
||
575 | * @return mixed|void |
||
576 | */ |
||
577 | public function callInstance($instance, $args = []) |
||
585 | |||
586 | /** |
||
587 | * Determine if class name has been bound or not. |
||
588 | * |
||
589 | * @param string $abstract The unresolvable class name. |
||
590 | * @return bool |
||
591 | */ |
||
592 | public function isBound($abstract) |
||
596 | |||
597 | /** |
||
598 | * Determine if a given type is shared. |
||
599 | * |
||
600 | * @param string $abstract |
||
601 | * @return bool |
||
602 | */ |
||
603 | public function isShared($abstract) |
||
613 | |||
614 | /** |
||
615 | * Turn class name into resolvable closure. |
||
616 | * |
||
617 | * @param string $abstract The class name |
||
618 | * @param \Closure|string $concrete Can be instance of \Closure or class name. |
||
619 | * @return \Closure |
||
620 | */ |
||
621 | protected function turnIntoResolvableClosure($abstract, $concrete) |
||
628 | } |
||
629 |