1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler; |
7
|
|
|
|
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use PHPSA\Check; |
10
|
|
|
use PHPSA\CompiledExpression; |
11
|
|
|
use PHPSA\Compiler\Event\ExpressionBeforeCompile; |
12
|
|
|
use PHPSA\Context; |
13
|
|
|
use PhpParser\Node; |
14
|
|
|
use PHPSA\Definition\ClassDefinition; |
15
|
|
|
use PHPSA\Exception\RuntimeException; |
16
|
|
|
use PHPSA\Variable; |
17
|
|
|
use PHPSA\Compiler\Expression\AbstractExpressionCompiler; |
18
|
|
|
use Webiny\Component\EventManager\EventManager; |
19
|
|
|
|
20
|
|
|
class Expression |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Context |
24
|
|
|
*/ |
25
|
|
|
protected $context; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EventManager |
29
|
|
|
*/ |
30
|
|
|
protected $eventManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Context $context |
34
|
|
|
*/ |
35
|
434 |
|
public function __construct(Context $context, EventManager $eventManager) |
36
|
|
|
{ |
37
|
434 |
|
$this->context = $context; |
38
|
434 |
|
$this->eventManager = $eventManager; |
39
|
434 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $expr |
43
|
|
|
* @return ExpressionCompilerInterface|AbstractExpressionCompiler |
44
|
|
|
*/ |
45
|
418 |
|
protected function factory($expr) |
46
|
|
|
{ |
47
|
418 |
|
switch (get_class($expr)) { |
48
|
|
|
/** |
49
|
|
|
* Call(s) |
50
|
|
|
*/ |
51
|
418 |
|
case Node\Expr\MethodCall::class: |
52
|
|
|
return new Expression\MethodCall(); |
53
|
418 |
|
case Node\Expr\FuncCall::class: |
54
|
10 |
|
return new Expression\FunctionCall(); |
55
|
410 |
|
case Node\Expr\StaticCall::class: |
56
|
|
|
return new Expression\StaticCall(); |
57
|
|
|
/** |
58
|
|
|
* Operators |
59
|
|
|
*/ |
60
|
410 |
|
case Node\Expr\New_::class: |
61
|
1 |
|
return new Expression\Operators\NewOp(); |
62
|
409 |
|
case Node\Expr\Instanceof_::class: |
63
|
|
|
return new Expression\Operators\InstanceOfOp(); |
64
|
|
|
/** |
65
|
|
|
* AssignOp |
66
|
|
|
*/ |
67
|
409 |
|
case Node\Expr\AssignOp\Pow::class: |
68
|
|
|
return new Expression\AssignOp\Pow(); |
69
|
409 |
|
case Node\Expr\AssignOp\Plus::class: |
70
|
|
|
return new Expression\AssignOp\Plus(); |
71
|
409 |
|
case Node\Expr\AssignOp\Minus::class: |
72
|
|
|
return new Expression\AssignOp\Minus(); |
73
|
409 |
|
case Node\Expr\AssignOp\Mod::class: |
74
|
|
|
return new Expression\AssignOp\Mod(); |
75
|
409 |
|
case Node\Expr\AssignOp\BitwiseOr::class: |
76
|
|
|
return new Expression\AssignOp\BitwiseOr(); |
77
|
409 |
|
case Node\Expr\AssignOp\BitwiseAnd::class: |
78
|
|
|
return new Expression\AssignOp\BitwiseAnd(); |
79
|
|
|
/** |
80
|
|
|
* BinaryOp |
81
|
|
|
*/ |
82
|
409 |
|
case Node\Expr\BinaryOp\Identical::class: |
83
|
28 |
|
return new Expression\BinaryOp\Identical(); |
84
|
381 |
|
case Node\Expr\BinaryOp\Concat::class: |
85
|
1 |
|
return new Expression\Operators\Concat(); |
86
|
380 |
|
case Node\Expr\BinaryOp\NotIdentical::class: |
87
|
14 |
|
return new Expression\BinaryOp\NotIdentical(); |
88
|
366 |
|
case Node\Expr\BinaryOp\Equal::class: |
89
|
34 |
|
return new Expression\BinaryOp\Equal(); |
90
|
332 |
|
case Node\Expr\BinaryOp\NotEqual::class: |
91
|
17 |
|
return new Expression\BinaryOp\NotEqual(); |
92
|
|
|
/** |
93
|
|
|
* @link http://php.net/manual/en/language.operators.increment.php |
94
|
|
|
*/ |
95
|
315 |
|
case Node\Expr\PostInc::class: |
96
|
4 |
|
return new Expression\Operators\PostInc(); |
97
|
311 |
|
case Node\Expr\PostDec::class: |
98
|
4 |
|
return new Expression\Operators\PostDec(); |
99
|
|
|
/** |
100
|
|
|
* Arithmetical |
101
|
|
|
*/ |
102
|
307 |
|
case Node\Expr\BinaryOp\Div::class: |
103
|
37 |
|
return new Expression\Operators\Arithmetical\Div(); |
104
|
270 |
|
case Node\Expr\BinaryOp\Plus::class: |
105
|
45 |
|
return new Expression\Operators\Arithmetical\Plus(); |
106
|
225 |
|
case Node\Expr\BinaryOp\Minus::class: |
107
|
18 |
|
return new Expression\Operators\Arithmetical\Minus(); |
108
|
207 |
|
case Node\Expr\BinaryOp\Mul::class: |
109
|
35 |
|
return new Expression\Operators\Arithmetical\Mul(); |
110
|
172 |
|
case Node\Expr\BinaryOp\Mod::class: |
111
|
35 |
|
return new Expression\Operators\Arithmetical\Mod(); |
112
|
|
|
/** |
113
|
|
|
* Bitwise |
114
|
|
|
* @link http://php.net/manual/ru/language.operators.bitwise.php |
115
|
|
|
*/ |
116
|
137 |
|
case Node\Expr\BinaryOp\BitwiseOr::class: |
117
|
|
|
return new Expression\Operators\Bitwise\BitwiseOr(); |
118
|
137 |
|
case Node\Expr\BinaryOp\BitwiseXor::class: |
119
|
|
|
return new Expression\Operators\Bitwise\BitwiseXor(); |
120
|
137 |
|
case Node\Expr\BinaryOp\BitwiseAnd::class: |
121
|
|
|
return new Expression\Operators\Bitwise\BitwiseAnd(); |
122
|
137 |
|
case Node\Expr\BinaryOp\ShiftRight::class: |
123
|
|
|
return new Expression\Operators\Bitwise\ShiftRight(); |
124
|
137 |
|
case Node\Expr\BinaryOp\ShiftLeft::class: |
125
|
|
|
return new Expression\Operators\Bitwise\ShiftLeft(); |
126
|
137 |
|
case Node\Expr\BitwiseNot::class: |
127
|
|
|
return new Expression\Operators\Bitwise\BitwiseNot(); |
128
|
|
|
/** |
129
|
|
|
* Logical |
130
|
|
|
*/ |
131
|
137 |
|
case Node\Expr\BinaryOp\BooleanOr::class: |
132
|
10 |
|
return new Expression\Operators\Logical\BooleanOr(); |
133
|
127 |
|
case Node\Expr\BinaryOp\BooleanAnd::class: |
134
|
5 |
|
return new Expression\Operators\Logical\BooleanAnd(); |
135
|
122 |
|
case Node\Expr\BooleanNot::class: |
136
|
5 |
|
return new Expression\Operators\Logical\BooleanNot(); |
137
|
117 |
|
case Node\Expr\BinaryOp\LogicalAnd::class: |
138
|
15 |
|
return new Expression\Operators\Logical\LogicalAnd(); |
139
|
102 |
|
case Node\Expr\BinaryOp\LogicalOr::class: |
140
|
17 |
|
return new Expression\Operators\Logical\LogicalOr(); |
141
|
85 |
|
case Node\Expr\BinaryOp\LogicalXor::class: |
142
|
17 |
|
return new Expression\Operators\Logical\LogicalXor(); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Comparison |
146
|
|
|
*/ |
147
|
68 |
|
case Node\Expr\BinaryOp\Greater::class: |
148
|
12 |
|
return new Expression\Operators\Comparison\Greater(); |
149
|
56 |
|
case Node\Expr\BinaryOp\GreaterOrEqual::class: |
150
|
12 |
|
return new Expression\Operators\Comparison\GreaterOrEqual(); |
151
|
44 |
|
case Node\Expr\BinaryOp\Smaller::class: |
152
|
12 |
|
return new Expression\Operators\Comparison\Smaller(); |
153
|
32 |
|
case Node\Expr\BinaryOp\SmallerOrEqual::class: |
154
|
12 |
|
return new Expression\Operators\Comparison\SmallerOrEqual(); |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Casts |
158
|
|
|
*/ |
159
|
20 |
|
case Node\Expr\Cast\Array_::class: |
160
|
1 |
|
return new Expression\Casts\ArrayCast(); |
161
|
20 |
|
case Node\Expr\Cast\Bool_::class: |
162
|
1 |
|
return new Expression\Casts\BoolCast(); |
163
|
20 |
|
case Node\Expr\Cast\Int_::class: |
164
|
1 |
|
return new Expression\Casts\IntCast(); |
165
|
20 |
|
case Node\Expr\Cast\Double::class: |
166
|
1 |
|
return new Expression\Casts\DoubleCast(); |
167
|
20 |
|
case Node\Expr\Cast\Object_::class: |
168
|
1 |
|
return new Expression\Casts\ObjectCast(); |
169
|
20 |
|
case Node\Expr\Cast\String_::class: |
170
|
1 |
|
return new Expression\Casts\StringCast(); |
171
|
20 |
|
case Node\Expr\Cast\Unset_::class: |
172
|
1 |
|
return new Expression\Casts\UnsetCast(); |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Other |
177
|
|
|
*/ |
178
|
19 |
|
case Node\Expr\Closure::class: |
179
|
|
|
return new Expression\Closure(); |
180
|
19 |
|
case Node\Expr\UnaryMinus::class: |
181
|
9 |
|
return new Expression\Operators\UnaryMinus(); |
182
|
10 |
|
case Node\Expr\UnaryPlus::class: |
183
|
9 |
|
return new Expression\Operators\UnaryPlus(); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
1 |
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param object|string $expr |
191
|
|
|
* @return CompiledExpression |
192
|
|
|
*/ |
193
|
434 |
|
public function compile($expr) |
|
|
|
|
194
|
|
|
{ |
195
|
434 |
|
if (is_string($expr)) { |
196
|
11 |
|
return new CompiledExpression(CompiledExpression::STRING, $expr); |
197
|
|
|
} |
198
|
|
|
|
199
|
434 |
|
if (is_null($expr)) { |
200
|
9 |
|
return new CompiledExpression(CompiledExpression::NULL); |
201
|
|
|
} |
202
|
|
|
|
203
|
434 |
|
if (!is_object($expr)) { |
204
|
|
|
throw new InvalidArgumentException('$expr must be string/object/null'); |
205
|
|
|
} |
206
|
|
|
|
207
|
434 |
|
$this->eventManager->fire( |
208
|
434 |
|
ExpressionBeforeCompile::EVENT_NAME, |
209
|
434 |
|
new ExpressionBeforeCompile( |
210
|
434 |
|
$expr, |
211
|
434 |
|
$this->context |
212
|
434 |
|
) |
213
|
434 |
|
); |
214
|
|
|
|
215
|
434 |
|
$className = get_class($expr); |
216
|
|
|
switch ($className) { |
217
|
434 |
|
case Node\Arg::class: |
218
|
|
|
/** |
219
|
|
|
* @todo Better compile |
220
|
|
|
*/ |
221
|
2 |
|
return $this->compile($expr->value); |
222
|
434 |
|
case Node\Expr\PropertyFetch::class: |
223
|
|
|
return $this->passPropertyFetch($expr); |
224
|
434 |
|
case Node\Stmt\Property::class: |
225
|
|
|
return $this->passProperty($expr); |
226
|
434 |
|
case Node\Expr\ClassConstFetch::class: |
227
|
|
|
return $this->passConstFetch($expr); |
228
|
434 |
|
case Node\Expr\Assign::class: |
229
|
11 |
|
return $this->passSymbol($expr); |
230
|
434 |
|
case Node\Expr\AssignRef::class: |
231
|
1 |
|
return $this->passSymbolByRef($expr); |
232
|
434 |
|
case Node\Expr\Variable::class: |
233
|
7 |
|
return $this->passExprVariable($expr); |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Expressions |
237
|
|
|
*/ |
238
|
434 |
|
case Node\Expr\Array_::class: |
239
|
24 |
|
return $this->getArray($expr); |
240
|
433 |
|
case Node\Expr\ConstFetch::class: |
241
|
5 |
|
return $this->constFetch($expr); |
242
|
433 |
|
case Node\Name::class: |
243
|
11 |
|
return $this->getNodeName($expr); |
244
|
433 |
|
case Node\Name\FullyQualified::class: |
245
|
|
|
return $this->getFullyQualifiedNodeName($expr); |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Simple Scalar(s) |
249
|
|
|
*/ |
250
|
433 |
|
case \PHPSA\Node\Scalar\Nil::class: |
251
|
21 |
|
return new CompiledExpression(CompiledExpression::NULL); |
252
|
432 |
|
case Node\Scalar\LNumber::class: |
253
|
285 |
|
return new CompiledExpression(CompiledExpression::INTEGER, $expr->value); |
254
|
426 |
|
case Node\Scalar\DNumber::class: |
255
|
135 |
|
return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value); |
256
|
425 |
|
case Node\Scalar\String_::class: |
257
|
24 |
|
return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
258
|
420 |
|
case \PHPSA\Node\Scalar\Boolean::class: |
259
|
97 |
|
return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
260
|
418 |
|
case \PHPSA\Node\Scalar\Fake::class: |
261
|
32 |
|
return new CompiledExpression($expr->type, $expr->value); |
262
|
|
|
} |
263
|
|
|
|
264
|
418 |
|
$expressionCompiler = $this->factory($expr); |
265
|
418 |
|
if (!$expressionCompiler) { |
266
|
1 |
|
$this->context->debug("Expression compiler is not implemented for {$className}"); |
267
|
1 |
|
return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
268
|
|
|
} |
269
|
|
|
|
270
|
418 |
|
$result = $expressionCompiler->pass($expr, $this->context); |
271
|
418 |
|
if (!$result instanceof CompiledExpression) { |
272
|
|
|
throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
273
|
|
|
} |
274
|
|
|
|
275
|
418 |
|
return $result; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @todo Implement |
280
|
|
|
* |
281
|
|
|
* @param Node\Stmt\Property $st |
282
|
|
|
* @return CompiledExpression |
283
|
|
|
*/ |
284
|
|
|
public function passProperty(Node\Stmt\Property $st) |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$docBlock = $st->getDocComment(); |
287
|
|
|
if (!$docBlock) { |
288
|
|
|
$this->context->notice( |
289
|
|
|
'missing-docblock', |
290
|
|
|
sprintf('Missing docblock for $%s property', $st->props[0]->name), |
291
|
|
|
$st |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
return new CompiledExpression(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$phpdoc = new \phpDocumentor\Reflection\DocBlock($docBlock->getText()); |
298
|
|
|
|
299
|
|
|
$varTags = $phpdoc->getTagsByName('var'); |
300
|
|
|
if ($varTags) { |
|
|
|
|
301
|
|
|
/** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ |
302
|
|
|
$varTag = current($varTags); |
303
|
|
|
|
304
|
|
|
$typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
305
|
|
|
|
306
|
|
|
try { |
307
|
|
|
$type = $typeResolver->resolve($varTag->getType()); |
308
|
|
|
} catch (\InvalidArgumentException $e) { |
309
|
|
|
return new CompiledExpression(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($type) { |
313
|
|
|
switch (get_class($type)) { |
314
|
|
|
case \phpDocumentor\Reflection\Types\Object_::class: |
315
|
|
|
return new CompiledExpression( |
316
|
|
|
CompiledExpression::OBJECT |
317
|
|
|
); |
318
|
|
|
case \phpDocumentor\Reflection\Types\Integer::class: |
319
|
|
|
return new CompiledExpression( |
320
|
|
|
CompiledExpression::INTEGER |
321
|
|
|
); |
322
|
|
|
case \phpDocumentor\Reflection\Types\String_::class: |
323
|
|
|
return new CompiledExpression( |
324
|
|
|
CompiledExpression::STRING |
325
|
|
|
); |
326
|
|
|
case \phpDocumentor\Reflection\Types\Float_::class: |
327
|
|
|
return new CompiledExpression( |
328
|
|
|
CompiledExpression::DOUBLE |
329
|
|
|
); |
330
|
|
|
case \phpDocumentor\Reflection\Types\Null_::class: |
331
|
|
|
return new CompiledExpression( |
332
|
|
|
CompiledExpression::NULL |
333
|
|
|
); |
334
|
|
|
case \phpDocumentor\Reflection\Types\Boolean::class: |
335
|
|
|
return new CompiledExpression( |
336
|
|
|
CompiledExpression::BOOLEAN |
337
|
|
|
); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return new CompiledExpression(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param Node\Expr\Variable $expr |
347
|
|
|
* @return CompiledExpression |
348
|
|
|
*/ |
349
|
1 |
|
public function declareVariable(Node\Expr\Variable $expr) |
350
|
|
|
{ |
351
|
1 |
|
$variable = $this->context->getSymbol($expr->name); |
352
|
1 |
|
if (!$variable) { |
353
|
|
|
$variable = new Variable($expr->name, null, CompiledExpression::UNKNOWN, $this->context->getCurrentBranch()); |
|
|
|
|
354
|
|
|
$this->context->addVariable($variable); |
355
|
|
|
} |
356
|
|
|
|
357
|
1 |
|
return new CompiledExpression($variable->getType(), $variable->getValue(), $variable); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param Node\Name\FullyQualified $expr |
362
|
|
|
* @return CompiledExpression |
363
|
|
|
*/ |
364
|
|
|
public function getFullyQualifiedNodeName(Node\Name\FullyQualified $expr) |
365
|
|
|
{ |
366
|
|
|
$this->context->debug('Unimplemented FullyQualified', $expr); |
367
|
|
|
|
368
|
|
|
return new CompiledExpression; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param Node\Name $expr |
373
|
|
|
* @return CompiledExpression |
374
|
|
|
*/ |
375
|
11 |
|
public function getNodeName(Node\Name $expr) |
376
|
|
|
{ |
377
|
11 |
|
$nodeString = $expr->toString(); |
378
|
11 |
|
if ($nodeString === 'null') { |
379
|
1 |
|
return new CompiledExpression(CompiledExpression::NULL); |
380
|
|
|
} |
381
|
|
|
|
382
|
10 |
|
if (in_array($nodeString, ['parent'], true)) { |
383
|
|
|
/** @var ClassDefinition $scope */ |
384
|
|
|
$scope = $this->context->scope; |
385
|
|
|
assert($scope instanceof ClassDefinition); |
386
|
|
|
|
387
|
|
|
if ($scope->getExtendsClass()) { |
|
|
|
|
388
|
|
|
$definition = $scope->getExtendsClassDefinition(); |
389
|
|
|
if ($definition) { |
390
|
|
|
return new CompiledExpression(CompiledExpression::OBJECT, $definition); |
391
|
|
|
} |
392
|
|
|
} else { |
393
|
|
|
$this->context->notice( |
394
|
|
|
'no-parent', |
395
|
|
|
'Cannot access parent:: when current class scope has no parent', |
396
|
|
|
$expr |
397
|
|
|
); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
10 |
|
if (in_array($nodeString, ['self', 'static'], true)) { |
402
|
|
|
return CompiledExpression::fromZvalValue($this->context->scope); |
403
|
|
|
} |
404
|
|
|
|
405
|
10 |
|
if (defined($nodeString)) { |
406
|
1 |
|
return CompiledExpression::fromZvalValue(constant($expr)); |
407
|
|
|
} |
408
|
|
|
|
409
|
10 |
|
return new CompiledExpression(CompiledExpression::STRING, $expr->toString()); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param Node\Expr\PropertyFetch $expr |
414
|
|
|
* @return CompiledExpression |
415
|
|
|
*/ |
416
|
|
|
protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
417
|
|
|
{ |
418
|
|
|
$propertNameCE = $this->compile($expr->name); |
419
|
|
|
|
420
|
|
|
$scopeExpression = $this->compile($expr->var); |
421
|
|
|
if ($scopeExpression->isObject()) { |
422
|
|
|
$scopeExpressionValue = $scopeExpression->getValue(); |
423
|
|
|
if ($scopeExpressionValue instanceof ClassDefinition) { |
424
|
|
|
$propertyName = $propertNameCE->isString() ? $propertNameCE->getValue() : false; |
425
|
|
|
if ($propertyName) { |
426
|
|
|
if ($scopeExpressionValue->hasProperty($propertyName, true)) { |
427
|
|
|
$property = $scopeExpressionValue->getProperty($propertyName, true); |
428
|
|
|
return $this->compile($property); |
429
|
|
|
} else { |
430
|
|
|
$this->context->notice( |
431
|
|
|
'undefined-property', |
432
|
|
|
sprintf( |
433
|
|
|
'Property %s does not exist in %s scope', |
434
|
|
|
$propertyName, |
435
|
|
|
$scopeExpressionValue->getName() |
436
|
|
|
), |
437
|
|
|
$expr |
438
|
|
|
); |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
444
|
|
|
} elseif (!$scopeExpression->canBeObject()) { |
445
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
$this->context->notice( |
449
|
|
|
'property-fetch-on-non-object', |
450
|
|
|
"It's not possible to fetch property on not object", |
451
|
|
|
$expr, |
452
|
|
|
Check::CHECK_BETA |
453
|
|
|
); |
454
|
|
|
|
455
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @param Node\Expr\ClassConstFetch $expr |
460
|
|
|
* @return CompiledExpression |
461
|
|
|
*/ |
462
|
|
|
protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
463
|
|
|
{ |
464
|
|
|
$leftCE = $this->compile($expr->class); |
465
|
|
|
if ($leftCE->isObject()) { |
466
|
|
|
$leftCEValue = $leftCE->getValue(); |
467
|
|
|
if ($leftCEValue instanceof ClassDefinition) { |
468
|
|
|
if (!$leftCEValue->hasConst($expr->name, true)) { |
469
|
|
|
$this->context->notice( |
470
|
|
|
'undefined-const', |
471
|
|
|
sprintf('Constant %s does not exist in %s scope', $expr->name, $expr->class), |
472
|
|
|
$expr |
473
|
|
|
); |
474
|
|
|
return new CompiledExpression(CompiledExpression::UNKNOWN); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return new CompiledExpression(); |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$this->context->debug('Unknown const fetch', $expr); |
482
|
|
|
return new CompiledExpression(); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* @param Node\Expr\Assign $expr |
487
|
|
|
* @return CompiledExpression |
488
|
|
|
*/ |
489
|
11 |
|
protected function passSymbol(Node\Expr\Assign $expr) |
|
|
|
|
490
|
|
|
{ |
491
|
11 |
|
$compiledExpression = $this->compile($expr->expr); |
492
|
|
|
|
493
|
11 |
|
if ($expr->var instanceof Node\Expr\List_) { |
494
|
1 |
|
$isCorrectType = $compiledExpression->isArray(); |
495
|
|
|
|
496
|
1 |
|
foreach ($expr->var->vars as $key => $var) { |
497
|
1 |
|
if (!$var instanceof Node\Expr\Variable) { |
498
|
|
|
continue; |
499
|
|
|
} |
500
|
|
|
|
501
|
1 |
|
if ($var->name instanceof Node\Expr\Variable) { |
502
|
|
|
// TODO what should we do here? |
503
|
|
|
// ex: list(${$foo}, $bar) = [1, 2]; |
504
|
1 |
|
continue; |
505
|
|
|
} |
506
|
|
|
|
507
|
1 |
|
$symbol = $this->context->getSymbol($var->name); |
508
|
1 |
|
if (!$symbol) { |
509
|
1 |
|
$symbol = new Variable( |
510
|
1 |
|
$var->name, |
|
|
|
|
511
|
1 |
|
null, |
512
|
1 |
|
CompiledExpression::UNKNOWN, |
513
|
1 |
|
$this->context->getCurrentBranch() |
514
|
1 |
|
); |
515
|
1 |
|
$this->context->addVariable($symbol); |
516
|
1 |
|
} |
517
|
|
|
|
518
|
1 |
|
if (!$isCorrectType) { |
519
|
|
|
$symbol->modify(CompiledExpression::NULL, null); |
520
|
|
|
} |
521
|
|
|
|
522
|
1 |
|
$symbol->incSets(); |
523
|
1 |
|
} |
524
|
|
|
|
525
|
1 |
|
return new CompiledExpression(); |
526
|
|
|
} |
527
|
|
|
|
528
|
11 |
|
if ($expr->var instanceof Node\Expr\Variable) { |
529
|
11 |
|
$compiledExpressionName = $this->compile($expr->var->name); |
|
|
|
|
530
|
11 |
|
switch ($compiledExpressionName->getType()) { |
531
|
11 |
|
case CompiledExpression::STRING: |
532
|
11 |
|
break; |
533
|
|
|
default: |
534
|
|
|
$this->context->debug('Unexpected type of Variable name after compile'); |
535
|
|
|
return new CompiledExpression(); |
536
|
11 |
|
} |
537
|
|
|
|
538
|
11 |
|
$symbol = $this->context->getSymbol($compiledExpressionName->getValue()); |
539
|
11 |
|
if ($symbol) { |
540
|
2 |
|
$symbol->modify($compiledExpression->getType(), $compiledExpression->getValue()); |
541
|
2 |
|
$this->context->modifyReferencedVariables( |
542
|
2 |
|
$symbol, |
543
|
2 |
|
$compiledExpression->getType(), |
544
|
2 |
|
$compiledExpression->getValue() |
545
|
2 |
|
); |
546
|
2 |
|
} else { |
547
|
11 |
|
$symbol = new Variable( |
548
|
11 |
|
$compiledExpressionName->getValue(), |
549
|
11 |
|
$compiledExpression->getValue(), |
550
|
11 |
|
$compiledExpression->getType(), |
551
|
11 |
|
$this->context->getCurrentBranch() |
552
|
11 |
|
); |
553
|
11 |
|
$this->context->addVariable($symbol); |
554
|
|
|
} |
555
|
|
|
|
556
|
11 |
|
$symbol->incSets(); |
557
|
11 |
|
return $compiledExpression; |
558
|
|
|
} |
559
|
|
|
|
560
|
1 |
|
if ($expr->var instanceof Node\Expr\PropertyFetch) { |
561
|
1 |
|
$compiledExpression = $this->compile($expr->var->var); |
562
|
1 |
|
if ($compiledExpression->getType() == CompiledExpression::OBJECT) { |
563
|
1 |
|
$objectDefinition = $compiledExpression->getValue(); |
564
|
1 |
|
if ($objectDefinition instanceof ClassDefinition) { |
565
|
1 |
|
if (is_string($expr->var->name)) { |
566
|
|
|
if ($objectDefinition->hasProperty($expr->var->name)) { |
567
|
|
|
return $this->compile($objectDefinition->getProperty($expr->var->name)); |
568
|
|
|
} |
569
|
|
|
} |
570
|
1 |
|
} |
571
|
1 |
|
} |
572
|
1 |
|
} |
573
|
|
|
|
574
|
1 |
|
$this->context->debug('Unknown how to pass symbol'); |
575
|
1 |
|
return new CompiledExpression(); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @param Node\Expr\AssignRef $expr |
580
|
|
|
* @return CompiledExpression |
581
|
|
|
*/ |
582
|
1 |
|
protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
583
|
|
|
{ |
584
|
1 |
|
if ($expr->var instanceof Node\Expr\Variable) { |
585
|
1 |
|
$name = $expr->var->name; |
586
|
|
|
|
587
|
1 |
|
$compiledExpression = $this->compile($expr->expr); |
588
|
|
|
|
589
|
1 |
|
$symbol = $this->context->getSymbol($name); |
590
|
1 |
|
if ($symbol) { |
591
|
|
|
$symbol->modify($compiledExpression->getType(), $compiledExpression->getValue()); |
592
|
|
|
} else { |
593
|
1 |
|
$symbol = new Variable( |
594
|
1 |
|
$name, |
|
|
|
|
595
|
1 |
|
$compiledExpression->getValue(), |
596
|
1 |
|
$compiledExpression->getType(), |
597
|
1 |
|
$this->context->getCurrentBranch() |
598
|
1 |
|
); |
599
|
1 |
|
$this->context->addVariable($symbol); |
600
|
|
|
} |
601
|
|
|
|
602
|
1 |
|
if ($expr->expr instanceof Node\Expr\Variable) { |
603
|
1 |
|
$rightVarName = $expr->expr->name; |
604
|
|
|
|
605
|
1 |
|
$rightSymbol = $this->context->getSymbol($rightVarName); |
606
|
1 |
|
if ($rightSymbol) { |
607
|
1 |
|
$rightSymbol->incUse(); |
608
|
1 |
|
$symbol->setReferencedTo($rightSymbol); |
609
|
1 |
|
} else { |
610
|
|
|
$this->context->debug('Cannot fetch variable by name: ' . $rightVarName); |
611
|
|
|
} |
612
|
1 |
|
} |
613
|
|
|
|
614
|
1 |
|
$symbol->incSets(); |
615
|
1 |
|
return $compiledExpression; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$this->context->debug('Unknown how to pass symbol by ref'); |
619
|
|
|
return new CompiledExpression(); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* @param Node\Expr\Variable $expr |
624
|
|
|
* @return CompiledExpression |
625
|
|
|
*/ |
626
|
7 |
|
protected function passExprVariable(Node\Expr\Variable $expr) |
627
|
|
|
{ |
628
|
7 |
|
$variable = $this->context->getSymbol($expr->name); |
629
|
7 |
|
if ($variable) { |
630
|
7 |
|
$variable->incGets(); |
631
|
7 |
|
return new CompiledExpression($variable->getType(), $variable->getValue(), $variable); |
632
|
|
|
} |
633
|
|
|
|
634
|
1 |
|
$this->context->notice( |
635
|
1 |
|
'undefined-variable', |
636
|
1 |
|
sprintf('You trying to use undefined variable $%s', $expr->name), |
637
|
|
|
$expr |
638
|
1 |
|
); |
639
|
|
|
|
640
|
1 |
|
return new CompiledExpression(); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Compile Array_ expression to CompiledExpression |
645
|
|
|
* |
646
|
|
|
* @param Node\Expr\Array_ $expr |
647
|
|
|
* @return CompiledExpression |
648
|
|
|
*/ |
649
|
24 |
|
protected function getArray(Node\Expr\Array_ $expr) |
650
|
|
|
{ |
651
|
24 |
|
if ($expr->items === []) { |
652
|
15 |
|
return new CompiledExpression(CompiledExpression::ARR, []); |
653
|
|
|
} |
654
|
|
|
|
655
|
10 |
|
$resultArray = []; |
656
|
|
|
|
657
|
10 |
|
foreach ($expr->items as $item) { |
658
|
10 |
|
$compiledValueResult = $this->compile($item->value); |
659
|
10 |
|
if ($item->key) { |
660
|
2 |
|
$compiledKeyResult = $this->compile($item->key); |
661
|
2 |
|
switch ($compiledKeyResult->getType()) { |
662
|
2 |
|
case CompiledExpression::INTEGER: |
663
|
2 |
|
case CompiledExpression::DOUBLE: |
664
|
2 |
|
case CompiledExpression::BOOLEAN: |
665
|
2 |
|
case CompiledExpression::NULL: |
666
|
2 |
|
case CompiledExpression::STRING: |
667
|
2 |
|
$resultArray[$compiledKeyResult->getValue()] = $compiledValueResult->getValue(); |
668
|
2 |
|
break; |
669
|
|
|
default: |
670
|
|
|
$this->context->debug("Type {$compiledKeyResult->getType()} is not supported for key value"); |
671
|
|
|
return new CompiledExpression(CompiledExpression::ARR); |
672
|
|
|
break; |
|
|
|
|
673
|
2 |
|
} |
674
|
2 |
|
} else { |
675
|
8 |
|
$resultArray[] = $compiledValueResult->getValue(); |
676
|
|
|
} |
677
|
10 |
|
} |
678
|
|
|
|
679
|
10 |
|
return new CompiledExpression(CompiledExpression::ARR, $resultArray); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Convert const fetch expr to CompiledExpression |
684
|
|
|
* |
685
|
|
|
* @param Node\Expr\ConstFetch $expr |
686
|
|
|
* @return CompiledExpression |
687
|
|
|
*/ |
688
|
5 |
|
protected function constFetch(Node\Expr\ConstFetch $expr) |
689
|
|
|
{ |
690
|
5 |
|
if ($expr->name instanceof Node\Name) { |
691
|
5 |
|
if ($expr->name->parts[0] === 'true') { |
692
|
4 |
|
return new CompiledExpression(CompiledExpression::BOOLEAN, true); |
693
|
|
|
} |
694
|
|
|
|
695
|
2 |
|
if ($expr->name->parts[0] === 'false') { |
696
|
|
|
return new CompiledExpression(CompiledExpression::BOOLEAN, false); |
697
|
|
|
} |
698
|
2 |
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* @todo Implement check |
702
|
|
|
*/ |
703
|
2 |
|
return $this->compile($expr->name); |
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.