Completed
Push — manage-articles ( 1fa998...1c60ef )
by Fèvre
02:30
created

ArticleValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 64.15 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 34
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 16 16 2
A update() 18 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Xetaravel\Models\Validators;
3
4
use Eloquence\Behaviours\Slug;
5
use Illuminate\Support\Facades\Validator as FacadeValidator;
6
use Illuminate\Validation\Rule;
7
use Illuminate\Validation\Validator;
8
use Mews\Purifier\Facades\Purifier;
9
10
class ArticleValidator
11
{
12
    /**
13
     * Get a validator for an incoming create request.
14
     *
15
     * @param array $data The data to validate.
16
     *
17
     * @return \Illuminate\Validation\Validator
18
     */
19 View Code Duplication
    public static function create(array $data): Validator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $rules = [
22
            'title' => 'required|min:5',
23
            'category_id' => 'required',
24
            'slug' => 'unique:articles',
25
            'content' => 'required|min:10'
26
        ];
27
28
        if (isset($data['content'])) {
29
            $data['content'] = Purifier::clean($data['content'], 'blog_article');
30
        }
31
        $data['slug'] = Slug::fromTitle($data['title']);
32
33
        return FacadeValidator::make($data, $rules);
34
    }
35
36
    /**
37
     * Get a validator for an incoming update request.
38
     *
39
     * @param array $data The data to validate.
40
     * @param int $id The actual article id to ignore the slug rule.
41
     *
42
     * @return \Illuminate\Validation\Validator
43
     */
44 View Code Duplication
    public static function update(array $data, int $id): Validator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $rules = [
47
            'title' => 'required|min:5',
48
            'category_id' => 'required',
49
            'slug' => [
50
                Rule::unique('articles')->ignore($id)
51
            ],
52
            'content' => 'required|min:10'
53
        ];
54
55
        if (isset($data['content'])) {
56
            $data['content'] = Purifier::clean($data['content'], 'blog_article');
57
        }
58
        $data['slug'] = Slug::fromTitle($data['title']);
59
60
        return FacadeValidator::make($data, $rules);
61
    }
62
}
63