Passed
Branch next (ee2197)
by Bas
02:37
created

ArithmeticExpression::compile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 10
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 PredicateExpression implements ExpressionInterface
11
{
12
13
    /**
14
     * @var array<mixed>
15
     */
16
    protected array $calculation = [];
17
18
    /**
19
     * Create predicate expression.
20
     *
21
     * @param mixed $leftOperand
22
     * @param string $operator
23
     * @param mixed $rightOperand
24
     */
25 3
    public function __construct(mixed $leftOperand, string $operator, mixed $rightOperand)
26
    {
27 3
        $this->calculation = [$leftOperand, $operator, $rightOperand];
28 3
    }
29
30
    /**
31
     * Compile calculation.
32
     *
33
     * @param  QueryBuilder|null  $queryBuilder
34
     * @return string
35
     * @throws \Exception
36
     */
37 3
    public function compile(QueryBuilder $queryBuilder = null): string
38
    {
39
        /** @phpstan-ignore-next-line */
40 3
        $normalizedCalculation = $this->normalizeCalculation($queryBuilder, $this->calculation);
0 ignored issues
show
Bug introduced by
It seems like $queryBuilder can also be of type null; however, parameter $queryBuilder of LaravelFreelancerNL\Flue...:normalizeCalculation() does only seem to accept LaravelFreelancerNL\FluentAQL\QueryBuilder, 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

40
        $normalizedCalculation = $this->normalizeCalculation(/** @scrutinizer ignore-type */ $queryBuilder, $this->calculation);
Loading history...
41
42 3
        $leftOperand = $normalizedCalculation['leftOperand']->compile($queryBuilder);
0 ignored issues
show
Bug introduced by
It seems like $queryBuilder can also be of type null; however, parameter $queryBuilder of LaravelFreelancerNL\Flue...QueryElement::compile() does only seem to accept LaravelFreelancerNL\FluentAQL\QueryBuilder, 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

42
        $leftOperand = $normalizedCalculation['leftOperand']->compile(/** @scrutinizer ignore-type */ $queryBuilder);
Loading history...
43 3
        if ($normalizedCalculation['leftOperand'] instanceof ArithmeticExpression) {
44 1
            $leftOperand = '(' . $leftOperand . ')';
45
        }
46
47 3
        $rightOperand = $normalizedCalculation['rightOperand']->compile($queryBuilder);
48 3
        if ($normalizedCalculation['rightOperand'] instanceof ArithmeticExpression) {
49 2
            $rightOperand = '(' . $rightOperand . ')';
50
        }
51
52 3
        return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand;
53
    }
54
55
    /**
56
     * @param  QueryBuilder  $queryBuilder
57
     * @param  array<mixed>  $calculation
58
     * @return array<mixed>
59
     * @throws ExpressionTypeException
60
     */
61 3
    public function normalizeCalculation(
62
        QueryBuilder $queryBuilder,
63
        array $calculation
64
    ): array {
65 3
        $normalizedCalculation = [];
66
67 3
        $leftOperand = $queryBuilder->normalizeArgument($calculation[0]);
68
69 3
        $arithmeticOperator = '+';
70 3
        if ($queryBuilder->grammar->isArithmeticOperator($calculation[1])) {
71 3
            $arithmeticOperator = $calculation[1];
72
        }
73
74 3
        $rightOperand = $queryBuilder->normalizeArgument($calculation[2]);
75
76 3
        $normalizedCalculation['leftOperand'] = $leftOperand;
77 3
        $normalizedCalculation['arithmeticOperator'] = $arithmeticOperator;
78 3
        $normalizedCalculation['rightOperand'] = $rightOperand;
79
80 3
        return $normalizedCalculation;
81
    }
82
}
83