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 |
||
| 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 = []) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Recursively resolving class dependency. |
||
| 255 | * |
||
| 256 | * @param string $class The valid class name. |
||
| 257 | * @return object |
||
| 258 | */ |
||
| 259 | protected function circularDependencyResolver($class) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get concrete implementation from abstract. |
||
| 298 | * |
||
| 299 | * @param string $interface The interface name. |
||
| 300 | * @return object |
||
| 301 | */ |
||
| 302 | protected function getConcreteFromInterface($interface) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Determine if current reflection object has constructor. |
||
| 325 | * |
||
| 326 | * @param \ReflectionClass $refl The current reflection class object. |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | protected function hasConstructor(Internal\ReflectionClassFactory $refl) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Determine if unresolvable class name has cloneable. |
||
| 336 | * |
||
| 337 | * @param \ReflectionClass $refl The current reflection class object. |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | protected function isCloneable(Internal\ReflectionClassFactory $refl) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Determine if unresolvable class name has serializable. |
||
| 347 | * |
||
| 348 | * @param \ReflectionClass $refl The current reflection class object. |
||
| 349 | * @return boolean |
||
| 350 | */ |
||
| 351 | protected function isSerializable(Internal\ReflectionClassFactory $refl) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Resolving class name without constructor. |
||
| 358 | * |
||
| 359 | * @param \ReflectionClass $refl An instance of \ReflectionClass |
||
| 360 | */ |
||
| 361 | protected function resolveInstanceWithoutConstructor(Internal\ReflectionClassFactory $refl) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get method parameters. |
||
| 368 | * |
||
| 369 | * @param \ReflectionClass $refl An reflection class instance. |
||
| 370 | * @param string $method The method name. |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | protected function getMethodParameters(Internal\ReflectionClassFactory $refl, $method) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Mark resolved class name to true. |
||
| 380 | * |
||
| 381 | * @param string $abstract The resolved class name. |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | protected function markAsResolved($abstract) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Bind service into binding container stack. |
||
| 395 | * |
||
| 396 | * @param string $abstract The unresolvable class name. |
||
| 397 | * @param \Closure|string $concrete Closure or class name being bound to the class name. |
||
| 398 | */ |
||
| 399 | public function bind($abstract, $concrete = null) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Bind service into binding container stack if supplied class name |
||
| 418 | * not being bound. |
||
| 419 | * |
||
| 420 | * @param string $abstract The unresolvable class name. |
||
| 421 | * @param \Closure|string $concrete Closure or class name begin bound to the class name. |
||
| 422 | */ |
||
| 423 | public function bindIf($abstract, $concrete) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Call defined instance. |
||
| 432 | * |
||
| 433 | * @param string $instance The class name to invoke/call. |
||
| 434 | * @param array $args The class name __invoke method argument. |
||
| 435 | * @return mixed|void |
||
| 436 | */ |
||
| 437 | public function callInstance($instance, $args = []) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Determine if class name has been bound or not. |
||
| 450 | * |
||
| 451 | * @param string $abstract The unresolvable class name. |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | public function isBound($abstract) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Turn class name into resolvable closure. |
||
| 461 | * |
||
| 462 | * @param string $abstract The class name |
||
| 463 | * @param \Closure|string $concrete Can be instance of \Closure or class name. |
||
| 464 | * @return \Closure |
||
| 465 | */ |
||
| 466 | protected function turnIntoResolvableClosure($abstract, $concrete) |
||
| 473 | } |
||
| 474 |
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: