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

PreInc   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 48%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 12
cts 25
cp 0.48
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 33 6
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 PreInc extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\PreInc';
13
14
    /**
15
     * ++{expr}
16
     *
17
     * @param \PhpParser\Node\Expr\PreInc $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21 10
    protected function compile($expr, Context $context)
22
    {
23 10
        if ($expr->var instanceof \PHPParser\Node\Expr\Variable) {
24 10
            $variableName = (string)$expr->var->name;
25 10
            $variable = $context->getSymbol($variableName);
26 10
            if ($variable) {
27 10
                $variable->incUse();
28
29 10
                switch ($variable->getType()) {
30 10
                    case CompiledExpression::INTEGER:
31 10
                    case CompiledExpression::DOUBLE:
32 10
                    case CompiledExpression::NUMBER:
33 10
                        $variable->inc();
34 10
                        return CompiledExpression::fromZvalValue($variable->getValue());
35
                }
36
37
                $context->notice(
38
                    'language_error',
39
                    'You are trying to use pre increment 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 increment operator on undefined variable: ' . $variableName,
47
                    $expr
48
                );
49
            }
50
        }
51
52
        return new CompiledExpression();
53
    }
54
}
55