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 = []) |
||
46 | |||
47 | /** |
||
48 | * Register a service alias. |
||
49 | * |
||
50 | * @param string $alias The alias name. |
||
51 | * @param string $abstract The class name. |
||
52 | */ |
||
53 | public function register($alias, $abstract) |
||
67 | |||
68 | /** |
||
69 | * Determine if registered alias were exists. |
||
70 | * |
||
71 | * @param string $alias The alias name. |
||
72 | */ |
||
73 | public function isAliasExists($alias) |
||
77 | |||
78 | /** |
||
79 | * Finds an entry of the container by its identifier and returns it. |
||
80 | * |
||
81 | * @param string $id Identifier of the entry to look for. |
||
82 | * |
||
83 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
||
84 | * @throws ContainerExceptionInterface Error while retrieving the entry. |
||
85 | * |
||
86 | * @return mixed Entry. |
||
87 | */ |
||
88 | public function get($id) |
||
104 | |||
105 | /** |
||
106 | * Returns true if the container can return an entry for the given identifier. |
||
107 | * Returns false otherwise. |
||
108 | * |
||
109 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
110 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
111 | * |
||
112 | * @param string $id Identifier of the entry to look for. |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function has($id) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function offsetExists($offset) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function offsetGet($offset) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function offsetSet($offset, $value) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function offsetUnset($offset) |
||
152 | |||
153 | /** |
||
154 | * Get list of unresolved class name from class binding stack. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function getAbstracts() |
||
162 | |||
163 | /** |
||
164 | * Determine if unresolved class name is exists. |
||
165 | * |
||
166 | * @param string $abstract The unresolved class name. |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isAbstractExists($abstract) |
||
173 | |||
174 | /** |
||
175 | * Determine if concrete dependency is exists. |
||
176 | * |
||
177 | * @param mixed $concrete The concrete dependency. |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function isConcreteExists($concrete) |
||
192 | |||
193 | /** |
||
194 | * Determine if unresolved abstract is an interface. |
||
195 | * |
||
196 | * @param string $abstract The unresolved abstract name. |
||
197 | */ |
||
198 | public function isInterface($abstract) |
||
204 | |||
205 | /** |
||
206 | * Get concrete list of dependencies based on supplied class name. |
||
207 | * |
||
208 | * @param string $abstract The unresolved class name. |
||
209 | * @return array |
||
210 | */ |
||
211 | public function getConcrete($abstract) |
||
215 | |||
216 | /** |
||
217 | * Resolve class dependencies in the supplied class name. |
||
218 | * |
||
219 | * @param string $instance The class name. |
||
220 | * @param array $parameters The needed class dependency. |
||
221 | * @return object |
||
222 | */ |
||
223 | protected function resolve($instance, $parameters = []) |
||
247 | |||
248 | /** |
||
249 | * Resolve method parameters. |
||
250 | * |
||
251 | * @param array $params The unresolvable method. |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function resolveMethodParameters($params = []) |
||
291 | |||
292 | /** |
||
293 | * Recursively resolving class dependency. |
||
294 | * |
||
295 | * @param string $class The valid class name. |
||
296 | * @return object |
||
297 | */ |
||
298 | protected function circularDependencyResolver($class) |
||
334 | |||
335 | /** |
||
336 | * Get concrete implementation from abstract. |
||
337 | * |
||
338 | * @param string $interface The interface name. |
||
339 | * @return object |
||
340 | */ |
||
341 | protected function getConcreteFromInterface($interface) |
||
361 | |||
362 | /** |
||
363 | * Determine if current reflection object has constructor. |
||
364 | * |
||
365 | * @param \ReflectionClass $refl The current reflection class object. |
||
366 | * @return boolean |
||
367 | */ |
||
368 | protected function hasConstructor(Internal\ReflectionClassFactory $refl) |
||
372 | |||
373 | /** |
||
374 | * Determine if unresolvable class name has cloneable. |
||
375 | * |
||
376 | * @param \ReflectionClass $refl The current reflection class object. |
||
377 | * @return boolean |
||
378 | */ |
||
379 | protected function isCloneable(Internal\ReflectionClassFactory $refl) |
||
383 | |||
384 | /** |
||
385 | * Determine if unresolvable class name has serializable. |
||
386 | * |
||
387 | * @param \ReflectionClass $refl The current reflection class object. |
||
388 | * @return boolean |
||
389 | */ |
||
390 | protected function isSerializable(Internal\ReflectionClassFactory $refl) |
||
394 | |||
395 | /** |
||
396 | * Resolving class name without constructor. |
||
397 | * |
||
398 | * @param \ReflectionClass $refl An instance of \ReflectionClass |
||
399 | */ |
||
400 | protected function resolveInstanceWithoutConstructor(Internal\ReflectionClassFactory $refl) |
||
404 | |||
405 | /** |
||
406 | * Get method parameters. |
||
407 | * |
||
408 | * @param \ReflectionClass $refl An reflection class instance. |
||
409 | * @param string $method The method name. |
||
410 | * @return array |
||
411 | */ |
||
412 | protected function getMethodParameters(Internal\ReflectionClassFactory $refl, $method) |
||
416 | |||
417 | /** |
||
418 | * Mark resolved class name to true. |
||
419 | * |
||
420 | * @param string $abstract The resolved class name. |
||
421 | * @return void |
||
422 | */ |
||
423 | protected function markAsResolved($abstract) |
||
431 | |||
432 | /** |
||
433 | * Bind service into binding container stack. |
||
434 | * |
||
435 | * @param string $abstract The unresolvable class name. |
||
436 | * @param \Closure|string $concrete Closure or class name being bound to the class name. |
||
437 | */ |
||
438 | public function bind($abstract, $concrete = null) |
||
454 | |||
455 | /** |
||
456 | * Bind service into binding container stack if supplied class name |
||
457 | * not being bound. |
||
458 | * |
||
459 | * @param string $abstract The unresolvable class name. |
||
460 | * @param \Closure|string $concrete Closure or class name begin bound to the class name. |
||
461 | */ |
||
462 | public function bindIf($abstract, $concrete) |
||
468 | |||
469 | /** |
||
470 | * Call defined instance. |
||
471 | * |
||
472 | * @param string $instance The class name to invoke/call. |
||
473 | * @param array $args The class name __invoke method argument. |
||
474 | * @return mixed|void |
||
475 | */ |
||
476 | public function callInstance($instance, $args = []) |
||
486 | |||
487 | /** |
||
488 | * Determine if class name has been bound or not. |
||
489 | * |
||
490 | * @param string $abstract The unresolvable class name. |
||
491 | * @return bool |
||
492 | */ |
||
493 | public function isBound($abstract) |
||
497 | |||
498 | /** |
||
499 | * Turn class name into resolvable closure. |
||
500 | * |
||
501 | * @param string $abstract The class name |
||
502 | * @param \Closure|string $concrete Can be instance of \Closure or class name. |
||
503 | * @return \Closure |
||
504 | */ |
||
505 | protected function turnIntoResolvableClosure($abstract, $concrete) |
||
512 | } |
||
513 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: