Div::compile()   B
last analyzed

Complexity

Conditions 8
Paths 14

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 14
nop 2
dl 0
loc 29
ccs 0
cts 19
cp 0
crap 72
rs 8.2114
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 Div extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\BinaryOp\Div';
18
19
    /**
20
     * {expr} / {expr}
21
     *
22
     * @param \PhpParser\Node\Expr\BinaryOp\Div $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26
    protected function compile($expr, Context $context)
27
    {
28
        $left = $context->getExpressionCompiler()->compile($expr->left);
29
        $right = $context->getExpressionCompiler()->compile($expr->right);
30
31
        if ($right->isEquals(0)) {
32
            $context->notice(
33
                'language_error',
34
                'You are trying to divide by 0.',
35
                $expr
36
            );
37
38
            return new CompiledExpression();
39
        }
40
41
        switch ($left->getType()) {
42
            case CompiledExpression::INTEGER:
43
            case CompiledExpression::DOUBLE:
44
            case CompiledExpression::BOOLEAN:
45
                switch ($right->getType()) {
46
                    case CompiledExpression::INTEGER:
47
                    case CompiledExpression::DOUBLE:
48
                    case CompiledExpression::BOOLEAN:
49
                        return CompiledExpression::fromZvalValue($left->getValue() / $right->getValue());
50
                }
51
        }
52
53
        return new CompiledExpression();
54
    }
55
}
56