| Conditions | 4 |
| Paths | 4 |
| Total Lines | 12 |
| Code Lines | 8 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 59 | public function byEverything($userId, $visitorId, $token) |
||
| 60 | { |
||
| 61 | if($userId){ |
||
| 62 | $user = $this->findUserService->byId($userId); |
||
| 63 | }else if($token){ |
||
| 64 | $user = Auth::user(); |
||
| 65 | }else if($visitorId){ |
||
| 66 | $user = $this->findUserService->byVisitorId($visitorId); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $user; |
||
|
|
|||
| 70 | } |
||
| 71 | |||
| 75 |
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: