Passed
Push — dependabot/github_actions/depe... ( d1016c )
by
unknown
11:16
created

BuildsSubqueries   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 18
c 2
b 0
f 0
dl 0
loc 58
ccs 14
cts 19
cp 0.7368
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A selectSub() 0 14 1
B parseSub() 0 25 9
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 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  Builder|EloquentBuilder|Relation|QueryBuilder|FunctionExpression|string  $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
                /** @phpstan-ignore-next-line */
54 1
                return $query->aqb->first($query->aqb);
55
            }
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