Completed
Push — master ( 187043...48cde4 )
by
unknown
01:30
created

QueryBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 3
cbo 8
dl 0
loc 97
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A for() 0 9 2
A get() 0 10 2
A initializeFromBuilder() 0 16 1
A setThrowInvalidQueryExceptions() 0 6 1
A disableInvalidQueryExceptions() 0 4 1
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Http\Request;
7
use Spatie\QueryBuilder\Concerns\AddsFieldsToQuery;
8
use Spatie\QueryBuilder\Concerns\AddsIncludesToQuery;
9
use Spatie\QueryBuilder\Concerns\AppendsAttributesToResults;
10
use Spatie\QueryBuilder\Concerns\FiltersQuery;
11
use Spatie\QueryBuilder\Concerns\SortsQuery;
12
13
class QueryBuilder extends Builder
14
{
15
    use FiltersQuery,
16
        SortsQuery,
17
        AddsIncludesToQuery,
18
        AddsFieldsToQuery,
19
        AppendsAttributesToResults;
20
21
    /** @var \Spatie\QueryBuilder\QueryBuilderRequest */
22
    protected $request;
23
24
    protected $throwInvalidQueryExceptions = true;
25
26
    /**
27
     * QueryBuilder constructor.
28
     *
29
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
30
     * @param null|\Illuminate\Http\Request $request
31
     */
32
    public function __construct($builder, ?Request $request = null)
33
    {
34
        parent::__construct(clone $builder->getQuery());
35
36
        $this->initializeFromBuilder($builder);
0 ignored issues
show
Bug introduced by Alex Vanderbist
It seems like $builder defined by parameter $builder on line 32 can also be of type object<Illuminate\Databa...ent\Relations\Relation>; however, Spatie\QueryBuilder\Quer...initializeFromBuilder() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
37
38
        $this->request = $request
39
            ? QueryBuilderRequest::fromRequest($request)
40
            : app(QueryBuilderRequest::class);
41
    }
42
43
    /**
44
     * Create a new QueryBuilder for a request and model.
45
     *
46
     * @param string|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $baseQuery Model class or base query builder
47
     * @param \Illuminate\Http\Request $request
48
     *
49
     * @return \Spatie\QueryBuilder\QueryBuilder
50
     */
51
    public static function for($baseQuery, ?Request $request = null): self
52
    {
53
        if (is_string($baseQuery)) {
54
            /** @var Builder $baseQuery */
55
            $baseQuery = $baseQuery::query();
56
        }
57
58
        return new static($baseQuery, $request ?? request());
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function get($columns = ['*'])
65
    {
66
        $results = parent::get($columns);
67
68
        if ($this->request->appends()->isNotEmpty()) {
69
            $results = $this->addAppendsToResults($results);
0 ignored issues
show
Bug introduced by Alex Vanderbist
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...
70
        }
71
72
        return $results;
73
    }
74
75
    /**
76
     * Add the model, scopes, eager loaded relationships, local macro's and onDelete callback
77
     * from the $builder to this query builder.
78
     *
79
     * @param \Illuminate\Database\Eloquent\Builder $builder
80
     */
81
    protected function initializeFromBuilder(Builder $builder)
82
    {
83
        $this
84
            ->setModel($builder->getModel())
0 ignored issues
show
Bug introduced by freek
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...
85
            ->setEagerLoads($builder->getEagerLoads());
86
87
        $builder->macro('getProtected', function (Builder $builder, string $property) {
88
            return $builder->{$property};
89
        });
90
91
        $this->scopes = $builder->getProtected('scopes');
92
93
        $this->localMacros = $builder->getProtected('localMacros');
94
95
        $this->onDelete = $builder->getProtected('onDelete');
96
    }
97
98
    public function setThrowInvalidQueryExceptions(bool $throwInvalidQueryExceptions = true): self
99
    {
100
        $this->throwInvalidQueryExceptions = $throwInvalidQueryExceptions;
101
102
        return $this;
103
    }
104
105
    public function disableInvalidQueryExceptions(): self
106
    {
107
        return $this->setThrowInvalidQueryExceptions(false);
108
    }
109
}
110