Completed
Push — devops/catch-breaking-changes-... ( 88c9a6 )
by Bas
28s queued 18s
created

BuildsSubqueries::parseSub()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.9991

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 24
ccs 8
cts 12
cp 0.6667
rs 8.0555
cc 9
nc 5
nop 1
crap 11.9991
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use InvalidArgumentException;
8
use LaravelFreelancerNL\Aranguent\Query\Builder;
9
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
10
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
11
12
trait BuildsSubqueries
13
{
14
    /**
15
     * Add a subselect expression to the query.
16
     *
17
     * @param Builder $query
18
     * @param  string  $as
19
     * @return $this
20
     *
21
     */
22 1
    public function selectSub($query, $as)
23
    {
24 1
        $query = $this->createSub($query);
0 ignored issues
show
Bug introduced by
It seems like createSub() 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

24
        /** @scrutinizer ignore-call */ 
25
        $query = $this->createSub($query);
Loading history...
25
26
        // Register $as as attribute alias
27 1
        $this->grammar->registerColumnAlias($as, $as);
28
29
        // Set $as as return value
30 1
        $this->columns[] = $as;
0 ignored issues
show
Bug Best Practice introduced by
The property columns does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32
        // let alias = query
33 1
        $this->variables[$as] = $query;
0 ignored issues
show
Bug Best Practice introduced by
The property variables does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
35 1
        return $query;
36
    }
37
38
    /**
39
     * Parse the subquery into SQL and bindings.
40
     *
41
     * @param  Builder|EloquentBuilder|Relation|QueryBuilder|FunctionExpression|string  $query
42
     * @return array|QueryBuilder
43
     *
44
     * @throws \InvalidArgumentException
45
     */
46 2
    protected function parseSub($query)
47
    {
48 2
        if ($query instanceof self || $query instanceof EloquentBuilder || $query instanceof Relation) {
49 1
            $query = $this->prependDatabaseNameIfCrossDatabaseQuery($query);
0 ignored issues
show
Bug introduced by
It seems like prependDatabaseNameIfCrossDatabaseQuery() 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

49
            /** @scrutinizer ignore-call */ 
50
            $query = $this->prependDatabaseNameIfCrossDatabaseQuery($query);
Loading history...
50 1
            $query->grammar->compileSelect($query);
51
52 1
            if (isset($query->limit) && $query->limit == 1) {
53
                //Return the value, not an array of values
54
                /** @phpstan-ignore-next-line */
55 1
                return $query->aqb->first($query->aqb);
56
            }
57
            return $query->aqb;
58
        }
59
60 1
        if ($query instanceof QueryBuilder || $query instanceof FunctionExpression) {
61 1
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query also could return the type LaravelFreelancerNL\Flue...ions\FunctionExpression which is incompatible with the documented return type LaravelFreelancerNL\FluentAQL\QueryBuilder|array.
Loading history...
62
        }
63
64
        if (is_string($query)) {
0 ignored issues
show
introduced by
The condition is_string($query) is always true.
Loading history...
65
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type string which is incompatible with the documented return type LaravelFreelancerNL\FluentAQL\QueryBuilder|array.
Loading history...
66
        }
67
68
        throw new InvalidArgumentException(
69
            'A subquery must be a query builder instance, a Closure, or a string.'
70
        );
71
    }
72
}
73