Skip to content

Commit 03e629c

Browse files
Rework
1 parent f05e587 commit 03e629c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function check(
7676
} else {
7777
$flattenedTypes = TypeUtils::flattenTypes($type);
7878
}
79+
7980
foreach ($flattenedTypes as $innerType) {
8081
if (
8182
$this->reportPossiblyNonexistentGeneralArrayOffset
@@ -95,9 +96,11 @@ public function check(
9596
break;
9697
}
9798
if ($dimType instanceof UnionType) {
98-
if ($innerType->hasOffsetValueType($dimType)->no()) {
99-
$report = true;
100-
break;
99+
foreach ($dimType->getTypes() as $subDimType) {
100+
if ($innerType->hasOffsetValueType($subDimType)->no()) {
101+
$report = true;
102+
break 2;
103+
}
101104
}
102105
continue;
103106
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function testRule(): void
119119
'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).',
120120
258,
121121
],
122+
[
123+
'Offset int|null might not exist on array<int, string>.',
124+
309,
125+
],
122126
[
123127
'Offset null does not exist on array<int, string>.',
124128
310,
@@ -127,6 +131,10 @@ public function testRule(): void
127131
'Offset int does not exist on array<string, string>.',
128132
312,
129133
],
134+
[
135+
' Offset int|null might not exist on array<string, string>.',
136+
314,
137+
],
130138
[
131139
'Offset \'baz\' might not exist on array{bar: 1, baz?: 2}.',
132140
344,
@@ -185,6 +193,10 @@ public function testStrings(): void
185193
'Offset 12.34 does not exist on \'foo\'.',
186194
13,
187195
],
196+
[
197+
"Offset int|object might not exist on 'foo'.",
198+
16,
199+
],
188200
[
189201
'Offset \'foo\' might not exist on array|string.',
190202
24,
@@ -193,6 +205,10 @@ public function testStrings(): void
193205
'Offset 12.34 might not exist on array|string.',
194206
28,
195207
],
208+
[
209+
'Offset int|object might not exist on array|string.',
210+
32,
211+
],
196212
]);
197213
}
198214

@@ -928,8 +944,6 @@ public function testBug3747(): void
928944

929945
public function testBug1061(): void
930946
{
931-
$this->reportPossiblyNonexistentConstantArrayOffset = true;
932-
933947
$this->analyse([__DIR__ . '/data/bug-1061.php'], [
934948
[
935949
"Offset 'one'|'two' might not exist on array{two: 1, three: 2}.",

0 commit comments

Comments
 (0)