Conditions | 5 |
Paths | 7 |
Total Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
48 | protected function getUnique(array $input): array |
||
49 | { |
||
50 | $result = []; |
||
51 | foreach ($input as $k => $val) { |
||
52 | if (is_object($val)) { |
||
53 | $key = \spl_object_hash($val); |
||
54 | } elseif (is_scalar($val)) { |
||
55 | $key = (string) $val; |
||
56 | } |
||
57 | if (!isset($result[$key])) { |
||
58 | $result[$key] = $val; |
||
|
|||
59 | } |
||
60 | } |
||
61 | return \array_values($result); |
||
62 | } |
||
63 | } |
||
64 |
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: