Completed
Pull Request — master (#223)
by Axel
04:10
created

QueryBuilderRequest::fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
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->query($parameter);
21
22
        if (! is_array($includeParts)) {
23
            $includeParts = explode(',', strtolower($this->query($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->query($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->query($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
    public function searches()
60
    {
61
        $searchParameter = config('query-builder.parameters.search');
62
63
        $searchParts = collect($this->query(null, []))
64
            ->filter(function ($value, $queryKey) use ($searchParameter) {
65
                return $queryKey === $searchParameter || Str::startsWith($queryKey, $searchParameter.':');
66
            })->toArray();
67
68
        if (is_string($searchParts)) {
69
            return collect();
70
        }
71
72
        $searches = collect($searchParts);
73
74
        return $searches->map(function ($value) {
75
            return $this->getSearchValue($value);
76
        });
77
    }
78
79 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...
80
    {
81
        $fieldsParameter = config('query-builder.parameters.fields');
82
83
        $fieldsPerTable = collect($this->query($fieldsParameter));
84
85
        if ($fieldsPerTable->isEmpty()) {
86
            return collect();
87
        }
88
89
        return $fieldsPerTable->map(function ($fields) {
90
            return explode(',', $fields);
91
        });
92
    }
93
94
    /**
95
     * @return array|string|null
96
     */
97
    public function sort()
98
    {
99
        return $this->query(config('query-builder.parameters.sort'));
100
    }
101
102
    public function sorts(): Collection
103
    {
104
        $sortParts = $this->sort();
105
106
        if (is_string($sortParts)) {
107
            $sortParts = explode(',', $sortParts);
108
        }
109
110
        return collect($sortParts)->filter();
111
    }
112
113 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...
114
    {
115
        if (is_array($value)) {
116
            return collect($value)->map(function ($valueValue) {
117
                return $this->getFilterValue($valueValue);
118
            })->all();
119
        }
120
121
        if (Str::contains($value, ',')) {
122
            return explode(',', $value);
123
        }
124
125
        if ($value === 'true') {
126
            return true;
127
        }
128
129
        if ($value === 'false') {
130
            return false;
131
        }
132
133
        return $value;
134
    }
135
136 View Code Duplication
    protected function getSearchValue($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...
137
    {
138
        if (is_array($value)) {
139
            return collect($value)->map(function ($valueValue) {
140
                return $this->getSearchValue($valueValue);
141
            })->all();
142
        }
143
144
        if (Str::contains($value, ',')) {
145
            return explode(',', $value);
146
        }
147
148
        if ($value === 'true') {
149
            return true;
150
        }
151
152
        if ($value === 'false') {
153
            return false;
154
        }
155
156
        return $value;
157
    }
158
}
159