Skip to content

Commit 6483f7c

Browse files
committed
fix
1 parent a3f0dd0 commit 6483f7c

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
@@ -1021,34 +1021,6 @@ public function testBug13538(): void
10211021
"Offset int might not exist on non-empty-array<int, ''>.",
10221022
17,
10231023
],
1024-
[
1025-
"Offset int might not exist on non-empty-array<int, ''>.",
1026-
21,
1027-
],
1028-
[
1029-
"Offset int might not exist on non-empty-array<int, ''>.",
1030-
25,
1031-
],
1032-
[
1033-
"Offset int might not exist on non-empty-array<int, ''>.",
1034-
41,
1035-
],
1036-
[
1037-
"Offset int might not exist on non-empty-array<int, ''>.",
1038-
45,
1039-
],
1040-
[
1041-
"Offset int might not exist on non-empty-array<int, ''>.",
1042-
49,
1043-
],
1044-
[
1045-
"Offset string might not exist on non-empty-array<string, ''>.",
1046-
68,
1047-
],
1048-
[
1049-
"Offset int might not exist on non-empty-array<int, non-empty-array<string, ''>>.",
1050-
68,
1051-
],
10521024
]);
10531025
}
10541026

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)