Completed
Push — master ( 20b252...4d5a46 )
by ARCANEDEV
04:35
created

CreatePostRequest::sanitize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 2
eloc 3
nc 1
nop 0
crap 6
1
<?php namespace Arcanesoft\Blog\Http\Requests\Admin\Posts;
2
3
use Illuminate\Support\Str;
4
use Illuminate\Validation\Rule;
5
6
/**
7
 * Class     CreatePostRequest
8
 *
9
 * @package  Arcanesoft\Blog\Http\Requests\Admin\Posts
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class CreatePostRequest extends PostRequest
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Main Functions
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Determine if the user is authorized to make this request.
20
     *
21
     * @return bool
22
     */
23
    public function authorize()
24
    {
25
        return true;
26
    }
27
28
    /**
29
     * Get the validation rules that apply to the request.
30
     *
31
     * @return array
32
     */
33
    public function rules()
34
    {
35
        return parent::rules() + [
0 ignored issues
show
Best Practice introduced by
The expression return parent::rules() +...$this->getSlugRule())); seems to be an array, but some of its elements' types (array<string|Illuminate\Validation\Rules\Unique>) are incompatible with the return type of the parent method Arcanesoft\Blog\Http\Req...osts\PostRequest::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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
36
            'slug' => ['required', $this->getSlugRule()],
37
        ];
38
    }
39
40
    /**
41
     * Sanitize the inputs.
42
     *
43
     * @return array
44
     */
45
    protected function sanitize()
46
    {
47
        $slug = Str::slug($this->get($this->has('slug') ? 'slug' : 'title', ''));
48
49
        return compact('slug');
50
    }
51
}
52