Completed
Push — master ( 68a553...e9f254 )
by Milroy
01:21
created

QueryBuilderAbstract::appendParamsToPaginatedUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.7998
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Query;
6
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Http\Request;
9
use Sarala\Contracts\QueryContract;
10
use Illuminate\Database\Eloquent\Builder;
11
12
abstract class QueryBuilderAbstract implements QueryContract
13
{
14
    use QueryMapper;
15
16
    /** @var Request $request */
17
    protected $request;
18
19
    /** @var Builder $query */
20
    protected $query;
21
22
    /** @var Fields $fields */
23
    protected $fields;
24
25
    /** @var QueryParamBag $includes */
26
    protected $includes;
27
28
    /** @var QueryParamBag $filters */
29
    protected $filters;
30
31
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
32
    {
33
        $this->request = $request;
34
        $this->fields = $this->request->fields();
35
        $this->includes = $this->request->includes();
36
        $this->filters = $this->request->filters();
37
        $this->query = $this->init();
38
    }
39
40
    public function fields()
41
    {
42
        // ..
43
    }
44
45
    public function filter(QueryParamBag $filters)
0 ignored issues
show
Unused Code introduced by
The parameter $filters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        // ..
48
    }
49
50
    public function include(QueryParamBag $includes)
51
    {
52
        // ..
53
    }
54
55
    public function orderBy(): array
56
    {
57
        return [];
58
    }
59
60
    public function fetch()
61
    {
62
        $this->fields();
63
64
        if ($this->request->filled('filter')) {
65
            $this->filter($this->filters);
66
        }
67
68
        if ($this->request->filled('include')) {
69
            $this->include($this->includes);
70
        }
71
72
        if ($this->request->filled('sort')) {
73
            $this->appendSortQuery();
74
        }
75
76
        if ($this->request->filled('page')) {
77
            return $this->appendParamsToPaginatedUrl();
78
        }
79
80
        return $this->query->get();
81
    }
82
83
    private function appendSortQuery(): void
84
    {
85
        $allowedSorts = $this->orderBy();
86
87
        $this->request->sorts()->each(function (SortField $field) use ($allowedSorts) {
88
            if (in_array($field->getField(), $allowedSorts)) {
89
                $this->query->orderBy($field->getField(), $field->getDirection());
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
            }
91
        });
92
    }
93
94
    private function appendParamsToPaginatedUrl(): LengthAwarePaginator
95
    {
96
        $perPage = $this->request->input('page.size', 10);
97
        $page = $this->request->input('page.number', 1);
98
        $paginator = $this->query->paginate($perPage, ['*'], 'page[number]', $page);
0 ignored issues
show
Bug introduced by
It seems like $perPage defined by $this->request->input('page.size', 10) on line 96 can also be of type array or string; however, Illuminate\Database\Eloquent\Builder::paginate() does only seem to accept integer|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $page defined by $this->request->input('page.number', 1) on line 97 can also be of type array or string; however, Illuminate\Database\Eloquent\Builder::paginate() does only seem to accept integer|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
100
        $paginator->appends($this->request->except('page'));
101
102
        if (! is_null($this->request->input('page.size'))) {
103
            $paginator->appends('page[size]', $this->request->input('page.size'));
0 ignored issues
show
Bug introduced by
It seems like $this->request->input('page.size') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Contracts\Pag...on\Paginator::appends() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
        }
105
106
        return $paginator;
107
    }
108
109
    public function fetchFirst()
110
    {
111
        return $this->fetch()->first();
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Eloquent\Collection, but not in Illuminate\Contracts\Pag...on\LengthAwarePaginator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
112
    }
113
}
114