Passed
Push — master ( 7c54d7...41926f )
by Zing
05:15
created

QueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

55
        return new self(/** @scrutinizer ignore-type */ $baseQuery, $request);
Loading history...
56
    }
57
58
    /**
59
     * @param string $name
60
     * @param mixed[] $arguments
61
     *
62
     * @return $this|mixed
63
     */
64 39
    public function __call($name, $arguments)
65
    {
66 39
        $result = $this->forwardCallTo($this->builder, $name, $arguments);
67
68
        /*
69
         * If the forwarded method call is part of a chain we can return $this
70
         * instead of the actual $result to keep the chain going.
71
         */
72 39
        if ($result === $this->builder) {
73 3
            return $this;
74
        }
75
76 39
        return $result;
77
    }
78
}
79