-
Notifications
You must be signed in to change notification settings - Fork 428
/
NonexistentOffsetInArrayDimFetchRule.php
105 lines (87 loc) · 2.76 KB
/
NonexistentOffsetInArrayDimFetchRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\ArrayDimFetch>
*/
class NonexistentOffsetInArrayDimFetchRule implements \PHPStan\Rules\Rule
{
private RuleLevelHelper $ruleLevelHelper;
private NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck;
private bool $reportMaybes;
public function __construct(
RuleLevelHelper $ruleLevelHelper,
NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck,
bool $reportMaybes
)
{
$this->ruleLevelHelper = $ruleLevelHelper;
$this->nonexistentOffsetInArrayDimFetchCheck = $nonexistentOffsetInArrayDimFetchCheck;
$this->reportMaybes = $reportMaybes;
}
public function getNodeType(): string
{
return \PhpParser\Node\Expr\ArrayDimFetch::class;
}
public function processNode(\PhpParser\Node $node, Scope $scope): array
{
if ($node->dim !== null) {
$dimType = $scope->getType($node->dim);
$unknownClassPattern = sprintf('Access to offset %s on an unknown class %%s.', SprintfHelper::escapeFormatString($dimType->describe(VerbosityLevel::value())));
} else {
$dimType = null;
$unknownClassPattern = 'Access to an offset on an unknown class %s.';
}
$isOffsetAccessibleTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->var,
$unknownClassPattern,
static function (Type $type): bool {
return $type->isOffsetAccessible()->yes();
}
);
$isOffsetAccessibleType = $isOffsetAccessibleTypeResult->getType();
if ($isOffsetAccessibleType instanceof ErrorType) {
return $isOffsetAccessibleTypeResult->getUnknownClassErrors();
}
$isOffsetAccessible = $isOffsetAccessibleType->isOffsetAccessible();
if ($scope->isInExpressionAssign($node) && $isOffsetAccessible->yes()) {
return [];
}
if (!$isOffsetAccessible->yes()) {
if ($isOffsetAccessible->no() || $this->reportMaybes) {
if ($dimType !== null) {
return [
RuleErrorBuilder::message(sprintf(
'Cannot access offset %s on %s.',
$dimType->describe(VerbosityLevel::value()),
$isOffsetAccessibleType->describe(VerbosityLevel::value())
))->build(),
];
}
return [
RuleErrorBuilder::message(sprintf(
'Cannot access an offset on %s.',
$isOffsetAccessibleType->describe(VerbosityLevel::typeOnly())
))->build(),
];
}
return [];
}
if ($dimType === null) {
return [];
}
return $this->nonexistentOffsetInArrayDimFetchCheck->check(
$scope,
$node->var,
$unknownClassPattern,
$dimType
);
}
}