Passed
Pull Request — next (#84)
by Bas
15:17 queued 11:12
created

CompilesUnions::compileUnions()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 2
b 0
f 0
nc 24
nop 2
dl 0
loc 33
ccs 18
cts 18
cp 1
crap 6
rs 9.0777
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Illuminate\Database\Query\Builder as IlluminateBuilder;
8
use LaravelFreelancerNL\Aranguent\Query\Builder;
9
10
trait CompilesUnions
11
{
12
    /**
13
     * Compile the "union" queries attached to the main query.
14
     *
15
     * @param Builder $query
16
     * @param string $firstQuery
17
     * @return string
18
     */
19 5
    protected function compileUnions(IlluminateBuilder $query, $firstQuery = '')
20
    {
21 5
        $unionResultsId = 'union' . $query->getQueryId() . 'Results';
22 5
        $unionDocId = 'union' . $query->getQueryId() . 'Result';
23
24 5
        $query->registerTableAlias($unionResultsId, $unionDocId);
25
26 5
        $firstQuery = $this->wrapSubquery($firstQuery);
0 ignored issues
show
Bug introduced by
It seems like wrapSubquery() 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

26
        /** @scrutinizer ignore-call */ 
27
        $firstQuery = $this->wrapSubquery($firstQuery);
Loading history...
27 5
        $unions = '';
28 5
        foreach ($query->unions as $union) {
29 5
            $prefix = ($unions !== '') ? $unions : $firstQuery;
30 5
            $unions = $this->compileUnion($union, $prefix);
31
        }
32
33 5
        $aql = 'LET ' . $unionResultsId . ' = ' . $unions
34 5
            . ' FOR ' . $unionDocId . ' IN ' . $unionResultsId;
35
36
        // Union groups
37
38 5
        if (!empty($query->unionOrders)) {
39 2
            $aql .= ' ' . $this->compileOrders($query, $query->unionOrders, $unionResultsId);
0 ignored issues
show
Bug introduced by
It seems like compileOrders() 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

39
            $aql .= ' ' . $this->/** @scrutinizer ignore-call */ compileOrders($query, $query->unionOrders, $unionResultsId);
Loading history...
40
        }
41
42 5
        if ($query->unionOffset) {
43 1
            $aql .= ' ' . $this->compileOffset($query, $query->unionOffset);
0 ignored issues
show
Bug introduced by
It seems like compileOffset() 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

43
            $aql .= ' ' . $this->/** @scrutinizer ignore-call */ compileOffset($query, $query->unionOffset);
Loading history...
44
        }
45
46 5
        if ($query->unionLimit) {
47 1
            $aql .= ' ' . $this->compileLimit($query, $query->unionLimit);
0 ignored issues
show
Bug introduced by
The method compileLimit() does not exist on LaravelFreelancerNL\Aran...Concerns\CompilesUnions. Did you maybe mean compileUnion()? ( Ignorable by Annotation )

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

47
            $aql .= ' ' . $this->/** @scrutinizer ignore-call */ compileLimit($query, $query->unionLimit);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        }
49
50
        // Union aggregates?
51 5
        return $aql . ' RETURN ' . $unionDocId;
52
    }
53
54
    /**
55
     * Compile a single union statement.
56
     *
57
     * @param array<mixed> $union
58
     * @param string $aql
59
     * @return string
60
     */
61 5
    protected function compileUnion(array $union, string $aql = '')
62
    {
63 5
        $unionType = $union['all'] ? 'UNION' : 'UNION_DISTINCT';
64
65 5
        return $unionType . '(' . $aql . ', ' . $this->wrapSubquery($union['query']->toSql()) . ')';
66
    }
67
}
68