Skip to content

Commit d9ffd4e

Browse files
committed
Try fixing assigning array offsets on possibly undefined nested array offsets
1 parent 08465ec commit d9ffd4e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/Analyser/NodeScopeResolver.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -5756,24 +5756,26 @@ static function (): void {
57565756
*/
57575757
private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type $offsetValueType, Type $valueToWrite): Type
57585758
{
5759-
$offsetValueTypeStack = [$offsetValueType];
5759+
$offsetValueTypeStack = [[$offsetValueType, TrinaryLogic::createYes()]];
57605760
foreach (array_slice($offsetTypes, 0, -1) as $offsetType) {
57615761
if ($offsetType === null) {
5762+
$has = TrinaryLogic::createYes();
57625763
$offsetValueType = new ConstantArrayType([], []);
57635764

57645765
} else {
5766+
$has = $offsetValueType->hasOffsetValueType($offsetType);
57655767
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
57665768
if ($offsetValueType instanceof ErrorType) {
57675769
$offsetValueType = new ConstantArrayType([], []);
57685770
}
57695771
}
57705772

5771-
$offsetValueTypeStack[] = $offsetValueType;
5773+
$offsetValueTypeStack[] = [$offsetValueType, $has];
57725774
}
57735775

57745776
foreach (array_reverse($offsetTypes) as $i => $offsetType) {
57755777
/** @var Type $offsetValueType */
5776-
$offsetValueType = array_pop($offsetValueTypeStack);
5778+
[$offsetValueType, $has] = array_pop($offsetValueTypeStack);
57775779
if (
57785780
!$offsetValueType instanceof MixedType
57795781
&& !$offsetValueType->isConstantArray()->yes()
@@ -5788,7 +5790,10 @@ private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type
57885790
}
57895791
$offsetValueType = TypeCombinator::intersect($offsetValueType, TypeCombinator::union(...$types));
57905792
}
5791-
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);
5793+
if (!$has->yes()) {
5794+
$offsetValueType = TypeCombinator::union($offsetValueType, new ConstantArrayType([], []));
5795+
}
5796+
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite);
57925797
}
57935798

57945799
return $valueToWrite;

0 commit comments

Comments
 (0)