CompilesGroups   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 83
ccs 36
cts 38
cp 0.9474
rs 10
c 1
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileGroups() 0 34 4
A extractGroupVariable() 0 3 1
A keepColumns() 0 26 6
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<mixed> $groups
16
     * @return string
17
     * @throws \Exception
18
     */
19 11
    protected function compileGroups(IlluminateQueryBuilder $query, $groups): string
20
    {
21
        assert($query instanceof Builder);
22
23 11
        $aql = "COLLECT ";
24
25 11
        $aqlGroups = [];
26 11
        foreach ($groups as $group) {
27 11
            if ($group instanceof Expression) {
28 1
                $groupVariable = $this->extractGroupVariable($group);
29
                ;
30 1
                $query->registerTableAlias($groupVariable, $groupVariable);
31
32 1
                $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 1
                continue;
34
            }
35
36 10
            $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
38 10
            $query->registerTableAlias($group, $group);
39 10
            $query->groupVariables[] = $group;
40
        }
41
42 11
        $aql .= implode(", ", $aqlGroups);
43
44 11
        $variablesToKeep = $this->keepColumns($query, $groups);
45
46 11
        if (!empty($variablesToKeep)) {
47 1
            $query->registerTableAlias('groupsVariable', 'groupsVariable');
48 1
            $query->groupVariables[] = 'groupsVariable';
49
50 1
            $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 11
        return $aql;
53
    }
54
55
    /**
56
     * @param IlluminateQueryBuilder $query
57
     * @param array<mixed> $groups
58
     * @return array<string>
59
     * @throws \Exception
60
     */
61 11
    protected function keepColumns(IlluminateQueryBuilder $query, $groups)
62
    {
63 11
        if (is_null($query->columns)) {
64
            return [];
65
        }
66 11
        $tempGroups = [];
67 11
        foreach ($groups as $group) {
68 11
            if ($group instanceof Expression) {
69 1
                $tempGroups[] = $this->extractGroupVariable($group);
70 1
                continue;
71
            }
72 10
            $tempGroups[] = $group;
73
        }
74
75 11
        $diff = array_diff_assoc($query->columns, $tempGroups);
76
77 11
        $results = [];
78 11
        foreach ($diff as $key => $value) {
79 1
            if (is_numeric($key)) {
80 1
                $results[$value] = $this->normalizeColumn($query, $value);
81 1
                continue;
82
            }
83
            $results[$key] = $this->normalizeColumn($query, $value);
84
        }
85
86 11
        return $results;
87
    }
88
89 1
    protected function extractGroupVariable(Expression $group): string
90
    {
91 1
        return explode(' = ', (string) $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

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