Completed
Push — master ( 40ee83...aecd2c )
by Дмитрий
07:12 queued 02:11
created

PostInc::compile()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 50
ccs 0
cts 37
cp 0
rs 5.7647
cc 10
eloc 34
nc 14
nop 2
crap 110

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