Conditions | 3 |
Paths | 2 |
Total Lines | 14 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
16 | public function getIterator() |
||
17 | { |
||
18 | $innerCount = $this->innerIterator->count(); |
||
19 | $count = min($this->count, $innerCount); |
||
20 | for ($i = 0; $i < $count; $i++) { |
||
21 | do { |
||
22 | $index = rand(0, $innerCount - 1); |
||
23 | } while (isset($indexes[$index])); |
||
24 | $indexes[$index] = $index; |
||
|
|||
25 | } |
||
26 | sort($indexes); |
||
27 | $indexes = new \ArrayIterator($indexes); |
||
28 | return new IndexIterator($this->innerIterator, $indexes); |
||
29 | } |
||
30 | } |
||
31 |
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: