Test Setup Failed
Branch master (4b8153)
by Phan
05:07
created

AuthController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\API\UserLoginRequest;
6
use Exception;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Log\Logger;
9
use Tymon\JWTAuth\JWTAuth;
10
11
class AuthController extends Controller
12
{
13
    private $auth;
14
    private $logger;
15
16
    public function __construct(JWTAuth $auth, Logger $logger)
17
    {
18
        $this->auth = $auth;
19
        $this->logger = $logger;
20
    }
21
22
    /**
23
     * Log a user in.
24
     *
25
     * @return JsonResponse
26
     */
27
    public function login(UserLoginRequest $request)
28
    {
29
        $token = $this->auth->attempt($request->only('email', 'password'));
30
        abort_unless($token, 401, 'Invalid credentials');
0 ignored issues
show
Bug introduced by
It seems like $token defined by $this->auth->attempt($re...y('email', 'password')) on line 29 can also be of type string; however, abort_unless() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
32
        return response()->json(compact('token'));
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...
33
    }
34
35
    /**
36
     * Log the current user out.
37
     *
38
     * @return JsonResponse
39
     */
40
    public function logout()
41
    {
42
        if ($token = $this->auth->getToken()) {
43
            try {
44
                $this->auth->invalidate($token);
45
            } catch (Exception $e) {
46
                $this->logger->error($e);
47
            }
48
        }
49
50
        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...
51
    }
52
}
53