Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

CompilesUnions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileUnionsX() 0 28 3
A compileUnions() 0 29 3
A compileUnion() 0 5 2
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 Illuminate\Database\Query\Expression;
9
use LaravelFreelancerNL\Aranguent\Query\Builder;
10
11
trait CompilesUnions
12
{
13
    /**
14
     * Compile the "union" queries attached to the main query.
15
     *
16
     * @param Builder $query
17
     * @param string $aql
18
     * @return string
19
     */
20
    protected function compileUnions(IlluminateBuilder $query, $firstQuery = '')
21
    {
22
        $unionResultsId = 'union'.$query->getQueryId().'Results';
23
        $unionDocId = 'union'.$query->getQueryId().'Results';
24
25
        $firstQuery = $this->wrapUnion($firstQuery);
0 ignored issues
show
Bug introduced by
It seems like wrapUnion() 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

25
        /** @scrutinizer ignore-call */ 
26
        $firstQuery = $this->wrapUnion($firstQuery);
Loading history...
26
        $unions = '';
27
        foreach ($query->unions as $union) {
28
            $prefix = ($unions !== '') ? $unions : $firstQuery;
29
            $unions = $this->compileUnion($union, $prefix);
30
        }
31
32
        $aql = 'LET '.$unionResultsId.' = '.$unions
33
            .' FOR '.$unionDocId.'Doc IN '.$unionResultsId;
34
        $aql .= ' RETURN '.$unionDocId.'Doc';
35
36
//        if (! empty($query->unionOrders)) {
37
//            $sql .= ' '.$this->compileOrders($query, $query->unionOrders);
38
//        }
39
//
40
//        if (isset($query->unionLimit)) {
41
//            $sql .= ' '.$this->compileLimit($query, $query->unionLimit);
42
//        }
43
//
44
//        if (isset($query->unionOffset)) {
45
//            $sql .= ' '.$this->compileOffset($query, $query->unionOffset);
46
//        }
47
48
        return $aql;
49
    }
50
51
    /**
52
     * Compile a single union statement.
53
     *
54
     * @param  array  $union
55
     * @return string
56
     */
57
    protected function compileUnion(array $union, string $aql = '')
58
    {
59
        $unionType = $union['all'] ? 'UNION' : 'UNION_DISTINCT';
60
61
        return $unionType.'('.$aql.', '.$this->wrapUnion($union['query']->toSql()).')';
62
    }
63
64
    /**
65
     * Compile the "union" queries attached to the main query.
66
     *
67
     * @param IlluminateBuilder $query
68
     * @param string $aql
69
     * @return string
70
     */
71
    protected function compileUnionsX(IlluminateBuilder $query, $aql = '')
72
    {
73
        $unionType = $query->unions[0]['all'] ? 'UNION' : 'UNION_DISTINCT';
0 ignored issues
show
Unused Code introduced by
The assignment to $unionType is dead and can be removed.
Loading history...
74
75
        $unions = [];
76
77
78
        foreach ($query->unions as $union) {
79
            ray('compileUnions', $union['query']);
80
            $unions[] = $this->wrapUnion($union['query']->toSql());
81
        }
82
        $unions[] = $this->wrapUnion($aql);
83
84
85
//        if (!empty($query->unionOrders)) {
86
//            $sql .= ' ' . $this->compileOrders($query, $query->unionOrders);
87
//        }
88
//
89
//        if (isset($query->unionLimit)) {
90
//            $sql .= ' ' . $this->compileLimit($query, $query->unionLimit);
91
//        }
92
//
93
//        if (isset($query->unionOffset)) {
94
//            $sql .= ' ' . $this->compileOffset($query, $query->unionOffset);
95
//        }
96
//
97
//        return ltrim($sql);
98
        return $aql;
99
    }
100
}