Completed
Pull Request — master (#222)
by
unknown
01:43
created

QueryBuilderRequest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 60
loc 120
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromRequest() 0 4 1
A includes() 12 12 2
A appends() 12 12 2
A filters() 0 16 2
A fields() 14 14 2
A sort() 0 4 1
A sorts() 0 10 2
A getFilterValue() 22 22 5
A getPartsOfRequest() 0 12 3

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
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
9
class QueryBuilderRequest extends Request
10
{
11
    public static function fromRequest(Request $request): self
12
    {
13
        return static::createFrom($request, new self());
0 ignored issues
show
Documentation introduced by
$request is of type object<Illuminate\Http\Request>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
14
    }
15
16 View Code Duplication
    public function includes()
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...
17
    {
18
        $parameter = config('query-builder.parameters.include');
19
20
        $includeParts = $this->getPartsOfRequest($parameter);
21
22
        if (! is_array($includeParts)) {
23
            $includeParts = explode(',', strtolower($this->getPartsOfRequest($parameter)));
24
        }
25
26
        return collect($includeParts)->filter();
27
    }
28
29 View Code Duplication
    public function appends()
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...
30
    {
31
        $appendParameter = config('query-builder.parameters.append');
32
33
        $appendParts = $this->getPartsOfRequest($appendParameter);
34
35
        if (! is_array($appendParts)) {
36
            $appendParts = explode(',', strtolower($appendParts));
37
        }
38
39
        return collect($appendParts)->filter();
40
    }
41
42
    public function filters()
43
    {
44
        $filterParameter = config('query-builder.parameters.filter');
45
46
        $filterParts = $this->getPartsOfRequest($filterParameter);
47
48
        if (is_string($filterParts)) {
49
            return collect();
50
        }
51
52
        $filters = collect($filterParts);
53
54
        return $filters->map(function ($value) {
55
            return $this->getFilterValue($value);
56
        });
57
    }
58
59 View Code Duplication
    public function fields(): Collection
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...
60
    {
61
        $fieldsParameter = config('query-builder.parameters.fields');
62
63
        $fieldsPerTable = collect($this->getPartsOfRequest($fieldsParameter));
64
65
        if ($fieldsPerTable->isEmpty()) {
66
            return collect();
67
        }
68
69
        return $fieldsPerTable->map(function ($fields) {
70
            return explode(',', $fields);
71
        });
72
    }
73
74
    /**
75
     * @return array|string|null
76
     */
77
    public function sort()
78
    {
79
        return $this->getPartsOfRequest(config('query-builder.parameters.sort'));
80
    }
81
82
    public function sorts(): Collection
83
    {
84
        $sortParts = $this->sort();
85
86
        if (is_string($sortParts)) {
87
            $sortParts = explode(',', $sortParts);
88
        }
89
90
        return collect($sortParts)->filter();
91
    }
92
93 View Code Duplication
    protected function getFilterValue($value)
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...
94
    {
95
        if (is_array($value)) {
96
            return collect($value)->map(function ($valueValue) {
97
                return $this->getFilterValue($valueValue);
98
            })->all();
99
        }
100
101
        if (Str::contains($value, ',')) {
102
            return explode(',', $value);
103
        }
104
105
        if ($value === 'true') {
106
            return true;
107
        }
108
109
        if ($value === 'false') {
110
            return false;
111
        }
112
113
        return $value;
114
    }
115
116
    protected function getPartsOfRequest($parameter)
117
    {
118
        if (! empty($this->query($parameter, []))) {
119
            return $this->query($parameter, []);
120
        }
121
122
        if (! empty($this->json($parameter, []))) {
123
            return $this->json($parameter, []);
124
        }
125
126
        return [];
127
    }
128
}
129