Conditions | 3 |
Paths | 4 |
Total Lines | 16 |
Code Lines | 8 |
Lines | 0 |
Ratio | 0 % |
Tests | 10 |
CRAP Score | 3 |
Changes | 0 |
1 | <?php |
||
18 | 2 | public function create(array $config) |
|
19 | { |
||
20 | 2 | $config = $this->validate($config); |
|
21 | |||
22 | 2 | if ($config["connector"] === "doorman") { |
|
23 | 1 | $connector = new DoormanConnector(); |
|
24 | 1 | } |
|
25 | |||
26 | 2 | if ($config["connector"] === "blocking") { |
|
27 | 1 | $connector = new BlockingConnector(); |
|
28 | 1 | } |
|
29 | |||
30 | 2 | $connector->connect($config); |
|
|
|||
31 | |||
32 | 2 | return $connector; |
|
33 | } |
||
34 | |||
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: