Completed
Pull Request — master (#259)
by Cristian
02:16
created

ResetPasswordController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A showResetForm() 0 8 1
A broker() 0 6 1
A guard() 0 4 1
1
<?php
2
3
namespace Backpack\Base\app\Http\Controllers\Auth;
4
5
use Backpack\Base\app\Http\Controllers\Controller;
6
use Illuminate\Foundation\Auth\ResetsPasswords;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Password;
9
10
class ResetPasswordController extends Controller
11
{
12
    protected $data = []; // the information we send to the view
13
14
    /*
15
    |--------------------------------------------------------------------------
16
    | Password Reset Controller
17
    |--------------------------------------------------------------------------
18
    |
19
    | This controller is responsible for handling password reset requests
20
    | and uses a simple trait to include this behavior. You're free to
21
    | explore this trait and override any methods you wish to tweak.
22
    |
23
    */
24
25
    use ResetsPasswords;
26
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @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...
31
     */
32
    public function __construct()
33
    {
34
        $guard = backpack_guard_name();
35
36
        $this->middleware("guest:$guard");
37
38
        if (!backpack_users_have_email()) {
39
            abort(501, trans('backpack::base.no_email_column'));
40
        }
41
42
        // where to redirect after password was reset
43
        $this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'admin').'/dashboard';
0 ignored issues
show
Bug introduced by
The property redirectTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
    }
45
46
    // -------------------------------------------------------
47
    // Laravel overwrites for loading backpack views
48
    // -------------------------------------------------------
49
50
    /**
51
     * Display the password reset view for the given token.
52
     *
53
     * If no token is present, display the link request form.
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     * @param string|null              $token
57
     *
58
     * @return \Illuminate\Http\Response
59
     */
60
    public function showResetForm(Request $request, $token = null)
61
    {
62
        $this->data['title'] = trans('backpack::base.reset_password'); // set the page title
63
64
        return view('backpack::auth.passwords.reset', $this->data)->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...
65
            ['token' => $token, 'email' => $request->email]
66
        );
67
    }
68
69
    /**
70
     * Get the broker to be used during password reset.
71
     *
72
     * @return \Illuminate\Contracts\Auth\PasswordBroker
73
     */
74
    public function broker()
75
    {
76
        $passwords = config('backpack.base.passwords', config('auth.defaults.passwords'));
77
78
        return Password::broker($passwords);
79
    }
80
81
    /**
82
     * Get the guard to be used during password reset.
83
     *
84
     * @return \Illuminate\Contracts\Auth\StatefulGuard
85
     */
86
    protected function guard()
87
    {
88
        return backpack_auth();
89
    }
90
}
91