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:
| 1 | <?php  | 
            ||
| 9 | final class ConnectorFactory  | 
            ||
| 10 | { | 
            ||
| 11 | /**  | 
            ||
| 12 | * @param array $config  | 
            ||
| 13 | *  | 
            ||
| 14 | * @return Connector  | 
            ||
| 15 | *  | 
            ||
| 16 | * @throws InvalidArgumentException  | 
            ||
| 17 | */  | 
            ||
| 18 | 2 | public function create(array $config)  | 
            |
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * @param array $config  | 
            ||
| 37 | *  | 
            ||
| 38 | * @return array  | 
            ||
| 39 | *  | 
            ||
| 40 | * @throws InvalidArgumentException  | 
            ||
| 41 | */  | 
            ||
| 42 | 2 | private function validate(array $config)  | 
            |
| 62 | }  | 
            ||
| 63 | 
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: