Passed
Push — next ( ee2197...d54041 )
by Bas
02:37
created

ForClause   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
c 3
b 0
f 0
dl 0
loc 53
ccs 22
cts 23
cp 0.9565
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
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;
0 ignored issues
show
Bug introduced by
The type LaravelFreelancerNL\FluentAQL\Clauses\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
    /**
24
     * @param array<string|Expression>|string|Expression $variables
25
     * @param array<mixed>|string|QueryBuilder|Expression|null $in
26
     */
27 22
    public function __construct(
28
        array|string|Expression $variables,
29
        array|string|QueryBuilder|Expression $in = null
30
    ) {
31 22
        parent::__construct();
32
33 22
        if (!is_array($variables)) {
0 ignored issues
show
introduced by
The condition is_array($variables) is always true.
Loading history...
34
            $variables = [$variables];
35
        }
36 22
        $this->variables = $variables;
37
38 22
        $this->in = $in;
39 22
    }
40
41 22
    public function compile(QueryBuilder $queryBuilder): string
42
    {
43 22
        $variables = [];
44 22
        foreach ($this->variables as $key => $value) {
45 22
            $variables[$key] = $queryBuilder->normalizeArgument($value, 'Variable');
46 22
            $queryBuilder->registerVariable($variables[$key]->compile($queryBuilder));
47
        }
48 22
        $variableExpression =  array_map(function ($variable) use ($queryBuilder) {
49 22
            return $variable->compile($queryBuilder);
50 22
        }, $variables);
51
52 22
        $variableExpression = implode(', ', $variableExpression);
53
54 22
        $inExpression = '';
55 22
        if ($this->in !== null) {
56 22
            $this->in = $queryBuilder->normalizeArgument(
57 22
                $this->in,
58 22
                ['Collection', 'Range', 'List', 'Reference', 'Query', 'CollectionBind']
59
            );
60 22
            $inExpression = $this->in->compile($queryBuilder);
61
        }
62
63 22
        return "FOR {$variableExpression} IN {$inExpression}";
64
    }
65
}
66