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

PreDec   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 2
Bugs 0 Features 0
Metric Value
c 2
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
namespace PHPSA\Compiler\Expression\Operators;
4
5
use PhpParser\Node\Name;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Context;
8
use PHPSA\Compiler\Expression;
9
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
10
11
class PreDec extends AbstractExpressionCompiler
12
{
13
    protected $name = 'PhpParser\Node\Expr\PreDec';
14
15
    /**
16
     * --{expr}
17
     *
18
     * @param \PhpParser\Node\Expr\PreDec $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22 8
    protected function compile($expr, Context $context)
23
    {
24 8
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
25 8
            $variableName = $expr->var->name;
26 8
            if ($variableName instanceof Name) {
27 8
                $variableName = $variableName->parts[0];
28 8
            }
29
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
                    'predec.variable.wrong-type',
44
                    'You are trying to use pre decrement operator on variable $' . $variableName .
45
                    ' with type: ' . $variable->getTypeName(),
46
                    $expr
47
                );
48
            } else {
49
                $context->notice(
50
                    'predec.undefined-variable',
51
                    'You are trying to use pre decrement operator on undefined variable: ' . $variableName,
52
                    $expr
53
                );
54
            }
55
        }
56
57
        return new CompiledExpression();
58
    }
59
}
60