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

Div::compile()   C

Complexity

Conditions 10
Paths 34

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.003

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 34
nop 2
dl 0
loc 49
ccs 31
cts 32
cp 0.9688
crap 10.003
rs 5.5471
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\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 Div extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\BinaryOp\Div';
18
19
    /**
20
     * {expr} / {expr}
21
     *
22
     * @param \PhpParser\Node\Expr\BinaryOp\Div $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 37
    protected function compile($expr, Context $context)
27
    {
28 37
        $left = $context->getExpressionCompiler()->compile($expr->left);
29 37
        $right = $context->getExpressionCompiler()->compile($expr->right);
30
31 37
        if ($left->isEquals(0)) {
32 5
            $context->notice(
33 5
                'division-zero',
34 5
                sprintf('You are trying to divide from 0: %s/{expr}', $left->getValue()),
35
                $expr
36 5
            );
37 5
        }
38
39 37
        if ($right->isEquals(0)) {
40 1
            $context->notice(
41 1
                'division-zero',
42 1
                sprintf('You are trying to divide by 0: {expr}/%s', $right->getValue()),
43
                $expr
44 1
            );
45
46 1
            return new CompiledExpression();
47
        }
48
49 36
        switch ($left->getType()) {
50 36
            case CompiledExpression::INTEGER:
51 36
            case CompiledExpression::DOUBLE:
52 36
            case CompiledExpression::BOOLEAN:
53 35
                switch ($right->getType()) {
54 35
                    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 35
                    case CompiledExpression::INTEGER:
67 35
                    case CompiledExpression::DOUBLE:
68 35
                    case CompiledExpression::BOOLEAN:
69 35
                        return CompiledExpression::fromZvalValue($left->getValue() / $right->getValue());
70
                }
71 1
        }
72
73 1
        return new CompiledExpression();
74
    }
75
}
76