| Conditions | 5 |
| Paths | 5 |
| Total Lines | 20 |
| Code Lines | 10 |
| Lines | 3 |
| Ratio | 15 % |
| Tests | 6 |
| CRAP Score | 6.6 |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 42 | 2 | private function validate(array $config) |
|
| 43 | { |
||
| 44 | 2 | if (!isset($config["driver"])) { |
|
| 45 | throw new InvalidArgumentException("Undefined driver"); |
||
| 46 | } |
||
| 47 | |||
| 48 | 2 | View Code Duplication | if (!in_array($config["driver"], ["sqlsrv", "mysql", "pgsql", "sqlite"])) { |
| 49 | throw new InvalidArgumentException("Unrecognised driver"); |
||
| 50 | } |
||
| 51 | |||
| 52 | 2 | if (!isset($config["connector"])) { |
|
| 53 | throw new InvalidArgumentException("Undefined connector"); |
||
| 54 | } |
||
| 55 | |||
| 56 | 2 | if (!in_array($config["connector"], ["doorman", "blocking"])) { |
|
| 57 | throw new InvalidArgumentException("Unrecognised connector"); |
||
| 58 | } |
||
| 59 | |||
| 60 | 2 | return $config; |
|
| 61 | } |
||
| 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: