|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Static Analysis project 2015 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PHPSA\Compiler\Expression\Operators; |
|
9
|
|
|
|
|
10
|
|
|
use PhpParser\Node\Name; |
|
11
|
|
|
use PHPSA\CompiledExpression; |
|
12
|
|
|
use PHPSA\Compiler\Types; |
|
13
|
|
|
use PHPSA\Context; |
|
14
|
|
|
use PHPSA\Compiler\Expression; |
|
15
|
|
|
use PHPSA\Compiler\Expression\AbstractExpressionCompiler; |
|
16
|
|
|
|
|
17
|
|
|
class PostDec extends AbstractExpressionCompiler |
|
18
|
|
|
{ |
|
19
|
|
|
protected $name = 'PhpParser\Node\Expr\PostDec'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {expr}++ |
|
23
|
|
|
* |
|
24
|
|
|
* @param \PhpParser\Node\Expr\PostDec $expr |
|
25
|
|
|
* @param Context $context |
|
26
|
|
|
* @return CompiledExpression |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function compile($expr, Context $context) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($expr->var instanceof \PHPParser\Node\Expr\Variable) { |
|
31
|
|
|
$variableName = $expr->var->name; |
|
32
|
|
|
if ($variableName instanceof Name) { |
|
33
|
|
|
$variableName = $variableName->parts[0]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$variable = $context->getSymbol($variableName); |
|
37
|
|
|
if ($variable) { |
|
38
|
|
|
$variable->incUse(); |
|
39
|
|
|
|
|
40
|
|
|
switch ($variable->getType()) { |
|
41
|
|
|
case CompiledExpression::INTEGER: |
|
42
|
|
|
case CompiledExpression::DOUBLE: |
|
43
|
|
|
case CompiledExpression::NUMBER: |
|
44
|
|
|
$variable->dec(); |
|
45
|
|
|
return CompiledExpression::fromZvalValue($variable->getValue()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$context->notice( |
|
49
|
|
|
'postdec.variable.wrong-type', |
|
50
|
|
|
'You are trying to use post derement operator on variable $' . $variableName . |
|
51
|
|
|
' with type: ' . $variable->getTypeName(), |
|
52
|
|
|
$expr |
|
53
|
|
|
); |
|
54
|
|
|
} else { |
|
55
|
|
|
$context->notice( |
|
56
|
|
|
'postdec.undefined-variable', |
|
57
|
|
|
'You are trying to use post derement operator on undefined variable: ' . $variableName, |
|
58
|
|
|
$expr |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$expression = new Expression($context); |
|
66
|
|
|
$compiledExpression = $expression->compile($expr->var); |
|
67
|
|
|
|
|
68
|
|
|
switch ($compiledExpression->getType()) { |
|
69
|
|
|
case CompiledExpression::INTEGER: |
|
70
|
|
|
case CompiledExpression::DOUBLE: |
|
71
|
|
|
case CompiledExpression::NUMBER: |
|
72
|
|
|
$value = $compiledExpression->getValue(); |
|
73
|
|
|
return CompiledExpression::fromZvalValue($value++); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|