Completed
Push — master ( 78de56...a458a2 )
by Abdelrahman
08:50 queued 10s
created

AccountPasswordRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
A withValidator() 0 12 3
A rules() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Tenantarea;
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...
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')])) {
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