Failed Conditions
Push — refactor/improve-static-analys... ( ba8000...c6edde )
by Bas
14:06
created

CompilesGroups   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 80
rs 10
ccs 9
cts 9
cp 1
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractGroupVariable() 0 3 1
A compileGroups() 0 34 4
A keepColumns() 0 23 5
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
6
use Illuminate\Database\Query\Expression;
7
use LaravelFreelancerNL\Aranguent\Query\Builder;
8
9
trait CompilesGroups
10
{
11
    /**
12
     * Compile the "group by" portions of the query.
13
     *
14
     * @param IlluminateQueryBuilder $query
15
     * @param array $groups
16 5
     * @return string
17
     * @throws \Exception
18 5
     */
19 5
    protected function compileGroups(IlluminateQueryBuilder $query, $groups): string
20 5
    {
21 5
        assert($query instanceof Builder);
22
23
        $aql = "COLLECT ";
24 5
25
        $aqlGroups = [];
26 5
        foreach ($groups as $group) {
27
            if ($group instanceof Expression) {
28
                $groupVariable = $this->extractGroupVariable($group);
29
                ;
30
                $query->registerTableAlias($groupVariable, $groupVariable);
31
32
                $aqlGroups[] = $group->getValue($this);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...Concerns\CompilesGroups is incompatible with the type Illuminate\Database\Grammar expected by parameter $grammar of Illuminate\Database\Query\Expression::getValue(). ( Ignorable by Annotation )

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

32
                $aqlGroups[] = $group->getValue(/** @scrutinizer ignore-type */ $this);
Loading history...
33
                continue;
34
            }
35 3
36
            $aqlGroups[] = $group . " = " . $this->normalizeColumn($query, $group);
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
            $aqlGroups[] = $group . " = " . $this->/** @scrutinizer ignore-call */ normalizeColumn($query, $group);
Loading history...
37 3
38
            $query->registerTableAlias($group, $group);
39
            $query->groupVariables[] = $group;
40
        }
41
42
        $aql .= implode(", ", $aqlGroups);
43
44
        $variablesToKeep = $this->keepColumns($query, $groups);
45
46
        if (!empty($variablesToKeep)) {
47
            $query->registerTableAlias('groupsVariable', 'groupsVariable');
48
            $query->groupVariables[] = 'groupsVariable';
49
50
            $aql .= ' INTO groupsVariable = ' . $this->generateAqlObject($variablesToKeep);
0 ignored issues
show
Bug introduced by
It seems like generateAqlObject() 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

50
            $aql .= ' INTO groupsVariable = ' . $this->/** @scrutinizer ignore-call */ generateAqlObject($variablesToKeep);
Loading history...
51
        }
52
        return $aql;
53
    }
54
55
    /**
56
     * @param IlluminateQueryBuilder $query
57
     * @param $groups
58
     * @return array
59
     * @throws \Exception
60
     */
61
    protected function keepColumns(IlluminateQueryBuilder $query, $groups)
62
    {
63
        $tempGroups = [];
64
        foreach($groups as $group) {
65
            if ($group instanceof Expression) {
66
                $tempGroups[] = $this->extractGroupVariable($group);
67
                continue;
68
            }
69
            $tempGroups[] = $group;
70
        }
71
72
        $diff = array_diff_assoc($query->columns, $tempGroups);
73
74
        $results = [];
75
        foreach ($diff as $key => $value) {
76
            if (is_numeric($key)) {
77
                $results[$value] = $this->normalizeColumn($query, $value);
78
                continue;
79
            }
80
            $results[$key] = $this->normalizeColumn($query, $value);
81
        }
82
83
        return $results;
84
    }
85
86
    protected function extractGroupVariable(Expression $group)
87
    {
88
        return explode(' = ', $group->getValue($this))[0];
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...Concerns\CompilesGroups is incompatible with the type Illuminate\Database\Grammar expected by parameter $grammar of Illuminate\Database\Query\Expression::getValue(). ( Ignorable by Annotation )

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

88
        return explode(' = ', $group->getValue(/** @scrutinizer ignore-type */ $this))[0];
Loading history...
89
    }
90
}
91