AuthenticationController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 6
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A login() 0 22 3
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Http\Controllers\Api\v2;
9
10
use Tymon\JWTAuth\JWTAuth;
11
use Illuminate\Http\Request;
12
use App\Http\Controllers\Controller;
13
use Tymon\JWTAuth\Exceptions\JWTException;
14
use Illuminate\Foundation\Testing\HttpException;
15
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
16
17
class AuthenticationController extends Controller
18
{
19
    public function login(Request $request, JWTAuth $JWTAuth)
20
    {
21
        \Debugbar::disable();
22
23
        $credentials['email'] = $request->header('email');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$credentials was never initialized. Although not strictly required by PHP, it is generally a good practice to add $credentials = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
24
        $credentials['password'] = $request->header('password');
25
26
        try {
27
            $token = $JWTAuth->attempt($credentials);
28
            if (! $token) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $token of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
29
                throw new AccessDeniedHttpException();
30
            }
31
        } catch (JWTException $e) {
32
            throw new HttpException(500);
33
        }
34
35
        return response()->json([
36
            'status_code' => '200',
37
            'message'     => 'ok',
38
            'token'       => $token,
39
        ]);
40
    }
41
}
42