Conditions | 5 |
Paths | 12 |
Total Lines | 21 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 5 |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
47 | 1 | public function getChain($uri) |
|
48 | { |
||
49 | 1 | $chain = explode('/', $uri); |
|
50 | 1 | foreach($chain as $k => $v) { |
|
51 | 1 | if(!$v) { |
|
52 | 1 | unset($chain[$k]); |
|
53 | 1 | } |
|
54 | 1 | } |
|
55 | |||
56 | 1 | $chain = array_values($chain); |
|
57 | |||
58 | 1 | if(!count($chain)) { |
|
59 | 1 | $chain[] = 'index'; |
|
60 | 1 | } |
|
61 | |||
62 | 1 | if(count($chain) == 1) { |
|
63 | 1 | $chain[] = 'index'; |
|
64 | 1 | } |
|
65 | |||
66 | 1 | return $chain; |
|
67 | } |
||
68 | } |
||
69 |
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: