Passed
Pull Request — master (#146)
by Zing
09:04 queued 03:59
created

QueryBuilder::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\WithSearchable;
15
use Zing\QueryBuilder\Concerns\WithSorts;
16
use Zing\QueryBuilder\Concerns\WithTypedFilter;
17
18
/**
19
 * @mixin \Illuminate\Database\Eloquent\Builder
20
 */
21
class QueryBuilder
22
{
23
    use WithFilters;
24
    use WithSearchable;
25
    use WithSorts;
26
    use Pageable;
27
    use ForwardsCalls;
28
    use WithTypedFilter;
29
30
    /**
31
     * @var \Illuminate\Http\Request
32
     */
33
    protected $request;
34
35
    /**
36
     * @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
37
     */
38
    protected $builder;
39
40
    /**
41
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
42
     * @param \Illuminate\Http\Request $request
43
     */
44 49
    public function __construct($builder, $request)
45
    {
46 49
        $this->builder = $builder;
47 49
        $this->request = $request;
48 49
    }
49
50
    /**
51
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|string $baseQuery
52
     */
53 49
    public static function fromBuilder($baseQuery, Request $request): self
54
    {
55 49
        if (is_subclass_of($baseQuery, Model::class)) {
56 48
            $baseQuery = forward_static_call([$baseQuery, 'query']);
57
        }
58
59 49
        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

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