Passed
Push — next ( 64a7ed...91e38a )
by Bas
03:25 queued 12s
created

CompilesColumns::determineReturnValues()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 6
nop 3
crap 5
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 103
    protected function compileColumns(Builder $builder, array $columns): Builder
18
    {
19 103
        $returnDocs = [];
20 103
        $returnAttributes = [];
21 103
        $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 103
        foreach ($columns as $column) {
25
            // Extract rows
26 103
            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
            // Extract groups
34 103
            if (is_array($builder->groups) && in_array($column, $builder->groups)) {
35 5
                $returnAttributes[] = $column;
36
37 5
                continue;
38
            }
39
40 98
            if ($column != null && $column != '*') {
41 26
                [$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

41
                /** @scrutinizer ignore-call */ 
42
                [$column, $alias] = $this->extractAlias($column);
Loading history...
42
43 26
                $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

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