UpdateUserRequest::getValidatedData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\Http\Requests\Admin\Users;
2
3
/**
4
 * Class     UpdateUserRequest
5
 *
6
 * @package  Arcanesoft\Auth\Http\Requests\Admin\Users
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class UpdateUserRequest extends UserFormRequest
10
{
11
    /* -----------------------------------------------------------------
12
     |  Main Methods
13
     | -----------------------------------------------------------------
14
     */
15
16
    /**
17
     * Determine if the user is authorized to make this request.
18
     *
19
     * @return bool
20
     */
21
    public function authorize()
22
    {
23
        return true;
24
    }
25
26
    /**
27
     * Get the validation rules that apply to the request.
28
     *
29
     * @return array
30
     */
31
    public function rules()
32
    {
33
        /** @var  \Arcanesoft\Contracts\Auth\Models\User  $user */
34
        $user = $this->route('auth_user');
35
36
        return array_merge(parent::rules(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...ired_with:password'))); (array<string,array<strin...lidation\Rules\Unique>>) is incompatible with the return type of the parent method Arcanesoft\Auth\Http\Req...\UserFormRequest::rules of type array<string,string[]>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
37
            'username'              => ['required', 'string', 'min:3', $this->getUsernameRule()->ignore($user->id)],
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\User 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...
38
            'email'                 => ['required', 'string', 'email', $this->getEmailRule()->ignore($user->id)],
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\User 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...
39
            'password'              => ['nullable', 'string', 'required_with:password_confirmation', 'min:8', 'confirmed'],
40
            'password_confirmation' => ['nullable', 'string', 'required_with:password'],
41
        ]);
42
    }
43
44
    /* -----------------------------------------------------------------
45
     |  Other Methods
46
     | -----------------------------------------------------------------
47
     */
48
49
    /**
50
     * Get the validated data.
51
     *
52
     * @return array
53
     */
54
    public function getValidatedData()
55
    {
56
        return array_filter(
57
            $this->only(['username', 'email', 'password', 'first_name', 'last_name'])
58
        );
59
    }
60
}
61