Div::compile()   B
last analyzed

Complexity

Conditions 10
Paths 22

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 22
nop 2
dl 0
loc 33
ccs 0
cts 22
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PHPSA\Compiler\Expression\AssignOp;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Compiler\Expression;
7
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
8
use PHPSA\Context;
9
10
class Div extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\AssignOp\Div';
13
14
    /**
15
     * {left-expr} /= {right-expr}
16
     *
17
     * @param \PhpParser\Node\Expr\AssignOp\Div $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21
    protected function compile($expr, Context $context)
22
    {
23
        $left = $context->getExpressionCompiler()->compile($expr->var);
24
        $expExpression = $context->getExpressionCompiler()->compile($expr->expr);
25
26
        if ($expExpression->isEquals(0)) {
27
            $context->notice(
28
                'language_error',
29
                'You are trying to divide by 0.',
30
                $expr
31
            );
32
33
            return new CompiledExpression();
34
        }
35
36
        switch ($left->getType()) {
37
            case CompiledExpression::INTEGER:
38
            case CompiledExpression::DOUBLE:
39
            case CompiledExpression::NUMBER:
40
            case CompiledExpression::BOOLEAN:
41
                switch ($expExpression->getType()) {
42
                    case CompiledExpression::INTEGER:
43
                    case CompiledExpression::DOUBLE:
44
                    case CompiledExpression::NUMBER:
45
                    case CompiledExpression::BOOLEAN:
46
                        return CompiledExpression::fromZvalValue(
47
                            $left->getValue() / $expExpression->getValue()
48
                        );
49
                }
50
        }
51
        
52
        return new CompiledExpression();
53
    }
54
}
55