ForClause   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 50
ccs 19
cts 19
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A compile() 0 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Clauses;
6
7
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
8
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
9
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
10
11
class ForClause extends Clause
12
{
13
    /**
14
     * @var array<string|Expression>
15
     */
16
    protected array $variables;
17
18
    /**
19
     * @var array<mixed>|Expression|ExpressionInterface|QueryBuilder|string|null
20
     */
21
    protected array|Expression|ExpressionInterface|QueryBuilder|string|null $in;
22
23
    /**
24
     * @param array<string|Expression> $variables
25
     * @param array<mixed>|string|QueryBuilder|Expression|null $in
26
     */
27 24
    public function __construct(
28
        array $variables,
29
        array|string|QueryBuilder|Expression $in = null
30
    ) {
31 24
        parent::__construct();
32
33 24
        $this->variables = $variables;
34
35 24
        $this->in = $in;
36
    }
37
38 24
    public function compile(QueryBuilder $queryBuilder): string
39
    {
40 24
        $variables = [];
41 24
        foreach ($this->variables as $key => $value) {
42 24
            $variables[$key] = $queryBuilder->normalizeArgument($value, 'Variable');
43 24
            $queryBuilder->registerVariable($variables[$key]->compile($queryBuilder));
44
        }
45 24
        $variableExpression =  array_map(function ($variable) use ($queryBuilder) {
46 24
            return $variable->compile($queryBuilder);
47
        }, $variables);
48
49 24
        $variableExpression = implode(', ', $variableExpression);
50
51 24
        $inExpression = '';
52 24
        if ($this->in !== null) {
53 24
            $this->in = $queryBuilder->normalizeArgument(
54 24
                $this->in,
55 24
                ['Collection', 'Range', 'List', 'Reference', 'Query', 'CollectionBind']
56
            );
57 24
            $inExpression = $this->in->compile($queryBuilder);
58
        }
59
60 24
        return "FOR {$variableExpression} IN {$inExpression}";
61
    }
62
}
63