Completed
Push — master ( 49a390...e48438 )
by Andrea Marco
06:28
created

QueryFilters::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cerbero\QueryFilters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Abstract implementation of a query filters applier.
11
 *
12
 */
13
abstract class QueryFilters
14
{
15
    /**
16
     * The current HTTP request.
17
     *
18
     * @var Illuminate\Http\Request
19
     */
20
    protected $request;
21
22
    /**
23
     * Eloquent query builder.
24
     *
25
     * @var     Illuminate\Database\Eloquent\Builder
26
     */
27
    protected $query;
28
29
    /**
30
     * Set the dependencies.
31
     *
32
     * @param    Request    $request
33
     * @return    void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35
    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...
36
    {
37
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<Illuminate\Http\Request> is incompatible with the declared type object<Cerbero\QueryFilt...lluminate\Http\Request> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * Apply all the filters to the given query.
42
     *
43
     * @param    Illuminate\Database\Eloquent\Builder    $query
44
     * @return    Illuminate\Database\Eloquent\Builder
45
     */
46
    public function applyToQuery(Builder $query)
47
    {
48
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query of type object<Illuminate\Database\Eloquent\Builder> is incompatible with the declared type object<Cerbero\QueryFilt...abase\Eloquent\Builder> of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50
        foreach ($this->request->all() as $filter => $value) {
51
            $method = Str::camel($filter);
52
53
            if (method_exists($this, $method)) {
54
                call_user_func([$this, $method], $value);
55
            }
56
        }
57
58
        return $query;
59
    }
60
}
61