|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSA\Compiler\Expression; |
|
4
|
|
|
|
|
5
|
|
|
use PHPSA\CompiledExpression; |
|
6
|
|
|
use PHPSA\Context; |
|
7
|
|
|
|
|
8
|
|
|
class ArrayDimFetch extends AbstractExpressionCompiler |
|
9
|
|
|
{ |
|
10
|
|
|
protected $name = 'PhpParser\Node\Expr\ArrayDimFetch'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* $array[1], $array[$var], $array["string"], "string"[1] |
|
14
|
|
|
* |
|
15
|
|
|
* @param \PhpParser\Node\Expr\ArrayDimFetch $expr |
|
16
|
|
|
* @param Context $context |
|
17
|
|
|
* @return CompiledExpression |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function compile($expr, Context $context) |
|
20
|
|
|
{ |
|
21
|
|
|
$compiler = $context->getExpressionCompiler(); |
|
22
|
|
|
$var = $compiler->compile($expr->var); |
|
23
|
|
|
$dim = $compiler->compile($expr->dim); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if ($var->getType() === CompiledExpression::UNIMPLEMENTED |
|
26
|
|
|
|| $dim->getType() === CompiledExpression::UNIMPLEMENTED |
|
27
|
|
|
) { |
|
28
|
|
|
return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!$var->isTypeKnown() || !$dim->isTypeKnown() |
|
32
|
|
|
|| !$var->isCorrectValue() || !$dim->isCorrectValue() |
|
33
|
|
|
) { |
|
34
|
|
|
return new CompiledExpression(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
switch ($var->getType()) { |
|
38
|
|
|
case CompiledExpression::STRING: |
|
39
|
|
|
case CompiledExpression::ARR: |
|
40
|
|
|
if ($dim->isArray()) { |
|
41
|
|
|
$context->notice( |
|
42
|
|
|
'language_error', |
|
43
|
|
|
'Illegal offset type', |
|
44
|
|
|
$expr |
|
45
|
|
|
); |
|
46
|
|
|
return new CompiledExpression(); |
|
47
|
|
|
} |
|
48
|
|
|
break; |
|
49
|
|
|
default: |
|
50
|
|
|
break; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($var->isArray() && $var->isCorrectValue()) { |
|
54
|
|
|
$resultArray = $var->getValue(); |
|
55
|
|
|
return CompiledExpression::fromZvalValue($resultArray[$dim->getValue()]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return new CompiledExpression(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: