ArithmeticExpression::normalizeCalculation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Expressions;
6
7
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
8
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
9
10
class ArithmeticExpression extends Expression implements ExpressionInterface
11
{
12
    /**
13
     * @var array<int|string|float|null|Expression|QueryBuilder>
14
     */
15
    protected array $calculation = [];
16
17
    /**
18
     * Create predicate expression.
19
     *
20
     * @param int|float|null|Expression|QueryBuilder $leftOperand
21
     * @param string $operator
22
     * @param int|float|null|Expression|QueryBuilder $rightOperand
23
     */
24 3
    public function __construct(
25
        int|float|null|Expression|QueryBuilder $leftOperand,
26
        string $operator,
27
        int|float|null|Expression|QueryBuilder $rightOperand
28
    ) {
29 3
        $this->calculation = [$leftOperand, $operator, $rightOperand];
30
    }
31
32
    /**
33
     * Compile calculation.
34
     *
35
     * @param  QueryBuilder  $queryBuilder
36
     * @return string
37
     * @throws \Exception
38
     */
39 3
    public function compile(QueryBuilder $queryBuilder): string
40
    {
41
        /** @var Expression[] $normalizedCalculation */
42 3
        $normalizedCalculation = $this->normalizeCalculation($queryBuilder, $this->calculation);
43
44 3
        $leftOperand = $normalizedCalculation['leftOperand']->compile($queryBuilder);
45 3
        if ($normalizedCalculation['leftOperand'] instanceof ArithmeticExpression) {
46 1
            $leftOperand = '(' . $leftOperand . ')';
47
        }
48
49 3
        $arithmeticOperator = $normalizedCalculation['arithmeticOperator']->compile($queryBuilder);
50
51 3
        $rightOperand = $normalizedCalculation['rightOperand']->compile($queryBuilder);
52 3
        if ($normalizedCalculation['rightOperand'] instanceof ArithmeticExpression) {
53 2
            $rightOperand = '(' . $rightOperand . ')';
54
        }
55
56 3
        return $leftOperand . ' ' . $arithmeticOperator . ' ' . $rightOperand;
57
    }
58
59
    /**
60
     * @param  QueryBuilder  $queryBuilder
61
     * @param  array<int|float|string|null|Expression|QueryBuilder>  $calculation
62
     * @return array<mixed>
63
     * @throws ExpressionTypeException
64
     */
65 3
    public function normalizeCalculation(
66
        QueryBuilder $queryBuilder,
67
        array $calculation
68
    ): array {
69 3
        $normalizedCalculation = [];
70
71 3
        $leftOperand = $queryBuilder->normalizeArgument($calculation[0]);
72
73 3
        $arithmeticOperator = '+';
74
        /** @var string $calculation[1] */
75 3
        if ($queryBuilder->grammar->isArithmeticOperator($calculation[1])) {
0 ignored issues
show
Bug introduced by
It seems like $calculation[1] can also be of type LaravelFreelancerNL\Flue...\Expressions\Expression and null; however, parameter $operator of LaravelFreelancerNL\Flue...:isArithmeticOperator() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        if ($queryBuilder->grammar->isArithmeticOperator(/** @scrutinizer ignore-type */ $calculation[1])) {
Loading history...
76 3
            $arithmeticOperator = new LiteralExpression($calculation[1]);
77
        }
78
79 3
        $rightOperand = $queryBuilder->normalizeArgument($calculation[2]);
80
81 3
        $normalizedCalculation['leftOperand'] = $leftOperand;
82 3
        $normalizedCalculation['arithmeticOperator'] = $arithmeticOperator;
83 3
        $normalizedCalculation['rightOperand'] = $rightOperand;
84
85 3
        return $normalizedCalculation;
86
    }
87
}
88