Completed
Push — master ( 558809...774145 )
by Дмитрий
03:36
created

PreDec   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 61
ccs 15
cts 36
cp 0.4167
rs 10
wmc 10
lcom 0
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C compile() 0 49 10
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
            return new CompiledExpression(CompiledExpression::UNKNOWN);
57
        }
58
59
        $compiledExpression = $context->getExpressionCompiler()->compile($expr->var);
60
61
        switch ($compiledExpression->getType()) {
62
            case CompiledExpression::INTEGER:
63
            case CompiledExpression::DOUBLE:
64
            case CompiledExpression::NUMBER:
65
                $value = $compiledExpression->getValue();
66
                return CompiledExpression::fromZvalValue(--$value);
67
        }
68
69
        return new CompiledExpression(CompiledExpression::UNKNOWN);
70
    }
71
}
72