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

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