Conditions | 4 |
Paths | 6 |
Total Lines | 16 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
24 | public function run($userId, $token = null) |
||
25 | { |
||
26 | if ($userId) { |
||
27 | $user = $this->call(FindUserByIdTask::class, [$userId])->withToken(); |
||
28 | } else { |
||
29 | if ($token) { |
||
30 | $user = $this->call(GetAuthenticatedUserTask::class, [])->withToken(); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | if (!$user) { |
||
|
|||
35 | throw new UserNotFoundException(); |
||
36 | } |
||
37 | |||
38 | return $user; |
||
39 | } |
||
40 | |||
42 |
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: