Passed
Push — master ( ef1328...91a1d1 )
by Bas
13:32 queued 09:25
created

BuildsSubqueries::parseSub()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 13.608

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 23
ccs 8
cts 13
cp 0.6153
rs 8.0555
cc 9
nc 5
nop 1
crap 13.608
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\FluentAQL\Expressions\FunctionExpression;
9
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
10
11
trait BuildsSubqueries
12
{
13
    /**
14
     * Add a subselect expression to the query.
15
     *
16
     * @param $query
17
     * @param  string  $as
18
     * @return $this
19
     *
20
     */
21 1
    public function selectSub($query, $as)
22
    {
23 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

23
        /** @scrutinizer ignore-call */ 
24
        $query = $this->createSub($query);
Loading history...
24
25
        // Register $as as attribute alias
26 1
        $this->grammar->registerColumnAlias($as, $as);
27
28
        // Set $as as return value
29 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...
30
31
        // let alias = query
32 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...
33
34 1
        return $query;
35
    }
36
37
    /**
38
     * Parse the subquery into SQL and bindings.
39
     *
40
     * @param  mixed  $query
41
     * @return array|QueryBuilder
42
     *
43
     * @throws \InvalidArgumentException
44
     */
45 2
    protected function parseSub($query)
46
    {
47 2
        if ($query instanceof self || $query instanceof EloquentBuilder || $query instanceof Relation) {
48 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

48
            /** @scrutinizer ignore-call */ 
49
            $query = $this->prependDatabaseNameIfCrossDatabaseQuery($query);
Loading history...
49 1
            $query->grammar->compileSelect($query);
50
51 1
            if (isset($query->limit) && $query->limit == 1) {
52
                //Return the value, not an array of values
53 1
                return $query->aqb->first($query->aqb);
54
            }
55
            return $query->aqb;
56
        }
57
58 1
        if ($query instanceof QueryBuilder || $query instanceof FunctionExpression) {
59 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...
60
        }
61
62
        if (is_string($query)) {
63
            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...
64
        }
65
66
        throw new InvalidArgumentException(
67
            'A subquery must be a query builder instance, a Closure, or a string.'
68
        );
69
    }
70
}
71