PermissionMiddleware::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
nop 3
crap 20
1
<?php
2
3
namespace Yajra\Acl\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class PermissionMiddleware
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @param  string  $permission
16
     * @return mixed
17
     */
18
    public function handle(Request $request, Closure $next, string $permission)
19
    {
20
        if (!auth()->user() || !auth()->user()->can($permission)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
21
            if ($request->ajax()) {
22
                return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
23
                    'error' => [
24
                        'status_code' => 401,
25
                        'code' => 'INSUFFICIENT_PERMISSIONS',
26
                        'description' => 'You are not authorized to access this resource.',
27
                    ],
28
                ], 401);
29
            }
30
31
            abort(401, 'You are not authorized to access this resource.');
32
        }
33
34
        return $next($request);
35
    }
36
}
37