Completed
Push — master ( 8bad52...c13fe7 )
by Дмитрий
03:41
created

ArrayIllegalOffsetType::analyzeExpression()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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