Completed
Push — develop ( 4e1646...0038a1 )
by Abdelrahman
02:00
created

AccountPasswordRequest::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class AccountPasswordRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @throws \Cortex\Foundation\Exceptions\GenericException
15
     *
16
     * @return bool
17
     */
18
    public function authorize(): bool
19
    {
20
        return true;
21
    }
22
23
    /**
24
     * Configure the validator instance.
25
     *
26
     * @param \Illuminate\Validation\Validator $validator
27
     *
28
     * @return void
29
     */
30
    public function withValidator($validator): void
31
    {
32
        $validator->after(function ($validator) {
33
            if (! auth()->guard($this->route('guard'))->getProvider()->validateCredentials($this->user($this->route('guard')), ['password' => $this->get('old_password')])) {
0 ignored issues
show
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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 173 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...
34
                $validator->errors()->add('old_password', trans('cortex/auth::messages.account.wrong_password'));
35
            }
36
37
            if (auth()->guard($this->route('guard'))->getProvider()->validateCredentials($this->user($this->route('guard')), ['password' => $this->get('new_password')])) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 171 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...
38
                $validator->errors()->add('new_password', trans('cortex/auth::messages.account.different_password'));
39
            }
40
        });
41
    }
42
43
    /**
44
     * Get the validation rules that apply to the request.
45
     *
46
     * @return array
47
     */
48
    public function rules(): array
49
    {
50
        return [
51
            'old_password' => 'required|min:'.config('cortex.auth.password_min_chars'),
52
            'new_password' => 'required|confirmed|min:'.config('cortex.auth.password_min_chars'),
53
        ];
54
    }
55
}
56