Completed
Pull Request — master (#297)
by algo13
03:27
created

PreDec::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
namespace PHPSA\Compiler\Expression\Operators;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
10
class PreDec extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\PreDec';
13
14
    /**
15
     * --{expr}
16
     *
17
     * @param \PhpParser\Node\Expr\PreDec $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21 9
    protected function compile($expr, Context $context)
22
    {
23 9
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
24 9
            $variableName = (string)$expr->var->name;
25 9
            $variable = $context->getSymbol($variableName);
26 9
            if ($variable) {
27 9
                $variable->incUse();
28
29 9
                switch ($variable->getType()) {
30 9
                    case CompiledExpression::INTEGER:
31 9
                    case CompiledExpression::DOUBLE:
32 9
                    case CompiledExpression::NUMBER:
33 9
                        $variable->dec();
34 9
                        return CompiledExpression::fromZvalValue($variable->getValue());
35
                }
36
37
                $context->notice(
38
                    'language_error',
39
                    'You are trying to use pre decrement operator on variable $' . $variableName .
40
                    ' with type: ' . $variable->getTypeName(),
41
                    $expr
42
                );
43
            } else {
44
                $context->notice(
45
                    'language_error',
46
                    'You are trying to use pre decrement operator on undefined variable: ' . $variableName,
47
                    $expr
48
                );
49
            }
50
        }
51
52
        return new CompiledExpression();
53
    }
54
}
55