Completed
Pull Request — master (#337)
by
unknown
01:15
created

QueryBuilder::setExceptionInvalidFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Database\Eloquent\Builder;
7
use Spatie\QueryBuilder\Concerns\SortsQuery;
8
use Spatie\QueryBuilder\Concerns\FiltersQuery;
9
use Spatie\QueryBuilder\Concerns\AddsFieldsToQuery;
10
use Spatie\QueryBuilder\Concerns\AddsIncludesToQuery;
11
use Spatie\QueryBuilder\Concerns\AppendsAttributesToResults;
12
13
class QueryBuilder extends Builder
14
{
15
    /**
16
     * Allow ignore invalid fields without throwing an exception
17
     * if is false
18
     *
19
     * @var bool
20
     */
21
    protected $exceptionInvalidFilter = true;
22
23
    use FiltersQuery,
24
        SortsQuery,
25
        AddsIncludesToQuery,
26
        AddsFieldsToQuery,
27
        AppendsAttributesToResults;
28
29
    /** @var \Spatie\QueryBuilder\QueryBuilderRequest */
30
    protected $request;
31
32
    public function __construct(Builder $builder, ? Request $request = null)
33
    {
34
        parent::__construct(clone $builder->getQuery());
35
36
        $this->initializeFromBuilder($builder);
37
38
        $this->request = QueryBuilderRequest::fromRequest($request ?? request());
39
    }
40
41
    /**
42
     * Create a new QueryBuilder for a request and model.
43
     *
44
     * @param string|\Illuminate\Database\Eloquent\Builder $baseQuery Model class or base query builder
45
     * @param \Illuminate\Http\Request                  $request
46
     *
47
     * @return \Spatie\QueryBuilder\QueryBuilder
48
     */
49
    public static function for($baseQuery, ?Request $request = null): self
50
    {
51
        if (is_string($baseQuery)) {
52
            /** @var Builder $baseQuery */
53
            $baseQuery = $baseQuery::query();
54
        }
55
56
        return new static($baseQuery, $request ?? request());
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get($columns = ['*'])
63
    {
64
        $results = parent::get($columns);
65
66
        if ($this->request->appends()->isNotEmpty()) {
67
            $results = $this->addAppendsToResults($results);
0 ignored issues
show
Bug introduced by
It seems like $results can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Spatie\QueryBuilder\Conc...::addAppendsToResults() does only seem to accept object<Illuminate\Support\Collection>, 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...
68
        }
69
70
        return $results;
71
    }
72
73
    /**
74
     * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback
75
     * from the $builder to this query builder.
76
     *
77
     * @param \Illuminate\Database\Eloquent\Builder $builder
78
     */
79
    protected function initializeFromBuilder(Builder $builder)
80
    {
81
        $this
82
            ->setModel($builder->getModel())
0 ignored issues
show
Bug introduced by
It seems like $builder->getModel() targeting Illuminate\Database\Eloquent\Builder::getModel() can also be of type object<Illuminate\Database\Eloquent\Builder>; however, Illuminate\Database\Eloquent\Builder::setModel() does only seem to accept object<Illuminate\Database\Eloquent\Model>, 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...
83
            ->setEagerLoads($builder->getEagerLoads());
84
85
        $builder->macro('getProtected', function (Builder $builder, string $property) {
86
            return $builder->{$property};
87
        });
88
89
        $this->scopes = $builder->getProtected('scopes');
90
91
        $this->localMacros = $builder->getProtected('localMacros');
92
93
        $this->onDelete = $builder->getProtected('onDelete');
94
    }
95
96
    /**
97
     * Allow ignore invalid fields without throwing an exception
98
     *
99
     * @param bool $value
100
     */
101
    public function setExceptionInvalidFilter(bool $value = true)
102
    {
103
        $this->exceptionInvalidFilter = $value;
104
105
        return $this;
106
    }
107
}
108