1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSA\Compiler\Expression; |
4
|
|
|
|
5
|
|
|
use PHPSA\CompiledExpression; |
6
|
|
|
use PHPSA\Context; |
7
|
|
|
use PHPSA\Compiler\Expression; |
8
|
|
|
use PHPSA\Compiler\Expression\AbstractExpressionCompiler; |
9
|
|
|
use PhpParser\Node\Expr\Variable as VariableNode; |
10
|
|
|
|
11
|
|
|
class AssignRef extends AbstractExpressionCompiler |
12
|
|
|
{ |
13
|
|
|
protected $name = 'PhpParser\Node\Expr\AssignRef'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* $a &= $b; |
17
|
|
|
* |
18
|
|
|
* @param \PhpParser\Node\Expr\AssignRef $expr |
19
|
|
|
* @param Context $context |
20
|
|
|
* @return CompiledExpression |
21
|
|
|
*/ |
22
|
1 |
|
protected function compile($expr, Context $context) |
23
|
|
|
{ |
24
|
1 |
|
$compiler = $context->getExpressionCompiler(); |
25
|
1 |
|
if ($expr->var instanceof VariableNode) { |
26
|
1 |
|
$name = $expr->var->name; |
27
|
|
|
|
28
|
1 |
|
$compiledExpression = $compiler->compile($expr->expr); |
29
|
|
|
|
30
|
1 |
|
$symbol = $context->getSymbol($name); |
31
|
1 |
|
if ($symbol) { |
32
|
|
|
$symbol->modify($compiledExpression->getType(), $compiledExpression->getValue()); |
33
|
|
|
} else { |
34
|
1 |
|
$symbol = new \PHPSA\Variable( |
35
|
1 |
|
$name, |
|
|
|
|
36
|
1 |
|
$compiledExpression->getValue(), |
37
|
1 |
|
$compiledExpression->getType(), |
38
|
1 |
|
$expr->expr, |
39
|
1 |
|
$context->getCurrentBranch() |
40
|
1 |
|
); |
41
|
1 |
|
$context->addVariable($symbol); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
if ($expr->expr instanceof VariableNode) { |
45
|
1 |
|
$rightVarName = $expr->expr->name; |
46
|
|
|
|
47
|
1 |
|
$rightSymbol = $context->getSymbol($rightVarName); |
48
|
1 |
|
if ($rightSymbol) { |
49
|
1 |
|
$rightSymbol->incUse(); |
50
|
1 |
|
$symbol->setReferencedTo($rightSymbol); |
51
|
1 |
|
} else { |
52
|
|
|
$context->debug('Cannot fetch variable by name: ' . $rightVarName); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
$symbol->incSets(); |
57
|
1 |
|
return $compiledExpression; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$context->debug('Unknown how to pass symbol by ref'); |
61
|
|
|
return new CompiledExpression(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.