1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSA\Compiler\Expression; |
4
|
|
|
|
5
|
|
|
use PHPSA\CompiledExpression; |
6
|
|
|
use PHPSA\Context; |
7
|
|
|
use PhpParser\Node; |
8
|
|
|
use PHPSA\Definition\ClassDefinition; |
9
|
|
|
|
10
|
|
|
class Assign extends AbstractExpressionCompiler |
11
|
|
|
{ |
12
|
|
|
protected $name = 'PhpParser\Node\Expr\Assign'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* $a = 3; |
16
|
|
|
* |
17
|
|
|
* @param \PhpParser\Node\Expr\Assign $expr |
18
|
|
|
* @param Context $context |
19
|
|
|
* @return CompiledExpression |
20
|
|
|
*/ |
21
|
|
|
protected function compile($expr, Context $context) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$compiler = $context->getExpressionCompiler(); |
24
|
|
|
|
25
|
|
|
$compiledExpression = $compiler->compile($expr->expr); |
26
|
|
|
|
27
|
|
|
if ($expr->var instanceof Node\Expr\List_) { |
28
|
|
|
$isCorrectType = $compiledExpression->isArray(); |
29
|
|
|
|
30
|
|
|
foreach ($expr->var->items as $var) { |
31
|
|
|
if ($var === null) { |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!$var->value instanceof Node\Expr\Variable) { |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($var->value->name instanceof Node\Expr\Variable) { |
40
|
|
|
$this->compileVariableDeclaration($compiler->compile($var->value->name), new CompiledExpression(), $context); |
|
|
|
|
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$symbol = $context->getSymbol($var->value->name); |
45
|
|
|
if (!$symbol) { |
46
|
|
|
$symbol = new \PHPSA\Variable( |
47
|
|
|
$var->value->name, |
48
|
|
|
null, |
49
|
|
|
CompiledExpression::UNKNOWN, |
50
|
|
|
$context->getCurrentBranch() |
51
|
|
|
); |
52
|
|
|
$context->addVariable($symbol); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$isCorrectType) { |
56
|
|
|
$symbol->modify(CompiledExpression::NULL, null); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$symbol->incSets(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new CompiledExpression(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($expr->var instanceof Node\Expr\Variable) { |
66
|
|
|
$this->compileVariableDeclaration($compiler->compile($expr->var->name), $compiledExpression, $context); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $compiledExpression; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($expr->var instanceof Node\Expr\PropertyFetch) { |
72
|
|
|
$compiledExpression = $compiler->compile($expr->var->var); |
73
|
|
|
if ($compiledExpression->getType() == CompiledExpression::OBJECT) { |
74
|
|
|
$objectDefinition = $compiledExpression->getValue(); |
75
|
|
|
if ($objectDefinition instanceof ClassDefinition) { |
76
|
|
|
if (is_string($expr->var->name)) { |
77
|
|
|
if ($objectDefinition->hasProperty($expr->var->name)) { |
78
|
|
|
return $compiler->compile($objectDefinition->getProperty($expr->var->name)); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$context->debug('Unknown how to pass symbol', $expr); |
86
|
|
|
return new CompiledExpression(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
protected function compileVariableDeclaration(CompiledExpression $variableName, CompiledExpression $value, Context $context) |
91
|
|
|
{ |
92
|
|
|
switch ($variableName->getType()) { |
93
|
|
|
case CompiledExpression::STRING: |
94
|
|
|
break; |
95
|
|
|
default: |
96
|
|
|
$context->debug('Unexpected type of Variable name after compile'); |
97
|
|
|
return new CompiledExpression(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$symbol = $context->getSymbol($variableName->getValue()); |
101
|
|
|
if ($symbol) { |
102
|
|
|
$symbol->modify($value->getType(), $value->getValue()); |
103
|
|
|
$context->modifyReferencedVariables( |
104
|
|
|
$symbol, |
105
|
|
|
$value->getType(), |
106
|
|
|
$value->getValue() |
107
|
|
|
); |
108
|
|
|
} else { |
109
|
|
|
$symbol = new \PHPSA\Variable( |
110
|
|
|
$variableName->getValue(), |
111
|
|
|
$value->getValue(), |
112
|
|
|
$value->getType(), |
113
|
|
|
$context->getCurrentBranch() |
114
|
|
|
); |
115
|
|
|
$context->addVariable($symbol); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$symbol->incSets(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.