UpdatePostRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 45
ccs 0
cts 18
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
A rules() 0 9 1
A sanitize() 0 7 3
1
<?php namespace Arcanesoft\Blog\Http\Requests\Admin\Posts;
2
3
use Illuminate\Support\Str;
4
5
/**
6
 * Class     UpdatePostRequest
7
 *
8
 * @package  Arcanesoft\Blog\Http\Requests\Admin\Posts
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class UpdatePostRequest extends PostRequest
12
{
13
    /* -----------------------------------------------------------------
14
     |  Main Methods
15
     | -----------------------------------------------------------------
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
        /** @var  \Arcanesoft\Blog\Models\Post  $post */
36
        $post = $this->route('blog_post');
37
38
        return array_merge(parent::rules(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...->ignore($post->id)))); (array<string,array<strin...lidation\Rules\Unique>>) is 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 '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...
39
            'slug' => ['required', 'string', $this->getSlugRule()->ignore($post->id)],
40
        ]);
41
    }
42
43
    /**
44
     * Sanitize the inputs.
45
     *
46
     * @return array
47
     */
48
    protected function sanitize()
49
    {
50
        return [
51
            'slug'      => Str::slug($this->get($this->has('slug') ? 'slug' : 'title', '')),
52
            'seo_title' => $this->get(empty($this->get('seo_title')) ? 'title' : 'seo_title'),
53
        ];
54
    }
55
}
56