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 |
||
11 | class Container implements \ArrayAccess |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $bindings = []; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $resolved = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $aliases = []; |
||
27 | |||
28 | /** |
||
29 | * Resolving all dependencies in the supplied class or object instance constructor. |
||
30 | * |
||
31 | * @param string $instance The class name. |
||
32 | * @param array $parameters List of needed class dependency. |
||
33 | * @return object |
||
34 | */ |
||
35 | public function make($instance, $parameters = []) |
||
44 | |||
45 | /** |
||
46 | * Register a service alias. |
||
47 | * |
||
48 | * @param string $alias The alias name. |
||
49 | * @param string $abstract The class name. |
||
50 | */ |
||
51 | public function register($alias, $abstract) |
||
65 | |||
66 | /** |
||
67 | * Determine if registered alias were exists. |
||
68 | * |
||
69 | * @param string $alias The alias name. |
||
70 | */ |
||
71 | public function isAliasExists($alias) |
||
75 | |||
76 | /** |
||
77 | * Get concrete implementation from supplied alias name. |
||
78 | * |
||
79 | * @param string $alias The alias name. |
||
80 | */ |
||
81 | public function get($alias) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function offsetExists($offset) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function offsetGet($offset) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function offsetSet($offset, $value) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function offsetUnset($offset) |
||
123 | |||
124 | /** |
||
125 | * Get list of unresolved class name from class binding stack. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function getAbstracts() |
||
133 | |||
134 | /** |
||
135 | * Determine if unresolved class name is exists. |
||
136 | * |
||
137 | * @param string $abstract The unresolved class name. |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function isAbstractExists($abstract) |
||
144 | |||
145 | /** |
||
146 | * Determine if concrete dependency is exists. |
||
147 | * |
||
148 | * @param mixed $concrete The concrete dependency. |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function isConcreteExists($concrete) |
||
163 | |||
164 | public function isInterface($abstract) |
||
170 | |||
171 | /** |
||
172 | * Get concrete list of dependencies based on supplied class name. |
||
173 | * |
||
174 | * @param string $abstract The unresolved class name. |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getConcrete($abstract) |
||
181 | |||
182 | /** |
||
183 | * Resolve class dependencies in the supplied class name. |
||
184 | * |
||
185 | * @param string $instance The class name. |
||
186 | * @param array $parameters The needed class dependency. |
||
187 | * @return object |
||
188 | */ |
||
189 | protected function resolve($instance, $parameters = []) |
||
213 | |||
214 | /** |
||
215 | * Resolve method parameters. |
||
216 | * |
||
217 | * @param array $params The unresolvable method. |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function resolveMethodParameters($params = []) |
||
257 | |||
258 | /** |
||
259 | * Recursively resolving class dependency. |
||
260 | * |
||
261 | * @param string $class The valid class name. |
||
262 | * @return object |
||
263 | */ |
||
264 | protected function circularDependencyResolver($class) |
||
300 | |||
301 | /** |
||
302 | * Get concrete implementation from abstract. |
||
303 | * |
||
304 | * @param string $interface The interface name. |
||
305 | * @return object |
||
306 | */ |
||
307 | protected function getConcreteFromInterface($interface) |
||
327 | |||
328 | /** |
||
329 | * Determine if current reflection object has constructor. |
||
330 | * |
||
331 | * @param \ReflectionClass $refl The current reflection class object. |
||
332 | * @return boolean |
||
333 | */ |
||
334 | protected function hasConstructor(Internal\ReflectionClassFactory $refl) |
||
338 | |||
339 | /** |
||
340 | * Determine if unresolvable class name has cloneable. |
||
341 | * |
||
342 | * @param \ReflectionClass $refl The current reflection class object. |
||
343 | * @return boolean |
||
344 | */ |
||
345 | protected function isCloneable(Internal\ReflectionClassFactory $refl) |
||
349 | |||
350 | /** |
||
351 | * Determine if unresolvable class name has serializable. |
||
352 | * |
||
353 | * @param \ReflectionClass $refl The current reflection class object. |
||
354 | * @return boolean |
||
355 | */ |
||
356 | protected function isSerializable(Internal\ReflectionClassFactory $refl) |
||
360 | |||
361 | /** |
||
362 | * Resolving class name without constructor. |
||
363 | * |
||
364 | * @param \ReflectionClass $refl An instance of \ReflectionClass |
||
365 | */ |
||
366 | protected function resolveInstanceWithoutConstructor(Internal\ReflectionClassFactory $refl) |
||
370 | |||
371 | /** |
||
372 | * Get method parameters. |
||
373 | * |
||
374 | * @param \ReflectionClass $refl An reflection class instance. |
||
375 | * @param string $method The method name. |
||
376 | * @return array |
||
377 | */ |
||
378 | protected function getMethodParameters(Internal\ReflectionClassFactory $refl, $method) |
||
382 | |||
383 | /** |
||
384 | * Mark resolved class name to true. |
||
385 | * |
||
386 | * @param string $abstract The resolved class name. |
||
387 | * @return void |
||
388 | */ |
||
389 | protected function markAsResolved($abstract) |
||
397 | |||
398 | /** |
||
399 | * Bind service into binding container stack. |
||
400 | * |
||
401 | * @param string $abstract The unresolvable class name. |
||
402 | * @param \Closure|string $concrete Closure or class name being bound to the class name. |
||
403 | */ |
||
404 | public function bind($abstract, $concrete = null) |
||
420 | |||
421 | /** |
||
422 | * Bind service into binding container stack if supplied class name |
||
423 | * not being bound. |
||
424 | * |
||
425 | * @param string $abstract The unresolvable class name. |
||
426 | * @param \Closure|string $concrete Closure or class name begin bound to the class name. |
||
427 | */ |
||
428 | public function bindIf($abstract, $concrete) |
||
434 | |||
435 | /** |
||
436 | * Call defined instance. |
||
437 | * |
||
438 | * @param string $instance The class name to invoke/call. |
||
439 | * @param array $args The class name __invoke method argument. |
||
440 | * @return mixed|void |
||
441 | */ |
||
442 | public function callInstance($instance, $args = []) |
||
452 | |||
453 | /** |
||
454 | * Determine if class name has been bound or not. |
||
455 | * |
||
456 | * @param string $abstract The unresolvable class name. |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function isBound($abstract) |
||
463 | |||
464 | /** |
||
465 | * Turn class name into resolvable closure. |
||
466 | * |
||
467 | * @param string $abstract The class name |
||
468 | * @param \Closure|string $concrete Can be instance of \Closure or class name. |
||
469 | * @return \Closure |
||
470 | */ |
||
471 | protected function turnIntoResolvableClosure($abstract, $concrete) |
||
478 | } |
||
479 |
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: