Completed
Pull Request — master (#124)
by Enrico
04:04
created

Mod::compile()   C

Complexity

Conditions 9
Paths 28

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 9.0027

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 28
nop 2
dl 0
loc 48
ccs 30
cts 31
cp 0.9677
crap 9.0027
rs 5.5102
c 0
b 0
f 0
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\Operators\Arithmetical;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Compiler\Expression;
13
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
14
15
class Mod extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\BinaryOp\Mod';
18
19
    /**
20
     * {expr} % {expr}
21
     *
22
     * @param \PhpParser\Node\Expr\BinaryOp\Mod $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 35
    protected function compile($expr, Context $context)
27
    {
28 35
        $left = $context->getExpressionCompiler()->compile($expr->left);
29 35
        $right = $context->getExpressionCompiler()->compile($expr->right);
30
31 35
        if ($left->isEquals(0)) {
32 3
            $context->notice(
33 3
                'division-zero',
34 3
                'You are trying to divide from 0: ' . $left->getValue() . '%{expr}',
35
                $expr
36 3
            );
37 3
        }
38
39 35
        if ($right->isEquals(0)) {
40 1
            $context->notice(
41 1
                'division-zero',
42 1
                'You are trying to divide by 0: {expr}%' . $right->getValue(),
43
                $expr
44 1
            );
45
46 1
            return new CompiledExpression();
47
        }
48
49 34
        switch ($left->getType()) {
50 34
            case CompiledExpression::INTEGER:
51 34
            case CompiledExpression::DOUBLE:
52 34
            case CompiledExpression::BOOLEAN:
53 33
                switch ($right->getType()) {
54 33
                    case CompiledExpression::BOOLEAN:
55
                        /**
56
                         * Boolean is true since isEquals(0) check did not pass before
57
                         * {expr}/1 = {expr}
58
                         */
59
60 3
                        $context->notice(
61 3
                            'division-by-true',
62 3
                            'You are trying to divide by true: {expr}%true ~ {expr}%1 = {expr}',
63
                            $expr
64 3
                        );
65
                        //no break
66 33
                    case CompiledExpression::INTEGER:
67 33
                    case CompiledExpression::DOUBLE:
68 33
                        return CompiledExpression::fromZvalValue($left->getValue() % $right->getValue());
69
                }
70 1
        }
71
72 1
        return new CompiledExpression();
73
    }
74
}
75