| Conditions | 3 |
| Paths | 3 |
| Total Lines | 31 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 12 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 21 | protected function compile($expr, Context $context) |
||
| 22 | { |
||
| 23 | $compiler = $context->getExpressionCompiler(); |
||
| 24 | |||
| 25 | $var = $compiler->compile($expr->var); |
||
| 26 | $dim = $compiler->compile($expr->dim); |
||
|
|
|||
| 27 | |||
| 28 | if (!$var->isArray()) { |
||
| 29 | $context->notice( |
||
| 30 | 'array_dim_fetch_on_non_array', |
||
| 31 | "It's not possible to fetch an array element on a non array", |
||
| 32 | $expr |
||
| 33 | ); |
||
| 34 | |||
| 35 | return new CompiledExpression(); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!in_array($dim->getValue(), $var->getValue())) { |
||
| 39 | $context->notice( |
||
| 40 | 'array_dim_fetch_not_found', |
||
| 41 | "The array does not contain this value", |
||
| 42 | $expr |
||
| 43 | ); |
||
| 44 | |||
| 45 | return new CompiledExpression(); |
||
| 46 | } |
||
| 47 | |||
| 48 | $resultArray = $var->getValue(); |
||
| 49 | |||
| 50 | return CompiledExpression::fromZvalValue($resultArray[$dim->getValue()]); |
||
| 51 | } |
||
| 52 | } |
||
| 53 |
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: