Conditions | 5 |
Paths | 6 |
Total Lines | 9 |
Code Lines | 6 |
Lines | 0 |
Ratio | 0 % |
Tests | 6 |
CRAP Score | 5 |
Changes | 0 |
1 | <?php |
||
27 | 1 | public function test($data, $origin = null, array $keys = []): bool |
|
28 | { |
||
29 | 1 | if (is_array($data) || $data instanceof \Countable) { |
|
30 | 1 | $length = count($data); |
|
31 | 1 | } elseif (is_string($data)) { |
|
32 | 1 | $length = mb_strlen($data, 'utf-8'); |
|
33 | } |
||
34 | 1 | return $length >= $this->min && $length <= $this->max; |
|
|
|||
35 | } |
||
36 | } |
||
37 |
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: