Skip to content

Commit d520c9f

Browse files
Fix non empty array with lot of optional keys
1 parent 815f455 commit d520c9f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ public function getAllArrays(): array
216216
} else {
217217
$optionalKeysCombinations = [
218218
[],
219+
array_slice($this->optionalKeys, 0, 1, true),
220+
array_slice($this->optionalKeys, -1, 1, true),
219221
$this->optionalKeys,
220222
];
221223
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,14 @@ public function testBug7143(): void
11171117
"Offset 'bar' might not exist on non-empty-array{foo?: string, bar?: string}.",
11181118
13,
11191119
],
1120+
[
1121+
"Offset 'foo' might not exist on non-empty-array{foo?: string, bar?: string, 1?: 1, 2?: 2, 3?: 3, 4?: 4, 5?: 5, 6?: 6, ...}.",
1122+
21,
1123+
],
1124+
[
1125+
"Offset 'bar' might not exist on non-empty-array{foo?: string, bar?: string, 1?: 1, 2?: 2, 3?: 3, 4?: 4, 5?: 5, 6?: 6, ...}.",
1126+
22,
1127+
],
11201128
]);
11211129
}
11221130

tests/PHPStan/Rules/Arrays/data/bug-7143.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ public function test(array $arr): void
1212
echo $arr['foo'];
1313
echo $arr['bar'];
1414
}
15+
16+
/**
17+
* @param array{foo?: string, bar?: string, 1?:1, 2?:2, 3?:3, 4?:4, 5?:5, 6?:6, 7?:7, 8?:8, 9?:9}&non-empty-array $arr
18+
*/
19+
public function test2(array $arr): void
20+
{
21+
echo $arr['foo'];
22+
echo $arr['bar'];
23+
}
1524
}

0 commit comments

Comments
 (0)