Completed
Push — master ( 361c7d...1d8c7b )
by Enrico
16s
created

PostDec::compile()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.0618

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 6
nop 2
dl 0
loc 33
ccs 12
cts 25
cp 0.48
crap 11.0618
rs 8.439
c 0
b 0
f 0
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;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Compiler\Expression;
13
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
14
15
class PostDec extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\PostDec';
18
19
    /**
20
     * {expr}--
21
     *
22
     * @param \PhpParser\Node\Expr\PostDec $expr
23
     * @param Context $context
24
     * @return CompiledExpression
25
     */
26 8
    protected function compile($expr, Context $context)
27
    {
28 8
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
29 8
            $variableName = (string)$expr->var->name;
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
                    'language_error',
44
                    'You are trying to use post decrement operator on variable $' . $variableName .
45
                    ' with type: ' . $variable->getTypeName(),
46
                    $expr
47
                );
48
            } else {
49
                $context->notice(
50
                    'language_error',
51
                    'You are trying to use post decrement operator on undefined variable: ' . $variableName,
52
                    $expr
53
                );
54
            }
55
        }
56
57
        return new CompiledExpression();
58
    }
59
}
60