Completed
Pull Request — master (#254)
by Enrico
14:10 queued 09:31
created

Div::compile()   D

Complexity

Conditions 10
Paths 22

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.0064

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 22
nop 2
dl 0
loc 33
ccs 24
cts 25
cp 0.96
crap 10.0064
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 20
    protected function compile($expr, Context $context)
22
    {
23 20
        $left = $context->getExpressionCompiler()->compile($expr->var);
24 20
        $expExpression = $context->getExpressionCompiler()->compile($expr->expr);
25
26 20
        if ($expExpression->isEquals(0)) {
27 1
            $context->notice(
28 1
                'language-error',
29 1
                'You are trying to divide by 0.',
30
                $expr
31 1
            );
32
33 1
            return new CompiledExpression();
34
        }
35
36 19
        switch ($left->getType()) {
37 19
            case CompiledExpression::INTEGER:
38 19
            case CompiledExpression::DOUBLE:
39 19
            case CompiledExpression::NUMBER:
40 19
            case CompiledExpression::BOOLEAN:
41 18
                switch ($expExpression->getType()) {
42 18
                    case CompiledExpression::INTEGER:
43 18
                    case CompiledExpression::DOUBLE:
44 18
                    case CompiledExpression::NUMBER:
45 18
                    case CompiledExpression::BOOLEAN:
46 18
                        return CompiledExpression::fromZvalValue(
47 18
                            $left->getValue() / $expExpression->getValue()
48 18
                        );
49
                }
50 1
        }
51
        
52 1
        return new CompiledExpression();
53
    }
54
}
55