| Conditions | 4 |
| Paths | 4 |
| Total Lines | 16 |
| Code Lines | 10 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 40 | public function run($userId, $visitorId, $token) |
||
| 41 | { |
||
| 42 | if ($userId) { |
||
| 43 | $user = $this->findUserService->byId($userId); |
||
| 44 | } else { |
||
| 45 | if ($token) { |
||
| 46 | $user = Auth::user(); |
||
| 47 | } else { |
||
| 48 | if ($visitorId) { |
||
| 49 | $user = $this->findUserService->byVisitorId($visitorId); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return $user; |
||
|
|
|||
| 55 | } |
||
| 56 | |||
| 58 |
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: