Skip to content

Commit 184ce9e

Browse files
Do not report non existent offset for invalid ones
1 parent c87456e commit 184ce9e

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use PHPStan\Rules\RuleLevelHelper;
1313
use PHPStan\Type\BenevolentUnionType;
1414
use PHPStan\Type\ErrorType;
15+
use PHPStan\Type\NeverType;
1516
use PHPStan\Type\Type;
17+
use PHPStan\Type\TypeCombinator;
1618
use PHPStan\Type\TypeUtils;
1719
use PHPStan\Type\VerbosityLevel;
1820
use function count;
@@ -59,7 +61,13 @@ public function check(
5961
return [];
6062
}
6163

62-
if ($type->hasOffsetValueType($dimType)->no()) {
64+
// Invalid dim types are already reported by InvalidKeyInArrayDimFetchRule and InvalidKeyInArrayItemRule.
65+
$validDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
66+
if ($validDimType instanceof NeverType) {
67+
return [];
68+
}
69+
70+
if ($type->hasOffsetValueType($validDimType)->no()) {
6371
return [
6472
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
6573
->identifier('offsetAccess.notFound')
@@ -81,23 +89,23 @@ public function check(
8189
$this->reportPossiblyNonexistentGeneralArrayOffset
8290
&& $innerType->isArray()->yes()
8391
&& !$innerType->isConstantArray()->yes()
84-
&& !$innerType->hasOffsetValueType($dimType)->yes()
92+
&& !$innerType->hasOffsetValueType($validDimType)->yes()
8593
) {
8694
$report = true;
8795
break;
8896
}
8997
if (
9098
$this->reportPossiblyNonexistentConstantArrayOffset
9199
&& $innerType->isConstantArray()->yes()
92-
&& !$innerType->hasOffsetValueType($dimType)->yes()
100+
&& !$innerType->hasOffsetValueType($validDimType)->yes()
93101
) {
94102
$report = true;
95103
break;
96104
}
97-
if ($dimType instanceof BenevolentUnionType) {
98-
$flattenedInnerTypes = [$dimType];
105+
if ($validDimType instanceof BenevolentUnionType) {
106+
$flattenedInnerTypes = [$validDimType];
99107
} else {
100-
$flattenedInnerTypes = TypeUtils::flattenTypes($dimType);
108+
$flattenedInnerTypes = TypeUtils::flattenTypes($validDimType);
101109
}
102110
foreach ($flattenedInnerTypes as $innerDimType) {
103111
if (

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ public function testStrings(): void
193193
'Offset 12.34 does not exist on \'foo\'.',
194194
13,
195195
],
196-
[
197-
'Offset int|object might not exist on \'foo\'.',
198-
16,
199-
],
200196
[
201197
'Offset \'foo\' might not exist on array|string.',
202198
24,
@@ -205,10 +201,6 @@ public function testStrings(): void
205201
'Offset 12.34 might not exist on array|string.',
206202
28,
207203
],
208-
[
209-
'Offset int|object might not exist on array|string.',
210-
32,
211-
],
212204
]);
213205
}
214206

0 commit comments

Comments
 (0)