Completed
Push — master ( 09dcac...3533be )
by Дмитрий
06:57
created

ArrayDimFetch::compile()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 12
eloc 26
nc 10
nop 2
dl 0
loc 41
ccs 0
cts 29
cp 0
crap 156
rs 5.1612
c 2
b 0
f 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        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