Failed Conditions
Push — refactor/improve-static-analys... ( ba8000...c6edde )
by Bas
14:06
created

BuildsSubqueries::hasLimitOfOne()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
ccs 2
cts 3
cp 0.6667
crap 3.3332
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
10
use InvalidArgumentException;
11
use LaravelFreelancerNL\Aranguent\Query\Grammar;
12
13
trait BuildsSubqueries
14
{
15
    /**
16
     * IN SQL subqueries in selects and where's need to return a single value,
17
     * whereas subqueries in joins return an object. This variable lets the
18
     * compiler know how to return a single value.
19
     *
20
     * @var bool
21
     */
22 1
    public bool $returnSingleValue = false;
23
24 1
    /**
25
     * Creates a subquery and parse it.
26
     *
27 1
     * @param  \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query
28
     * @return array
29
     */
30 1
    protected function createSub($query, bool $returnSingleValue = false)
31
    {
32
        // If the given query is a Closure, we will execute it while passing in a new
33 1
        // query instance to the Closure. This will give the developer a chance to
34
        // format and work with the query before we cast it to a raw SQL string.
35 1
        if ($query instanceof Closure) {
36
            $callback = $query;
37
38
            $callback($query = $this->forSubQuery());
39
        }
40
41
        if ($returnSingleValue) {
42
            $query->returnSingleValue = $returnSingleValue;
0 ignored issues
show
Bug introduced by
The property returnSingleValue does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
Bug introduced by
The property returnSingleValue does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
43
        }
44
45
        return $this->parseSub($query);
46 2
    }
47
48 2
    /**
49 1
     * Create a new query instance for sub-query.
50 1
     *
51
     * @return \Illuminate\Database\Query\Builder
52 1
     */
53
    protected function forSubQuery()
54
    {
55 1
        return $this->newQuery();
0 ignored issues
show
Bug introduced by
It seems like newQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

55
        return $this->/** @scrutinizer ignore-call */ newQuery();
Loading history...
56
    }
57
58
    protected function hasLimitOfOne(IlluminateQueryBuilder $query)
59
    {
60 1
        if (isset($query->limit) && $query->limit == 1) {
61 1
            return true;
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Parse the subquery into AQL and bindings.
69
     *
70
     * @param  mixed  $query
71
     * @return array
72
     *
73
     * @throws \InvalidArgumentException
74
     */
75
    protected function parseSub($query)
76
    {
77
        if ($query instanceof self || $query instanceof EloquentBuilder || $query instanceof Relation) {
78
            $this->exchangeTableAliases($query);
0 ignored issues
show
Bug introduced by
It seems like exchangeTableAliases() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
            $this->/** @scrutinizer ignore-call */ 
79
                   exchangeTableAliases($query);
Loading history...
79
80
            $this->importBindings($query);
0 ignored issues
show
Bug introduced by
It seems like importBindings() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

80
            $this->/** @scrutinizer ignore-call */ 
81
                   importBindings($query);
Loading history...
81
82
            assert($this->grammar instanceof Grammar);
83
            $queryString = $this->grammar->wrapSubquery($query->toSql());
0 ignored issues
show
Bug introduced by
It seems like $query->toSql() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Eloquent\Relations\Relation and LaravelFreelancerNL\Aran...ncerns\BuildsSubqueries; however, parameter $subquery of LaravelFreelancerNL\Aran...Grammar::wrapSubquery() does only seem to accept string, 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

83
            $queryString = $this->grammar->wrapSubquery(/** @scrutinizer ignore-type */ $query->toSql());
Loading history...
Bug introduced by
It seems like toSql() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

83
            $queryString = $this->grammar->wrapSubquery($query->/** @scrutinizer ignore-call */ toSql());
Loading history...
84
85
            if ($this->hasLimitOfOne($query)) {
0 ignored issues
show
Bug introduced by
$query of type Illuminate\Database\Eloq...ncerns\BuildsSubqueries is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $query of LaravelFreelancerNL\Aran...ueries::hasLimitOfOne(). ( Ignorable by Annotation )

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

85
            if ($this->hasLimitOfOne(/** @scrutinizer ignore-type */ $query)) {
Loading history...
86
                $queryString = 'FIRST(' . $queryString . ')';
87
            }
88
89
            return [$queryString, $query->getBindings()];
0 ignored issues
show
Bug introduced by
It seems like getBindings() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

89
            return [$queryString, $query->/** @scrutinizer ignore-call */ getBindings()];
Loading history...
90
        } elseif (is_string($query)) {
91
            return [$query, []];
92
        } else {
93
            throw new InvalidArgumentException(
94
                'A subquery must be a query builder instance, a Relation, a Closure, or a string.'
95
            );
96
        }
97
    }
98
}
99