Completed
Push — master ( 75298d...3aa483 )
by
unknown
02:10
created

QueryBuilderRequest::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 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->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
    public function sorts(): Collection
75
    {
76
        $sortParts = $this->sort();
0 ignored issues
show
Documentation Bug introduced by
The method sort does not exist on object<Spatie\QueryBuilder\QueryBuilderRequest>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
78
        if (is_string($sortParts)) {
79
            $sortParts = explode(',', $sortParts);
80
        }
81
82
        return collect($sortParts)->filter();
83
    }
84
85 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...
86
    {
87
        if (is_array($value)) {
88
            return collect($value)->map(function ($valueValue) {
89
                return $this->getFilterValue($valueValue);
90
            })->all();
91
        }
92
93
        if (Str::contains($value, ',')) {
94
            return explode(',', $value);
95
        }
96
97
        if ($value === 'true') {
98
            return true;
99
        }
100
101
        if ($value === 'false') {
102
            return false;
103
        }
104
105
        return $value;
106
    }
107
}
108