| Conditions | 4 |
| Paths | 4 |
| Total Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 10 |
| CRAP Score | 4 |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | 2 | public function partition(array $collection, callable ...$callbacks): array |
|
| 9 | { |
||
| 10 | 2 | $partitions = array_fill(0, count($callbacks) + 1, []); |
|
| 11 | |||
| 12 | 2 | foreach ($collection as $index => $element) { |
|
| 13 | 2 | foreach ($callbacks as $partition => $callback) { |
|
| 14 | 2 | if ($callback($element, $index, $collection)) { |
|
| 15 | 2 | $partitions[$partition][] = $element; |
|
| 16 | 2 | continue 2; |
|
| 17 | } |
||
| 18 | } |
||
| 19 | 2 | $partition++; |
|
|
|
|||
| 20 | 2 | $partitions[$partition][] = $element; |
|
| 21 | } |
||
| 22 | |||
| 23 | 2 | return $partitions; |
|
| 24 | } |
||
| 25 | } |
||
| 26 |
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: