Conditions | 6 |
Paths | 4 |
Total Lines | 24 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
7 | public static function moveElement($array, $toMove, $targetIndex) { |
||
8 | if (count($array) == 1) |
||
9 | return $array; |
||
10 | if (is_int($toMove)) { |
||
11 | $tmp = array_splice($array, $toMove, 1); |
||
12 | array_splice($array, $targetIndex, 0, $tmp); |
||
13 | $output = $array; |
||
14 | } |
||
15 | elseif (is_string($toMove)) { |
||
16 | $indexToMove = array_search($toMove, array_keys($array)); |
||
17 | $itemToMove = $array[$toMove]; |
||
18 | array_splice($array, $indexToMove, 1); |
||
19 | $i = 0; |
||
20 | $output = Array(); |
||
21 | foreach($array as $key => $item) { |
||
22 | if ($i == $targetIndex) { |
||
23 | $output[$toMove] = $itemToMove; |
||
24 | } |
||
25 | $output[$key] = $item; |
||
26 | $i++; |
||
27 | } |
||
28 | } |
||
29 | return $output; |
||
|
|||
30 | } |
||
31 | } |
||
33 |
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: