forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonexistentOffsetInArrayDimFetchCheck.php
More file actions
150 lines (132 loc) · 4.14 KB
/
NonexistentOffsetInArrayDimFetchCheck.php
File metadata and controls
150 lines (132 loc) · 4.14 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use PhpParser\Node\Expr;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
#[AutowiredService]
final class NonexistentOffsetInArrayDimFetchCheck
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
#[AutowiredParameter]
private bool $reportMaybes,
#[AutowiredParameter]
private bool $reportPossiblyNonexistentGeneralArrayOffset,
#[AutowiredParameter]
private bool $reportPossiblyNonexistentConstantArrayOffset,
)
{
}
/**
* @return list<IdentifierRuleError>
*/
public function check(
Scope $scope,
Expr $var,
string $unknownClassPattern,
Type $dimType,
): array
{
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $var),
$unknownClassPattern,
static fn (Type $type): bool => $type->hasOffsetValueType($dimType)->yes(),
);
$type = $typeResult->getType();
if ($type instanceof ErrorType) {
return $typeResult->getUnknownClassErrors();
}
if ($scope->isInExpressionAssign($var) || $scope->isUndefinedExpressionAllowed($var)) {
return [];
}
$hasOffsetValueType = $type->hasOffsetValueType($dimType);
if ($hasOffsetValueType->yes()) {
return [];
}
if ($hasOffsetValueType->no()) {
if ($type->isArray()->yes()) {
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
if ($validArrayDimType instanceof NeverType) {
// Already reported by InvalidKeyInArrayDimFetchRule
return [];
}
}
return [
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
->identifier('offsetAccess.notFound')
->build(),
];
}
if ($this->reportMaybes) {
$report = false;
if ($type instanceof BenevolentUnionType) {
$flattenedTypes = [$type];
} else {
$flattenedTypes = TypeUtils::flattenTypes($type);
}
$validArrayDimType = $dimType instanceof MixedType
? $dimType
: TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
foreach ($flattenedTypes as $innerType) {
$dimTypeToCheck = $innerType->isArray()->yes() ? $validArrayDimType : $dimType;
if (
$this->reportPossiblyNonexistentGeneralArrayOffset
&& $innerType->isArray()->yes()
&& !$innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
) {
$report = true;
break;
}
if (
$this->reportPossiblyNonexistentConstantArrayOffset
&& $innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
) {
$report = true;
break;
}
if ($dimTypeToCheck instanceof BenevolentUnionType) {
$flattenedInnerTypes = [$dimTypeToCheck];
} else {
$flattenedInnerTypes = TypeUtils::flattenTypes($dimTypeToCheck);
}
foreach ($flattenedInnerTypes as $innerDimType) {
if (
$innerType->hasOffsetValueType($innerDimType)->no()
) {
if ($innerType->isString()->yes() && $innerDimType->isInteger()->yes()) {
continue;
}
$report = true;
break 2;
}
}
}
if ($report) {
return [
RuleErrorBuilder::message(sprintf('Offset %s might not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
->identifier('offsetAccess.notFound')
->build(),
];
}
}
return [];
}
}