Skip to content

Commit a5db337

Browse files
Fix
1 parent 03e629c commit a5db337

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\ErrorType;
1515
use PHPStan\Type\Type;
1616
use PHPStan\Type\TypeUtils;
17-
use PHPStan\Type\UnionType;
1817
use PHPStan\Type\VerbosityLevel;
1918
use function count;
2019
use function sprintf;
@@ -77,6 +76,7 @@ public function check(
7776
$flattenedTypes = TypeUtils::flattenTypes($type);
7877
}
7978

79+
$allowedArrayKeysTypes = AllowedArrayKeysTypes::getType();
8080
foreach ($flattenedTypes as $innerType) {
8181
if (
8282
$this->reportPossiblyNonexistentGeneralArrayOffset
@@ -95,17 +95,16 @@ public function check(
9595
$report = true;
9696
break;
9797
}
98-
if ($dimType instanceof UnionType) {
99-
foreach ($dimType->getTypes() as $subDimType) {
100-
if ($innerType->hasOffsetValueType($subDimType)->no()) {
101-
$report = true;
102-
break 2;
103-
}
104-
}
105-
continue;
98+
if ($dimType instanceof BenevolentUnionType) {
99+
$flattenedInnerTypes = [$dimType];
100+
} else {
101+
$flattenedInnerTypes = TypeUtils::flattenTypes($dimType);
106102
}
107-
foreach (TypeUtils::flattenTypes($dimType) as $innerDimType) {
108-
if ($innerType->hasOffsetValueType($innerDimType)->no()) {
103+
foreach ($flattenedInnerTypes as $innerDimType) {
104+
if (
105+
$allowedArrayKeysTypes->isSuperTypeOf($innerDimType)->yes()
106+
&& $innerType->hasOffsetValueType($innerDimType)->no()
107+
) {
109108
$report = true;
110109
break 2;
111110
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testRule(): void
132132
312,
133133
],
134134
[
135-
' Offset int|null might not exist on array<string, string>.',
135+
'Offset int|null might not exist on array<string, string>.',
136136
314,
137137
],
138138
[
@@ -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)