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

CompilesColumns::mergeReturnDocs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 4
nop 4
crap 4
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use LaravelFreelancerNL\Aranguent\Query\Builder;
6
7
trait CompilesColumns
8
{
9
    /**
10
     * Compile the "RETURN" portion of the query.
11
     *
12
     * @param Builder $builder
13
     * @param array   $columns
14
     *
15
     * @return Builder
16
     */
17 92
    protected function compileColumns(Builder $builder, array $columns): Builder
18
    {
19 92
        $returnDocs = [];
20 92
        $returnAttributes = [];
21 92
        $values = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $values is dead and can be removed.
Loading history...
22
23
        // Prepare columns
24 92
        foreach ($columns as $column) {
25
            // Extract rows
26 92
            if (substr($column, strlen($column) - 2)  === '.*') {
27 12
                $table = substr($column, 0, strlen($column) - 2);
28 12
                $returnDocs[] = $this->getTableAlias($table);
0 ignored issues
show
Bug introduced by
It seems like getTableAlias() 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

28
                /** @scrutinizer ignore-call */ 
29
                $returnDocs[] = $this->getTableAlias($table);
Loading history...
29
30 12
                continue;
31
            }
32
33 92
            if ($column != null && $column != '*') {
34 25
                [$column, $alias] = $this->extractAlias($column);
0 ignored issues
show
Bug introduced by
It seems like extractAlias() 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

34
                /** @scrutinizer ignore-call */ 
35
                [$column, $alias] = $this->extractAlias($column);
Loading history...
35
36 25
                $returnAttributes[$alias] = $this->normalizeColumn($builder, $column);
0 ignored issues
show
Bug introduced by
It seems like normalizeColumn() 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

36
                /** @scrutinizer ignore-call */ 
37
                $returnAttributes[$alias] = $this->normalizeColumn($builder, $column);
Loading history...
37
            }
38
        }
39 92
        $values = $this->determineReturnValues($builder, $returnAttributes, $returnDocs);
40 92
        $builder->aqb = $builder->aqb->return($values, (bool) $builder->distinct);
41
42 92
        return $builder;
43
    }
44
45 92
    protected function determineReturnValues($builder, $returnAttributes = [], $returnDocs = [])
46
    {
47 92
        $values = $this->mergeReturnAttributes($returnAttributes, $returnDocs);
48
49 92
        $values = $this->mergeReturnDocs($values, $builder, $returnAttributes, $returnDocs);
50
51 92
        if ($builder->aggregate !== null) {
52 7
            $values = ['aggregate' => 'aggregateResult'];
53
        }
54
55 92
        if (empty($values)) {
56 85
            $values = $this->getTableAlias($builder->from);
57 85
            if (is_array($builder->joins) && !empty($builder->joins)) {
58 4
                $values = $this->mergeJoinResults($builder, $values);
59
            }
60
        }
61
62 92
        return $values;
63
    }
64
65 92
    protected function mergeReturnAttributes($returnAttributes, $returnDocs)
66
    {
67 92
        $values = [];
68 92
        if (! empty($returnAttributes)) {
69 25
            $values = $returnAttributes;
70
        }
71
72
        // If there is just one attribute/column given we assume that you want a list of values
73
        //  instead of a list of objects
74 92
        if (count($returnAttributes) == 1 && empty($returnDocs)) {
75 12
            $values = reset($returnAttributes);
76
        }
77
78 92
        return $values;
79
    }
80
81 92
    protected function mergeReturnDocs($values, $builder, $returnAttributes, $returnDocs)
82
    {
83 92
        if (! empty($returnAttributes) && ! empty($returnDocs)) {
84 12
            $returnDocs[] = $returnAttributes;
85
        }
86
87 92
        if (! empty($returnDocs)) {
88 12
            $values = $builder->aqb->merge(...$returnDocs);
89
        }
90 92
        return $values;
91
    }
92
93
94 4
    protected function mergeJoinResults($builder, $baseTable)
95
    {
96 4
        $tablesToJoin = [];
97 4
        foreach ($builder->joins as $join) {
98 4
            $tablesToJoin[] = $this->getTableAlias($join->table);
99
        }
100 4
        $tablesToJoin = array_reverse($tablesToJoin);
101 4
        $tablesToJoin[] = $baseTable;
102
103 4
        return $builder->aqb->merge(...$tablesToJoin);
104
    }
105
}
106