Completed
Push — master ( d20695...7a110c )
by Cristian
02:12 queued 02:07
created

ChangePasswordRequest::withValidator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
3
namespace Backpack\Base\app\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Hash;
8
9
class ChangePasswordRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        // only allow updates if the user is logged in
19
        return Auth::check();
20
    }
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        return [
30
            'old_password'     => 'required',
31
            'new_password'     => 'required|min:6',
32
            'confirm_password' => 'required|same:new_password|min:6',
33
        ];
34
    }
35
36
    /**
37
     * Configure the validator instance.
38
     *
39
     * @param \Illuminate\Validation\Validator $validator
40
     *
41
     * @return void
42
     */
43
    public function withValidator($validator)
44
    {
45
        $validator->after(function ($validator) {
46
            // check old password matches
47
            if (!Hash::check($this->input('old_password'), Auth::user()->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable 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...
48
                $validator->errors()->add('old_password', trans('backpack::base.old_password_incorrect'));
49
            }
50
        });
51
    }
52
}
53