QueryBuilder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 82
ccs 20
cts 20
cp 1
rs 10
c 4
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fromBuilder() 0 7 2
A clone() 0 3 1
A __get() 0 3 1
A __call() 0 5 2
A getBuilder() 0 7 2
A __construct() 0 4 1
A __clone() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Traits\ForwardsCalls;
12
use Zing\QueryBuilder\Concerns\Pageable;
13
use Zing\QueryBuilder\Concerns\WithFilters;
14
use Zing\QueryBuilder\Concerns\WithFlaggedFilter;
15
use Zing\QueryBuilder\Concerns\WithSearchable;
16
use Zing\QueryBuilder\Concerns\WithSorts;
17
use Zing\QueryBuilder\Concerns\WithTypedFilter;
18
19
/**
20
 * @mixin \Illuminate\Database\Eloquent\Builder
21
 */
22
class QueryBuilder
23
{
24
    use ForwardsCalls;
25
    use Pageable;
26
    use WithFilters;
27
    use WithFlaggedFilter;
28
    use WithSearchable;
29
    use WithSorts;
30
    use WithTypedFilter;
31
32
    /**
33
     * @var \Illuminate\Http\Request
34
     */
35
    protected $request;
36
37
    /**
38
     * @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
39
     */
40
    protected $builder;
41
42
    /**
43
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
44
     * @param \Illuminate\Http\Request $request
45
     */
46 66
    public function __construct($builder, $request)
47
    {
48 66
        $this->builder = $builder;
49 66
        $this->request = $request;
50
    }
51
52
    /**
53
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|string $baseQuery
54
     */
55 66
    public static function fromBuilder($baseQuery, Request $request): self
56
    {
57 66
        if (is_subclass_of($baseQuery, Model::class)) {
58 65
            $baseQuery = forward_static_call([$baseQuery, 'query']);
59
        }
60
61 66
        return new self($baseQuery, $request);
0 ignored issues
show
Bug introduced by
It seems like $baseQuery can also be of type string; however, parameter $builder of Zing\QueryBuilder\QueryBuilder::__construct() does only seem to accept Illuminate\Database\Eloq...uent\Relations\Relation, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return new self(/** @scrutinizer ignore-type */ $baseQuery, $request);
Loading history...
62
    }
63
64 54
    public function getBuilder(): Builder
65
    {
66 54
        if ($this->builder instanceof Relation) {
67 1
            return $this->builder->getQuery();
68
        }
69
70 53
        return $this->builder;
71
    }
72
73
    /**
74
     * @param string $name
75
     *
76
     * @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed
77
     */
78 1
    public function __get($name)
79
    {
80 1
        return $this->builder->{$name};
81
    }
82
83
    /**
84
     * @param string $name
85
     * @param mixed[] $arguments
86
     *
87
     * @return $this|mixed
88
     */
89 65
    public function __call($name, $arguments)
90
    {
91 65
        $result = $this->forwardCallTo($this->builder, $name, $arguments);
92
93 65
        return $result === $this->builder ? $this : $result;
94
    }
95
96 1
    public function clone(): self
97
    {
98 1
        return clone $this;
99
    }
100
101 2
    public function __clone()
102
    {
103 2
        $this->builder = clone $this->builder;
104
    }
105
}
106