Conditions | 8 |
Paths | 15 |
Total Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
11 | public function handle($request, Closure $next, $roleOrPermission) |
||
12 | { |
||
13 | if (is_string($roleOrPermission)) { // sample : 'create-admin|super-admin@admin' , here admin is guard name. create-admin is permission. super-admin is role. | Notice: guard is optional |
||
14 | $parsed = explode('@', $roleOrPermission); |
||
15 | $guard = isset($parsed[1]) |
||
16 | ? $parsed[1] |
||
17 | : null; |
||
18 | $rolesOrPermissions = explode('|', $parsed[0]); |
||
19 | } elseif (is_array($roleOrPermission)) { |
||
20 | $guard = isset($roleOrPermission['guard']) ? $roleOrPermission['guard'] : null; |
||
21 | $rolesOrPermissions = $roleOrPermission['roleOrPermission']; |
||
22 | } |
||
23 | if (auth($guard)->guest()) { |
||
24 | throw UnauthorizedException::notLoggedIn(); |
||
25 | } |
||
26 | |||
27 | if (!auth($guard)->user()->hasAnyRole($rolesOrPermissions) && !auth($guard)->user()->hasAnyPermission($rolesOrPermissions)) { |
||
28 | throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions); |
||
29 | } |
||
30 | |||
31 | return $next($request); |
||
32 | } |
||
33 | } |
||
34 |
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: