Passed
Pull Request — master (#13)
by Bas
03:44 queued 01:39
created

ArithmeticExpression::normalizeCalculation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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 18
ccs 0
cts 11
cp 0
crap 6
rs 9.9332
1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\Expressions;
4
5
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException;
6
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7
8
class ArithmeticExpression extends PredicateExpression implements ExpressionInterface
9
{
10
11
    protected $calculation = [];
12
13
    /**
14
     * Create predicate expression.
15
     *
16
     * @param string $leftOperand
17
     * @param string $rightOperand
18
     * @param string $operator
19
     */
20
    public function __construct($leftOperand, $operator, $rightOperand)
21
    {
22
        $this->calculation = [$leftOperand, $operator, $rightOperand];
23
    }
24
25
    /**
26
     * Compile calculation.
27
     *
28
     * @param  QueryBuilder|null  $queryBuilder
29
     * @return string
30
     * @throws \Exception
31
     */
32
    public function compile(QueryBuilder $queryBuilder = null): string
33
    {
34
        $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

34
        $normalizedCalculation = $this->normalizeCalculation(/** @scrutinizer ignore-type */ $queryBuilder, $this->calculation);
Loading history...
35
36
        $leftOperand = $normalizedCalculation['leftOperand']->compile($queryBuilder);
37
        if ($normalizedCalculation['leftOperand'] instanceof ArithmeticExpression) {
0 ignored issues
show
introduced by
$normalizedCalculation['leftOperand'] is never a sub-type of LaravelFreelancerNL\Flue...ns\ArithmeticExpression.
Loading history...
38
            $leftOperand = '(' . $leftOperand . ')';
39
        }
40
41
        $rightOperand = $normalizedCalculation['rightOperand']->compile($queryBuilder);
42
        if ($normalizedCalculation['rightOperand'] instanceof ArithmeticExpression) {
0 ignored issues
show
introduced by
$normalizedCalculation['rightOperand'] is never a sub-type of LaravelFreelancerNL\Flue...ns\ArithmeticExpression.
Loading history...
43
            $rightOperand = '(' . $rightOperand . ')';
44
        }
45
46
        return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand;
47
        return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand;
0 ignored issues
show
Unused Code introduced by
return $leftOperand . ' ...] . ' ' . $rightOperand is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
    }
49
50
    /**
51
     * @param  QueryBuilder  $queryBuilder
52
     * @param  array  $calculation
53
     * @return mixed
54
     * @throws ExpressionTypeException
55
     */
56
    public function normalizeCalculation(QueryBuilder $queryBuilder, array $calculation)
57
    {
58
        $normalizedCalculation = [];
59
60
        $leftOperand = $queryBuilder->normalizeArgument($calculation[0]);
61
62
        $arithmeticOperator = '+';
63
        if ($queryBuilder->grammar->isArithmeticOperator($calculation[1])) {
64
            $arithmeticOperator = $calculation[1];
65
        }
66
67
        $rightOperand = $queryBuilder->normalizeArgument($calculation[2]);
68
69
        $normalizedCalculation['leftOperand'] = $leftOperand;
70
        $normalizedCalculation['arithmeticOperator'] = $arithmeticOperator;
71
        $normalizedCalculation['rightOperand'] = $rightOperand;
72
73
        return $normalizedCalculation;
74
    }
75
}
76