ArrayIllegalOffsetType   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 8 4
A analyzeDimensionFetch() 0 15 4
A analyzeArray() 0 15 4
A analyzeExpression() 0 19 4
A getRegister() 0 7 1
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