Completed
Push — master ( e1144e...7e8850 )
by Enrico
03:37
created

ArrayDimFetch   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 49
ccs 0
cts 27
cp 0
rs 10
c 2
b 0
f 1
wmc 10
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
D compile() 0 37 10
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);
0 ignored issues
show
Bug introduced by
It seems like $expr->dim can be null; however, compile() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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
        $resultArray = $var->getValue();
54
        return CompiledExpression::fromZvalValue($resultArray[$dim->getValue()]);
55
    }
56
}
57