Skip to content

Commit 443c595

Browse files
committed
fix
1 parent 80e7fac commit 443c595

File tree

3 files changed

+11
-38
lines changed

3 files changed

+11
-38
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6146,6 +6146,8 @@ private function isImplicitArrayCreation(array $dimFetchStack, Scope $scope): Tr
61466146
*/
61476147
private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, array $offsetTypes, Type $offsetValueType, Type $valueToWrite, Scope $scope, array &$additionalExpressions = []): Type
61486148
{
6149+
$originalValueToWrite = $valueToWrite;
6150+
61496151
$offsetValueTypeStack = [$offsetValueType];
61506152
foreach (array_slice($offsetTypes, 0, -1) as $offsetType) {
61516153
if ($offsetType === null) {
@@ -6234,7 +6236,12 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
62346236
}
62356237
}
62366238

6237-
if (count($dimFetchStack) > 1) {
6239+
if (count($dimFetchStack) === 1) {
6240+
$dimFetch = $dimFetchStack[0];
6241+
if ($dimFetch->dim !== null) {
6242+
$additionalExpressions[] = [$dimFetchStack[0], $originalValueToWrite];
6243+
}
6244+
} else {
62386245
$offsetValueType = $valueToWrite;
62396246
foreach ($dimFetchStack as $dimFetch) {
62406247
if ($dimFetch->dim === null) {
@@ -6243,12 +6250,10 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
62436250
}
62446251

62456252
$offsetType = $scope->getType($dimFetch->dim);
6246-
if (!$offsetValueType->hasOffsetValueType($offsetType)->yes()) {
6247-
$additionalExpressions = [];
6248-
break;
6249-
}
6250-
62516253
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
6254+
if ($offsetValueType instanceof ErrorType) {
6255+
$offsetValueType = new ConstantArrayType([], []);
6256+
}
62526257
$additionalExpressions[] = [$dimFetch, $offsetValueType];
62536258
}
62546259
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,34 +1014,6 @@ public function testBug13538(): void
10141014
"Offset int might not exist on non-empty-array<int, ''>.",
10151015
17,
10161016
],
1017-
[
1018-
"Offset int might not exist on non-empty-array<int, ''>.",
1019-
21,
1020-
],
1021-
[
1022-
"Offset int might not exist on non-empty-array<int, ''>.",
1023-
25,
1024-
],
1025-
[
1026-
"Offset int might not exist on non-empty-array<int, ''>.",
1027-
41,
1028-
],
1029-
[
1030-
"Offset int might not exist on non-empty-array<int, ''>.",
1031-
45,
1032-
],
1033-
[
1034-
"Offset int might not exist on non-empty-array<int, ''>.",
1035-
49,
1036-
],
1037-
[
1038-
"Offset string might not exist on non-empty-array<string, ''>.",
1039-
68,
1040-
],
1041-
[
1042-
"Offset int might not exist on non-empty-array<int, non-empty-array<string, ''>>.",
1043-
68,
1044-
],
10451017
]);
10461018
}
10471019

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,5 @@ function doBar(array $arr, int $i, string $s): void
5757
assertType("non-empty-array<string, ''>", $logs[$i]);
5858
assertType("''", $logs[$i][$s]);
5959
echo $logs[$i][$s];
60-
61-
$i++;
62-
63-
echo $logs[$i][$s];
6460
}
6561
}

0 commit comments

Comments
 (0)