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

PostInc   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 10
c 6
b 1
f 1
lcom 0
cbo 7
dl 0
loc 62
ccs 0
cts 37
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C compile() 0 50 10
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