Completed
Pull Request — master (#107)
by
unknown
47:04 queued 15:58
created

EditDevice::getValidatorInstance()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 15
cp 0
rs 8.6737
cc 5
eloc 11
nc 1
nop 0
crap 30
1
<?php
2
3
namespace App\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use App\Http\Requests\Request;
7
use Illuminate\Support\Facades\Auth;
8
9
class EditDevice 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
        return Auth::user()->isUser();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method isUser() does only exist in the following implementations of said interface: App\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
19
    }
20
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules()
27
    {
28
        //TODO figure out way for unique location names for each specific site
29
        return [
30
            'name' => 'sometimes|nullable|min:2|max:75|name',
31
            'open_time' => 'sometimes|nullable|date_format:H:i',
32
            'close_time' => 'sometimes|nullable|date_format:H:i',
33
            'update_rate' => 'sometimes|nullable|integer|digits_between:1,7',
34
            'image_rate' => 'sometimes|nullable|integer|digits_between:1,7',
35
            'sensor_rate' => 'sometimes|nullable|integer|digits_between:1,7',
36
            'command' => 'sometimes|nullable|alpha|max:6|in:open,close,lock,unlock',
37
        ];
38
    }
39
    
40
    /**
41
     * Get the validation rules that apply to the request.
42
     *
43
     * @return \Illuminate\Contracts\Validation\Validator
44
     */
45
    protected function getValidatorInstance()
46
    {
47
        $validator = parent::getValidatorInstance();
48
    
49
        $validator->sometimes('site_id', 'bail|integer|digits_between:1,10|exists:sites,id', function ($input) {
50
            return !$input->new_site_name && $input->site_id;
51
        });
52
    
53
        $validator->sometimes('location_id', 'bail|integer|digits_between:1,10|exists:locations,id', function ($input) {
54
            return !$input->new_location_name && $input->location_id;
55
        });
56
    
57
        $validator->sometimes('new_site_name', 'bail|min:2|max:75|name|unique:sites,name', function ($input) {
58
            return !$input->site_id && $input->name;
59
        });
60
    
61
        $validator->sometimes('new_location_name', 'bail|min:2|max:75|name|unique:locations,name', function ($input) {
62
            return !$input->location_id && $input->name;
63
        });
64
        
65
        return $validator;
66
    }
67
    
68
    /**
69
     * Get the error messages for the defined validation rules.
70
     *
71
     * @return array
72
     */
73
    public function messages()
74
    {
75
        return [
76
            'name.required' => 'A device name is required',
77
            'site_id.required' => 'A site is required',
78
            'new_site_name.required' => 'A site is required',
79
            'location_id.required' => 'A location is required',
80
            'new_location_name.required' => 'A location is required',
81
            'open_time.required'  => 'A open time for the device is required',
82
            'open_time.date_format:H:i'  => 'A open time must be in the format hour:minutes',
83
            'close_time.required'  => 'A close time for the device is required',
84
            'close_time.date_format:H:i'  => 'A close time must be in the format hour:minutes',
85
            'update_rate.required'  => 'A update rate for the device is required',
86
            'image_rate.required'  => 'A image rate for the device is required',
87
            'sensor_rate.required'  => 'A sensor rate for the device is required',
88
        ];
89
    }
90
}
91