Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 6a87fb...a42a1b )
by Cristian
15:19 queued 07:45
created

ResetsPasswords::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Auth;
4
5
use Illuminate\Auth\Events\PasswordReset;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
use Illuminate\Support\Facades\Password;
11
use Illuminate\Support\Str;
12
use Illuminate\Validation\ValidationException;
13
14
trait ResetsPasswords
15
{
16
    use RedirectsUsers;
17
18
    /**
19
     * Display the password reset view for the given token.
20
     *
21
     * If no token is present, display the link request form.
22
     *
23
     * @param  \Illuminate\Http\Request  $request
24
     * @param  string|null  $token
25
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
26
     */
27
    public function showResetForm(Request $request, $token = null)
28
    {
29
        return view('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...
30
            ['token' => $token, 'email' => $request->email]
31
        );
32
    }
33
34
    /**
35
     * Reset the given user's password.
36
     *
37
     * @param  \Illuminate\Http\Request  $request
38
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
39
     */
40
    public function reset(Request $request)
41
    {
42
        $request->validate($this->rules(), $this->validationErrorMessages());
43
44
        // Here we will attempt to reset the user's password. If it is successful we
45
        // will update the password on an actual user model and persist it to the
46
        // database. Otherwise we will parse the error and return the response.
47
        $response = $this->broker()->reset(
48
            $this->credentials($request), function ($user, $password) {
49
                $this->resetPassword($user, $password);
50
            }
51
        );
52
53
        // If the password was successfully reset, we will redirect the user back to
54
        // the application's home authenticated view. If there is an error we can
55
        // redirect them back to where they came from with their error message.
56
        return $response == Password::PASSWORD_RESET
57
                    ? $this->sendResetResponse($request, $response)
58
                    : $this->sendResetFailedResponse($request, $response);
59
    }
60
61
    /**
62
     * Get the password reset validation rules.
63
     *
64
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
65
     */
66
    protected function rules()
67
    {
68
        return [
69
            'token' => 'required',
70
            'email' => 'required|email',
71
            'password' => 'required|confirmed|min:8',
72
        ];
73
    }
74
75
    /**
76
     * Get the password reset validation error messages.
77
     *
78
     * @return array
79
     */
80
    protected function validationErrorMessages()
81
    {
82
        return [];
83
    }
84
85
    /**
86
     * Get the password reset credentials from the request.
87
     *
88
     * @param  \Illuminate\Http\Request  $request
89
     * @return array
90
     */
91
    protected function credentials(Request $request)
92
    {
93
        return $request->only(
94
            'email', 'password', 'password_confirmation', 'token'
95
        );
96
    }
97
98
    /**
99
     * Reset the given user's password.
100
     *
101
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
102
     * @param  string  $password
103
     * @return void
104
     */
105
    protected function resetPassword($user, $password)
106
    {
107
        $this->setUserPassword($user, $password);
108
109
        $user->setRememberToken(Str::random(60));
110
111
        $user->save();
112
113
        event(new PasswordReset($user));
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contra...\Auth\CanResetPassword>, but the function expects a object<Illuminate\Contracts\Auth\Authenticatable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
115
        $this->guard()->login($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contra...\Auth\CanResetPassword>, but the function expects a object<Illuminate\Contracts\Auth\Authenticatable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
    }
117
118
    /**
119
     * Set the user's password.
120
     *
121
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
122
     * @param  string  $password
123
     * @return void
124
     */
125
    protected function setUserPassword($user, $password)
126
    {
127
        $user->password = Hash::make($password);
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\CanResetPassword suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
128
    }
129
130
    /**
131
     * Get the response for a successful password reset.
132
     *
133
     * @param  \Illuminate\Http\Request  $request
134
     * @param  string  $response
135
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
136
     */
137
    protected function sendResetResponse(Request $request, $response)
138
    {
139
        if ($request->wantsJson()) {
140
            return new JsonResponse(['message' => trans($response)], 200);
141
        }
142
143
        return redirect($this->redirectPath())
144
                            ->with('status', trans($response));
145
    }
146
147
    /**
148
     * Get the response for a failed password reset.
149
     *
150
     * @param  \Illuminate\Http\Request  $request
151
     * @param  string  $response
152
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
153
     */
154 View Code Duplication
    protected function sendResetFailedResponse(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...
155
    {
156
        if ($request->wantsJson()) {
157
            throw ValidationException::withMessages([
158
                'email' => [trans($response)],
159
            ]);
160
        }
161
162
        return redirect()->back()
163
                    ->withInput($request->only('email'))
164
                    ->withErrors(['email' => trans($response)]);
165
    }
166
167
    /**
168
     * Get the broker to be used during password reset.
169
     *
170
     * @return \Illuminate\Contracts\Auth\PasswordBroker
171
     */
172
    public function broker()
173
    {
174
        return Password::broker();
175
    }
176
177
    /**
178
     * Get the guard to be used during password reset.
179
     *
180
     * @return \Illuminate\Contracts\Auth\StatefulGuard
181
     */
182
    protected function guard()
183
    {
184
        return Auth::guard();
185
    }
186
}
187