These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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()); |
||
14 | } |
||
15 | |||
16 | public function includes() |
||
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() |
|
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 | View Code Duplication | public function fields(): Collection |
|
60 | { |
||
61 | $fieldsParameter = config('query-builder.parameters.fields'); |
||
62 | |||
63 | $fieldsPerTable = collect($this->query($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 | View Code Duplication | public function sorts(): Collection |
|
0 ignored issues
–
show
|
|||
75 | { |
||
76 | $sortParameter = config('query-builder.parameters.sort'); |
||
77 | |||
78 | $sortParts = $this->query($sortParameter); |
||
79 | |||
80 | if (is_string($sortParts)) { |
||
81 | $sortParts = explode(',', $sortParts); |
||
82 | } |
||
83 | |||
84 | return collect($sortParts)->filter(); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | protected function getFilterValue($value) |
|
88 | { |
||
89 | if (is_array($value)) { |
||
90 | return collect($value)->map(function ($valueValue) { |
||
91 | return $this->getFilterValue($valueValue); |
||
92 | })->all(); |
||
93 | } |
||
94 | |||
95 | if (Str::contains($value, ',')) { |
||
96 | return explode(',', $value); |
||
97 | } |
||
98 | |||
99 | if ($value === 'true') { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | if ($value === 'false') { |
||
104 | return false; |
||
105 | } |
||
106 | |||
107 | return $value; |
||
108 | } |
||
109 | } |
||
110 |
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.