Completed
Push — develop ( d878ad...d28e4b )
by Abdelrahman
05:14
created

AuthenticatesUsers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 104
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B sendLoginResponse() 0 32 4
A sendFailedLoginResponse() 0 6 1
A sendLockoutResponse() 0 10 1
A processLogout() 0 6 1
A username() 0 4 1
1
<?php
2
3
namespace Cortex\Fort\Traits;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Validation\ValidationException;
7
use Illuminate\Foundation\Auth\ThrottlesLogins;
8
9
trait AuthenticatesUsers
10
{
11
    use ThrottlesLogins;
12
13
    /**
14
     * Send the response after the user was authenticated.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     *
18
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
19
     */
20
    protected function sendLoginResponse(Request $request)
21
    {
22
        $user = auth()->guard($this->getGuard())->user();
0 ignored issues
show
Bug introduced by
It seems like getGuard() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

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
24
        $twofactor = $user->getTwoFactor();
25
        $totpStatus = $twofactor['totp']['enabled'] ?? false;
26
        $phoneStatus = $twofactor['phone']['enabled'] ?? false;
27
28
        $request->session()->regenerate();
0 ignored issues
show
Bug introduced by
The method regenerate() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        $this->clearLoginAttempts($request);
30
31
        // Enforce TwoFactor authentication
32
        if ($totpStatus || $phoneStatus) {
33
            $this->processLogout($request);
34
35
            $request->session()->put('rinvex.fort.twofactor', ['user_id' => $user->getKey(), 'remember' => $request->filled('remember'), 'totp' => $totpStatus, 'phone' => $phoneStatus]);
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 186 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
36
37
            $route = $totpStatus
38
                ? route('frontarea.verification.phone.verify')
39
                : route('frontarea.verification.phone.request');
40
41
            return intend([
42
                'url' => $route,
43
                'with' => ['warning' => trans('cortex/fort::messages.verification.twofactor.totp.required')],
44
            ]);
45
        }
46
47
        return intend([
48
            'intended' => route('frontarea.home'),
49
            'with' => ['success' => trans('cortex/fort::messages.auth.login')],
50
        ]);
51
    }
52
53
    /**
54
     * Get the failed login response instance.
55
     *
56
     * @param  \Illuminate\Http\Request  $request
57
     *
58
     * @throws ValidationException
59
     *
60
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
61
     */
62
    protected function sendFailedLoginResponse(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        throw ValidationException::withMessages([
65
            $this->username() => [trans('cortex/fort::messages.auth.failed')],
66
        ]);
67
    }
68
69
    /**
70
     * Redirect the user after determining they are locked out.
71
     *
72
     * @param  \Illuminate\Http\Request  $request
73
     *
74
     * @throws \Illuminate\Validation\ValidationException
75
     *
76
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
77
     */
78
    protected function sendLockoutResponse(Request $request)
79
    {
80
        $seconds = $this->limiter()->availableIn(
81
            $this->throttleKey($request)
82
        );
83
84
        throw ValidationException::withMessages([
85
            $this->username() => [trans('cortex/fort::messages.auth.lockout', ['seconds' => $seconds])],
86
        ])->status(423);
87
    }
88
89
    /**
90
     * Process logout.
91
     *
92
     * @param \Illuminate\Http\Request $request
93
     *
94
     * @return void
95
     */
96
    protected function processLogout(Request $request): void
97
    {
98
        auth()->guard($this->getGuard())->logout();
0 ignored issues
show
Bug introduced by
It seems like getGuard() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

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...
99
100
        $request->session()->invalidate();
101
    }
102
103
    /**
104
     * Get the login username to be used by the controller.
105
     *
106
     * @return string
107
     */
108
    protected function username()
109
    {
110
        return 'loginfield';
111
    }
112
}
113