Completed
Pull Request — master (#126)
by Kévin
03:33
created

ArrayIllegalOffsetType::analyseExpression()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
nc 5
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 6
rs 8.6737
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression;
7
8
use PhpParser\Node\Expr;
9
use PHPSA\Analyzer\Pass;
10
use PHPSA\Context;
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
13
class ArrayIllegalOffsetType implements Pass\AnalyzerPassInterface, Pass\ConfigurablePassInterface
14
{
15
    /**
16
     * @param Expr\Array_|Expr\Assign $expr
17
     * @param Context $context
18
     * @return bool
19
     */
20 33
    public function pass(Expr $expr, Context $context)
21
    {
22 33
        if ($expr instanceof Expr\Array_) {
23 24
            return $this->analyseArray($expr, $context);
24 12
        } else if ($expr instanceof Expr\Assign && $expr->var instanceof Expr\ArrayDimFetch) {
25 1
            return $this->analyseDimensionFetch($expr->var, $context);
26
        }
27 12
    }
28
29
    /**
30
     * @param Expr\ArrayDimFetch $expr
31
     * @param Context $context
32
     *
33
     * @return bool
34
     */
35 1
    private function analyseDimensionFetch(Expr\ArrayDimFetch $expr, Context $context)
36
    {
37 1
        return $this->analyseExpression($expr->dim, $context);
0 ignored issues
show
Bug introduced by
It seems like $expr->dim can be null; however, analyseExpression() 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...
38
    }
39
40
    /**
41
     * @param Expr\Array_ $expr
42
     * @param Context $context
43
     *
44
     * @return bool
45
     */
46 24
    private function analyseArray(Expr\Array_ $expr, Context $context)
47
    {
48 24
        $result = false;
49
50
        /** @var Expr\ArrayItem $item */
51 24
        foreach ($expr->items as $item) {
52 10
            if ($item->key === null) {
53 7
                continue;
54
            }
55
56 3
            $result = $this->analyseExpression($item->key, $context) || $result;
57 24
        }
58
59 24
        return $result;
60
    }
61
62 3
    private function analyseExpression(Expr $expr, Context $context)
63
    {
64 3
        $compiledKey = $context->getExpressionCompiler()->compile($expr);
65 3
        if (!$compiledKey->isTypeKnown() || $compiledKey->isScalar()) {
66 3
            return false;
67
        }
68
69 1
        $keyType = $compiledKey->getTypeName();
70 1
        if ($compiledKey->isObject() && $compiledKey->getValue()) {
71 1
            $keyType = get_class($compiledKey->getValue());
72 1
        }
73
74 1
        if ($expr instanceof Expr\Variable) {
75 1
            $message = sprintf('Illegal array offset type %s for key %s.', $keyType, '$'.$expr->name);
76 1
        } else {
77 1
            $message = sprintf('Illegal array offset type %s.', $keyType);
78
        }
79
80 1
        $context->notice('array.illegal_offset_type', $message, $expr);
81
82 1
        return true;
83
    }
84
85
    /**
86
     * @return array
87
     */
88 1
    public function getRegister()
89
    {
90
        return [
91 1
            Expr\Array_::class,
92 1
            Expr\Assign::class,
93 1
        ];
94
    }
95
96
    /**
97
     * @return TreeBuilder
98
     */
99
    public function getConfiguration()
100
    {
101
        $treeBuilder = new TreeBuilder();
102
        $treeBuilder->root('array.illegal_offset_type')
103
            ->canBeDisabled()
104
        ;
105
106
        return $treeBuilder;
107
    }
108
}
109