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

PreDec::compile()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.9044

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 2
b 0
f 0
nc 11
nop 2
dl 0
loc 37
ccs 15
cts 28
cp 0.5356
crap 11.9044
rs 6.7272
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