Completed
Push — master ( b51238...70c289 )
by Brandon
18s
created

EditDevice   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 52
ccs 0
cts 6
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
A rules() 0 15 1
A messages() 0 13 1
1
<?php
2
3
namespace App\Http\Requests;
4
5
use Illuminate\Foundation\Http\FormRequest;
6
use Illuminate\Support\Facades\Auth;
7
8
class EditDevice extends FormRequest
9
{
10
    /**
11
     * Determine if the user is authorized to make this request.
12
     *
13
     * @return bool
14
     */
15
    public function authorize()
16
    {
17
        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...
18
    }
19
20
    /**
21
     * Get the validation rules that apply to the request.
22
     *
23
     * @return array
24
     */
25
    public function rules()
26
    {
27
        return [
28
            'name' => 'required|string|max:255',
29
            'site' => 'required_without:new_site_name|int|max:255|nullable',
30
            'new_site_name' => 'required_without:site|unique:sites,name|nullable',
31
            'location' => 'required_without:new_location_name|int|max:255|nullable',
32
            'new_location_name' => 'required_without:location|unique:locations,name|string|max:255|nullable',
33
            'open_time' => 'required|date_format:H:i',
34
            'close_time' => 'required|date_format:H:i',
35
            'update_rate' => 'required|int|max:255',
36
            'image_rate' => 'required|int|max:255',
37
            'sensor_rate' => 'required|int|max:255',
38
        ];
39
    }
40
    
41
    /**
42
     * Get the error messages for the defined validation rules.
43
     *
44
     * @return array
45
     */
46
    public function messages()
47
    {
48
        return [
49
            'name.required' => 'A device name is required',
50
            'open_time.required'  => 'A open time for the device is required',
51
            'open_time.date_format:H:i'  => 'A open time most be in the format hour:minutes',
52
            'close_time.required'  => 'A close time for the device is required',
53
            'close_time.date_format:H:i'  => 'A close time most be in the format hour:minutes',
54
            'update_rate.required'  => 'A update rate for the device is required',
55
            'image_rate.required'  => 'A image rate for the device is required',
56
            'sensor_rate.required'  => 'A sensor rate for the device is required',
57
        ];
58
    }
59
}
60