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

Mod::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
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression\AssignOp;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Compiler\Expression;
12
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
13
use PHPSA\Context;
14
15
class Mod extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\AssignOp\Mod';
18
19
    /**
20
     * {left-expr} %= {right-expr}
21
     *
22
     * @param \PhpParser\Node\Expr\AssignOp\Mod $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 11
    protected function compile($expr, Context $context)
27
    {
28 11
        $left = $context->getExpressionCompiler()->compile($expr->var);
29 11
        $expExpression = $context->getExpressionCompiler()->compile($expr->expr);
30
31 11
        if ($expExpression->isEquals(0)) {
32 11
            $context->notice(
33 11
                'language-error',
34 11
                'You are trying to divide by 0.',
35 11
                $expr
36 10
            );
37 10
38 10
            return new CompiledExpression();
39 10
        }
40 10
41 9
        switch ($left->getType()) {
42 9
            case CompiledExpression::INTEGER:
43 9
            case CompiledExpression::DOUBLE:
44 1
            case CompiledExpression::NUMBER:
45 2
            case CompiledExpression::BOOLEAN:
46
                switch ($expExpression->getType()) {
47 2
                    case CompiledExpression::INTEGER:
48
                    case CompiledExpression::DOUBLE:
49
                    case CompiledExpression::NUMBER:
50
                    case CompiledExpression::BOOLEAN:
51
                        return CompiledExpression::fromZvalValue(
52
                            $left->getValue() % $expExpression->getValue()
53
                        );
54
                }
55
        }
56
57
        return new CompiledExpression();
58
    }
59
}
60