Completed
Pull Request — master (#124)
by Enrico
03:58
created

PostDec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 53.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
ccs 15
cts 28
cp 0.5356
rs 10
wmc 7
lcom 0
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
C compile() 0 37 7
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 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 PostDec extends AbstractExpressionCompiler
17
{
18
    protected $name = 'PhpParser\Node\Expr\PostDec';
19
20
    /**
21
     * {expr}--
22
     *
23
     * @param \PhpParser\Node\Expr\PostDec $expr
24
     * @param Context $context
25
     * @return CompiledExpression
26
     */
27 8
    protected function compile($expr, Context $context)
28
    {
29 8
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
30 8
            $variableName = $expr->var->name;
31 8
            if ($variableName instanceof Name) {
32 8
                $variableName = $variableName->parts[0];
33 8
            }
34
35 8
            $variable = $context->getSymbol($variableName);
36 8
            if ($variable) {
37 8
                $variable->incUse();
38
39 8
                switch ($variable->getType()) {
40 8
                    case CompiledExpression::INTEGER:
41 8
                    case CompiledExpression::DOUBLE:
42 8
                    case CompiledExpression::NUMBER:
43 8
                        $variable->dec();
44 8
                        return CompiledExpression::fromZvalValue($variable->getValue());
45
                }
46
47
                $context->notice(
48
                    'postdec.variable.wrong-type',
49
                    'You are trying to use post decrement operator on variable $' . $variableName .
50
                    ' with type: ' . $variable->getTypeName(),
51
                    $expr
52
                );
53
            } else {
54
                $context->notice(
55
                    'postdec.undefined-variable',
56
                    'You are trying to use post decrement operator on undefined variable: ' . $variableName,
57
                    $expr
58
                );
59
            }
60
        }
61
62
        return new CompiledExpression();
63
    }
64
}
65