Completed
Push — master ( 558809...774145 )
by Дмитрий
03:36
created

PreDec::compile()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 29.8461

Importance

Changes 0
Metric Value
cc 10
eloc 33
c 0
b 0
f 0
nc 14
nop 2
dl 0
loc 49
ccs 15
cts 36
cp 0.4167
crap 29.8461
rs 5.5471

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\Operators;
4
5
use PhpParser\Node\Name;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Context;
8
use PHPSA\Compiler\Expression;
9
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
10
11
class PreDec extends AbstractExpressionCompiler
12
{
13
    protected $name = 'PhpParser\Node\Expr\PreDec';
14
15
    /**
16
     * --{expr}
17
     *
18
     * @param \PhpParser\Node\Expr\PreDec $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22 8
    protected function compile($expr, Context $context)
23
    {
24 8
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
25 8
            $variableName = $expr->var->name;
26 8
            if ($variableName instanceof Name) {
27 8
                $variableName = $variableName->parts[0];
28 8
            }
29
30 8
            $variable = $context->getSymbol($variableName);
31 8
            if ($variable) {
32 8
                $variable->incUse();
33
34 8
                switch ($variable->getType()) {
35 8
                    case CompiledExpression::INTEGER:
36 8
                    case CompiledExpression::DOUBLE:
37 8
                    case CompiledExpression::NUMBER:
38 8
                        $variable->dec();
39 8
                        return CompiledExpression::fromZvalValue($variable->getValue());
40
                }
41
42
                $context->notice(
43
                    'predec.variable.wrong-type',
44
                    'You are trying to use pre decrement operator on variable $' . $variableName .
45
                    ' with type: ' . $variable->getTypeName(),
46
                    $expr
47
                );
48
            } else {
49
                $context->notice(
50
                    'predec.undefined-variable',
51
                    'You are trying to use pre decrement operator on undefined variable: ' . $variableName,
52
                    $expr
53
                );
54
            }
55
56
            return new CompiledExpression(CompiledExpression::UNKNOWN);
57
        }
58
59
        $compiledExpression = $context->getExpressionCompiler()->compile($expr->var);
60
61
        switch ($compiledExpression->getType()) {
62
            case CompiledExpression::INTEGER:
63
            case CompiledExpression::DOUBLE:
64
            case CompiledExpression::NUMBER:
65
                $value = $compiledExpression->getValue();
66
                return CompiledExpression::fromZvalValue(--$value);
67
        }
68
69
        return new CompiledExpression(CompiledExpression::UNKNOWN);
70
    }
71
}
72