Completed
Pull Request — master (#124)
by Enrico
04:04
created

Plus::compile()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0061

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 7
nop 2
dl 0
loc 34
ccs 19
cts 20
cp 0.95
crap 7.0061
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression\Operators\Arithmetical;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Compiler\Expression;
13
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
14
15
class Plus extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\BinaryOp\Plus';
18
19
    /**
20
     * {expr} + {expr}
21
     *
22
     * @param \PhpParser\Node\Expr\BinaryOp\Plus $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 45
    protected function compile($expr, Context $context)
27
    {
28 45
        $left = $context->getExpressionCompiler()->compile($expr->left);
29 45
        $right = $context->getExpressionCompiler()->compile($expr->right);
30
31 45
        switch ($left->getType()) {
32 45
            case CompiledExpression::INTEGER:
33 26
                switch ($right->getType()) {
34 26
                    case CompiledExpression::INTEGER:
35
                        /**
36
                         * php -r "var_dump(1 + 1);" int(2)
37
                         */
38 16
                        return new CompiledExpression(CompiledExpression::INTEGER, $left->getValue() + $right->getValue());
39 10
                    case CompiledExpression::DOUBLE:
40
                        /**
41
                         * php -r "var_dump(1 + 1.0);" double(2)
42
                         */
43 9
                        return new CompiledExpression(CompiledExpression::DOUBLE, $left->getValue() + $right->getValue());
44 1
                }
45 1
                break;
46 19
            case CompiledExpression::DOUBLE:
47 18
                switch ($right->getType()) {
48 18
                    case CompiledExpression::INTEGER:
49 18
                    case CompiledExpression::DOUBLE:
50
                        /**
51
                         * php -r "var_dump(1.0 + 1);"   double(2)
52
                         * php -r "var_dump(1.0 + 1.0);" double(2)
53
                         */
54 18
                        return new CompiledExpression(CompiledExpression::DOUBLE, $left->getValue() + $right->getValue());
55
                }
56 2
        }
57
58 2
        return new CompiledExpression();
59
    }
60
}
61