Authenticate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Auth\Factory as Auth;
7
8
class Authenticate
9
{
10
	/**
11
	 * The authentication guard factory instance.
12
	 *
13
	 * @var \Illuminate\Contracts\Auth\Factory
14
	 */
15
	protected $auth;
16
17
	/**
18
	 * Create a new middleware instance.
19
	 *
20
	 * @param  \Illuminate\Contracts\Auth\Factory $auth
21
	 */
22
	public function __construct(Auth $auth)
23
	{
24
		$this->auth = $auth;
25
	}
26
27
	/**
28
	 * Handle an incoming request.
29
	 *
30
	 * @param  \Illuminate\Http\Request $request
31
	 * @param  \Closure $next
32
	 * @param  string|null $guard
33
	 * @return mixed
34
	 */
35
	public function handle($request, Closure $next, $guard = null)
36
	{
37
		if ($this->auth->guard($guard)->guest()) {
38
			return response()->json(['message' => 'authentication-needed'], 401);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
39
		}
40
41
		if ($request->user()->isBanned == true) {
42
			return response('Unauthorized.', 401);
43
		}
44
45
		return $next($request);
46
	}
47
}