Completed
Pull Request — master (#254)
by Enrico
12:21
created

Div::compile()   D

Complexity

Conditions 10
Paths 22

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 22
nop 2
dl 0
loc 33
ccs 19
cts 19
cp 1
crap 10
rs 4.8196
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 18
    protected function compile($expr, Context $context)
22
    {
23 18
        $left = $context->getExpressionCompiler()->compile($expr->var);
24 18
        $expExpression = $context->getExpressionCompiler()->compile($expr->expr);
25
26 18
        if ($expExpression->isEquals(0)) {
27 18
            $context->notice(
28 18
                'language-error',
29 18
                'You are trying to divide by 0.',
30 18
                $expr
31 17
            );
32 17
33 17
            return new CompiledExpression();
34 17
        }
35 17
36 16
        switch ($left->getType()) {
37 16
            case CompiledExpression::INTEGER:
38 16
            case CompiledExpression::DOUBLE:
39 1
            case CompiledExpression::NUMBER:
40 2
            case CompiledExpression::BOOLEAN:
41
                switch ($expExpression->getType()) {
42 2
                    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