forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonexistentOffsetInArrayDimFetchCheck.php
118 lines (106 loc) · 3.16 KB
/
NonexistentOffsetInArrayDimFetchCheck.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
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use PhpParser\Node\Expr;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
final class NonexistentOffsetInArrayDimFetchCheck
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private bool $reportMaybes,
private bool $reportPossiblyNonexistentGeneralArrayOffset,
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 [];
}
if ($type->hasOffsetValueType($dimType)->no()) {
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);
}
foreach ($flattenedTypes as $innerType) {
if (
$this->reportPossiblyNonexistentGeneralArrayOffset
&& $innerType->isArray()->yes()
&& !$innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimType)->yes()
) {
$report = true;
break;
}
if (
$this->reportPossiblyNonexistentConstantArrayOffset
&& $innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimType)->yes()
) {
$report = true;
break;
}
if ($dimType instanceof UnionType) {
if ($innerType->hasOffsetValueType($dimType)->no()) {
$report = true;
break;
}
continue;
}
foreach (TypeUtils::flattenTypes($dimType) as $innerDimType) {
if ($innerType->hasOffsetValueType($innerDimType)->no()) {
$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 [];
}
}