CollectClause::compile()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 6
nop 1
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 4
rs 9.7
c 3
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Clauses;
6
7
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
8
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
9
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
10
11
class CollectClause extends Clause
12
{
13
    /**
14
     * @var array<array<string[]>>|array<mixed>
15
     */
16
    protected array $groups;
17
18
    /**
19
     * CollectClause constructor.
20
     * @param  array<array<string|null>> $groups
21
     */
22 2
    public function __construct(array $groups = [])
23
    {
24 2
        $this->groups = $groups;
25
    }
26
27
    /**
28
     * @throws ExpressionTypeException
29
     */
30 2
    public function compile(QueryBuilder $queryBuilder): string
31
    {
32
        /** @var array<array<Expression>>  $groups */
33 2
        $groups = [];
34
        /** @var array<int, string|null>  $group */
35 2
        foreach ($this->groups as $key => $group) {
36 2
            $groups[$key][0] = $queryBuilder->normalizeArgument(
37 2
                $group[0],
38
                'Variable'
39
            );
40 2
            $queryBuilder->registerVariable($groups[$key][0]);
41
42 2
            $groups[$key][1] = $queryBuilder->normalizeArgument(
43 2
                $group[1],
44 2
                ['Reference', 'Function', 'Query', 'Bind']
45
            );
46
        }
47
48 2
        $output = 'COLLECT';
49 2
        $groupOutput = '';
50 2
        foreach ($groups as $group) {
51 2
            if ($groupOutput !== '') {
52 1
                $groupOutput .= ',';
53
            }
54 2
            $groupOutput .= ' ' . $group[0]->compile($queryBuilder);
55
            /** @psalm-suppress PossiblyUndefinedArrayOffset */
56 2
            $groupOutput .=  ' = ' . $group[1]->compile($queryBuilder);
57
        }
58
59 2
        return $output . $groupOutput;
60
    }
61
}
62