ArrayIllegalOffsetType::analyzeExpression()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 19
ccs 0
cts 10
cp 0
crap 20
rs 9.6333
c 0
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\Helper\DefaultMetadataPassTrait;
10
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
11
use PHPSA\Context;
12
13
class ArrayIllegalOffsetType implements AnalyzerPassInterface
14
{
15
    use DefaultMetadataPassTrait;
16
17
    const DESCRIPTION = 'Checks for illegal array key types (for example objects).';
18
19
    /**
20
     * @param Expr\Array_|Expr\Assign $expr
21
     * @param Context $context
22
     * @return bool
23
     */
24
    public function pass(Expr $expr, Context $context)
25
    {
26
        if ($expr instanceof Expr\Array_) {
27
            return $this->analyzeArray($expr, $context);
28
        } elseif ($expr instanceof Expr\Assign && $expr->var instanceof Expr\ArrayDimFetch) {
29
            return $this->analyzeDimensionFetch($expr->var, $context);
30
        }
31
    }
32
33
    /**
34
     * @param Expr\ArrayDimFetch $expr
35
     * @param Context $context
36
     *
37
     * @return bool
38
     */
39
    private function analyzeDimensionFetch(Expr\ArrayDimFetch $expr, Context $context)
40
    {
41
        // $array[]
42
        if ($expr->dim === null) {
43
            return false;
44
        }
45
46
        $var = $context->getExpressionCompiler()->compile($expr->var);
47
        if ($var->isCorrectValue() && $var->isArray()) {
48
            // $array[…]
49
            return $this->analyzeExpression($expr->dim, $context);
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * @param Expr\Array_ $expr
57
     * @param Context $context
58
     *
59
     * @return bool
60
     */
61
    private function analyzeArray(Expr\Array_ $expr, Context $context)
62
    {
63
        $result = false;
64
65
        /** @var Expr\ArrayItem $item */
66
        foreach ($expr->items as $item) {
67
            if ($item->key === null) {
68
                continue;
69
            }
70
71
            $result = $this->analyzeExpression($item->key, $context) || $result;
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * @param Expr $expr
79
     * @param Context $context
80
     *
81
     * @return bool
82
     */
83
    private function analyzeExpression(Expr $expr, Context $context)
84
    {
85
        $compiledKey = $context->getExpressionCompiler()->compile($expr);
86
        if (!$compiledKey->isTypeKnown() || $compiledKey->isScalar()) {
87
            return false;
88
        }
89
90
        $keyType = $compiledKey->getTypeName();
91
92
        if ($expr instanceof Expr\Variable) {
93
            $message = sprintf('Illegal array offset type %s for key %s.', $keyType, '$'.$expr->name);
94
        } else {
95
            $message = sprintf('Illegal array offset type %s.', $keyType);
96
        }
97
98
        $context->notice('array.illegal_offset_type', $message, $expr);
99
100
        return true;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getRegister()
107
    {
108
        return [
109
            Expr\Array_::class,
110
            Expr\Assign::class,
111
        ];
112
    }
113
}
114