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

Mod::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
 * 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 13
    protected function compile($expr, Context $context)
27
    {
28 13
        $left = $context->getExpressionCompiler()->compile($expr->var);
29 13
        $expExpression = $context->getExpressionCompiler()->compile($expr->expr);
30
31 13
        if ($expExpression->isEquals(0)) {
32 1
            $context->notice(
33 1
                'language-error',
34 1
                'You are trying to divide by 0.',
35
                $expr
36 1
            );
37
38 1
            return new CompiledExpression();
39
        }
40
41 12
        switch ($left->getType()) {
42 12
            case CompiledExpression::INTEGER:
43 12
            case CompiledExpression::DOUBLE:
44 12
            case CompiledExpression::NUMBER:
45 12
            case CompiledExpression::BOOLEAN:
46 11
                switch ($expExpression->getType()) {
47 11
                    case CompiledExpression::INTEGER:
48 11
                    case CompiledExpression::DOUBLE:
49 11
                    case CompiledExpression::NUMBER:
50 11
                    case CompiledExpression::BOOLEAN:
51 11
                        return CompiledExpression::fromZvalValue(
52 11
                            $left->getValue() % $expExpression->getValue()
53 11
                        );
54
                }
55 1
        }
56
57 1
        return new CompiledExpression();
58
    }
59
}
60