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 = []) |
||
321 | |||
322 | /** |
||
323 | * Resolve method parameters. |
||
324 | * |
||
325 | * @param array $params The unresolvable method. |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function resolveMethodParameters($params = []) |
||
363 | |||
364 | /** |
||
365 | * Recursively resolving class dependency. |
||
366 | * |
||
367 | * @param string $class The valid class name. |
||
368 | * @return object |
||
369 | */ |
||
370 | protected function circularDependencyResolver($class) |
||
404 | |||
405 | /** |
||
406 | * Get concrete implementation from given abstract. |
||
407 | * |
||
408 | * @param string $abstract |
||
409 | * @return \Closure|string|null |
||
410 | */ |
||
411 | public function getConcrete($abstract) |
||
417 | |||
418 | /** |
||
419 | * Get concrete implementation from abstract. |
||
420 | * |
||
421 | * @param string $interface The interface name. |
||
422 | * @return object |
||
423 | */ |
||
424 | protected function getConcreteFromInterface($interface) |
||
449 | |||
450 | /** |
||
451 | * Determine if current reflection object has constructor. |
||
452 | * |
||
453 | * @param \ReflectionClass $refl The current reflection class object. |
||
454 | * @return boolean |
||
455 | */ |
||
456 | protected function hasConstructor(Internal\ReflectionClassFactory $refl) |
||
460 | |||
461 | /** |
||
462 | * Determine if unresolvable class name has cloneable. |
||
463 | * |
||
464 | * @param \ReflectionClass $refl The current reflection class object. |
||
465 | * @return boolean |
||
466 | */ |
||
467 | protected function isCloneable(Internal\ReflectionClassFactory $refl) |
||
471 | |||
472 | /** |
||
473 | * Determine if unresolvable class name has serializable. |
||
474 | * |
||
475 | * @param \ReflectionClass $refl The current reflection class object. |
||
476 | * @return boolean |
||
477 | */ |
||
478 | protected function isSerializable(Internal\ReflectionClassFactory $refl) |
||
482 | |||
483 | /** |
||
484 | * Resolving class name without constructor. |
||
485 | * |
||
486 | * @param \ReflectionClass $refl An instance of \ReflectionClass |
||
487 | */ |
||
488 | protected function resolveInstanceWithoutConstructor(Internal\ReflectionClassFactory $refl) |
||
492 | |||
493 | /** |
||
494 | * Get method parameters. |
||
495 | * |
||
496 | * @param \ReflectionClass $refl An reflection class instance. |
||
497 | * @param string $method The method name. |
||
498 | * @return array |
||
499 | */ |
||
500 | protected function getMethodParameters(Internal\ReflectionClassFactory $refl, $method) |
||
504 | |||
505 | /** |
||
506 | * Mark resolved class name to true. |
||
507 | * |
||
508 | * @param string $abstract The resolved class name. |
||
509 | * @param object $resolvedInstance The object instance of resolved abstract. |
||
510 | * @param mixed $flag The concrete-resolving behavior. |
||
511 | * @return void |
||
512 | */ |
||
513 | protected function markAsResolved($abstract, $resolvedInstance, $flag = []) |
||
527 | |||
528 | /** |
||
529 | * Register binding into container stack. |
||
530 | * |
||
531 | * @param string $abstract The unresolvable class name. |
||
532 | * @param \Closure|string $concrete Closure or class name being bound to the class name. |
||
533 | */ |
||
534 | public function bind($abstract, $concrete = null, $shared = false) |
||
546 | |||
547 | /** |
||
548 | * Register shared binding into container stack. |
||
549 | * |
||
550 | * @param string $abstract The unresolvable abstract |
||
551 | * @param \Closure|string|null The concrete form of supplied abstract. |
||
552 | */ |
||
553 | public function singleton($abstract, $concrete = null) |
||
557 | |||
558 | /** |
||
559 | * Bind service into binding container stack if supplied class name |
||
560 | * not being bound. |
||
561 | * |
||
562 | * @param string $abstract The unresolvable class name. |
||
563 | * @param \Closure|string $concrete Closure or class name begin bound to the class name. |
||
564 | */ |
||
565 | public function bindIf($abstract, $concrete) |
||
571 | |||
572 | /** |
||
573 | * Call defined instance. |
||
574 | * |
||
575 | * @param string $instance The class name to invoke/call. |
||
576 | * @param array $args The class name __invoke method argument. |
||
577 | * @return mixed|void |
||
578 | */ |
||
579 | public function callInstance($instance, $args = []) |
||
589 | |||
590 | /** |
||
591 | * Determine if class name has been bound or not. |
||
592 | * |
||
593 | * @param string $abstract The unresolvable class name. |
||
594 | * @return bool |
||
595 | */ |
||
596 | public function isBound($abstract) |
||
600 | |||
601 | /** |
||
602 | * Determine if a given type is shared. |
||
603 | * |
||
604 | * @param string $abstract |
||
605 | * @return bool |
||
606 | */ |
||
607 | public function isShared($abstract) |
||
617 | |||
618 | /** |
||
619 | * Turn class name into resolvable closure. |
||
620 | * |
||
621 | * @param string $abstract The class name |
||
622 | * @param \Closure|string $concrete Can be instance of \Closure or class name. |
||
623 | * @return \Closure |
||
624 | */ |
||
625 | protected function turnIntoResolvableClosure($abstract, $concrete) |
||
632 | } |
||
633 |