Completed
Push — master ( ad8d2b...c8618f )
by Manel
55:15 queued 25:14
created

ResetPasswordController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendResetFailedResponse() 0 9 2
A showResetForm() 0 6 1
A reset() 0 21 2
A sendResetResponse() 10 10 2
A __construct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Foundation\Auth\ResetsPasswords;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Password;
9
use Illuminate\Http\JsonResponse;
10
11
class ResetPasswordController extends Controller
12
{
13
    /*
14
    |--------------------------------------------------------------------------
15
    | Password Reset Controller
16
    |--------------------------------------------------------------------------
17
    |
18
    | This controller is responsible for handling password reset requests
19
    | and uses a simple trait to include this behavior. You're free to
20
    | explore this trait and override any methods you wish to tweak.
21
    |
22
    */
23
24
    use ResetsPasswords;
25
26
    /**
27
     * Reset the given user's password.
28
     *
29
     * @param  \Illuminate\Http\Request  $request
30
     * @return \Illuminate\Http\RedirectResponse
31
     */
32
    public function reset(Request $request)
33
    {
34
        $this->validate($request, $this->rules(), $this->validationErrorMessages());
35
36
        // Here we will attempt to reset the user's password. If it is successful we
37
        // will update the password on an actual user model and persist it to the
38
        // database. Otherwise we will parse the error and return the response.
39
        $response = $this->broker()->reset(
40
            $this->credentials($request),
41
            function ($user, $password) {
42
                $this->resetPassword($user, $password);
43
            }
44
        );
45
46
        // If the password was successfully reset, we will redirect the user back to
47
        // the application's home authenticated view. If there is an error we can
48
        // redirect them back to where they came from with their error message.
49
        return $response == Password::PASSWORD_RESET
50
            ? $this->sendResetResponse($request, $response)
51
            : $this->sendResetFailedResponse($request, $response);
52
    }
53
54
    /**
55
     * Get the response for a successful password reset.
56
     *
57
     * @param  \Illuminate\Http\Request
58
     * @param  string  $response
59
     * @return \Illuminate\Http\RedirectResponse
60
     */
61 View Code Duplication
    protected function sendResetResponse(Request $request, $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        if ($request->expectsJson()) {
64
            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...
65
                'status' => trans($response)
66
            ]);
67
        }
68
        return redirect($this->redirectPath())
69
            ->with('status', trans($response));
70
    }
71
72
    /**
73
     * Get the response for a failed password reset.
74
     *
75
     * @param  \Illuminate\Http\Request
76
     * @param  string  $response
77
     * @return mixed
78
     */
79
    protected function sendResetFailedResponse(Request $request, $response)
80
    {
81
        if ($request->expectsJson()) {
82
            return new JsonResponse(['email' => trans($response) ], 422);
83
        }
84
        return redirect()->back()
85
            ->withInput($request->only('email'))
86
            ->withErrors(['email' => trans($response)]);
87
    }
88
89
    /**
90
     * Display the password reset view for the given token.
91
     *
92
     * If no token is present, display the link request form.
93
     *
94
     * @param  \Illuminate\Http\Request  $request
95
     * @param  string|null  $token
96
     * @return \Illuminate\Http\Response
97
     */
98
    public function showResetForm(Request $request, $token = null)
99
    {
100
        return view('adminlte::auth.passwords.reset')->with(
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
101
            ['token' => $token, 'email' => $request->email]
102
        );
103
    }
104
105
    /**
106
     * Create a new controller instance.
107
     *
108
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
109
     */
110
    public function __construct()
111
    {
112
        $this->middleware('guest');
113
    }
114
}
115