@@ -5450,14 +5450,15 @@ private function processAssignVar(
54505450 $ offsetValueType = $ varType ;
54515451 $ offsetNativeValueType = $ varNativeType ;
54525452
5453- $ valueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ offsetTypes , $ offsetValueType , $ valueToWrite );
5453+ $ valueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ dimFetchStack , $ offsetTypes , $ offsetValueType , $ valueToWrite, $ scope );
54545454
54555455 if (!$ offsetValueType ->equals ($ offsetNativeValueType ) || !$ valueToWrite ->equals ($ nativeValueToWrite )) {
5456- $ nativeValueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ offsetNativeTypes , $ offsetNativeValueType , $ nativeValueToWrite );
5456+ $ nativeValueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ dimFetchStack , $ offsetNativeTypes , $ offsetNativeValueType , $ nativeValueToWrite, $ scope );
54575457 } else {
54585458 $ rewritten = false ;
54595459 foreach ($ offsetTypes as $ i => $ offsetType ) {
54605460 $ offsetNativeType = $ offsetNativeTypes [$ i ];
5461+
54615462 if ($ offsetType === null ) {
54625463 if ($ offsetNativeType !== null ) {
54635464 throw new ShouldNotHappenException ();
@@ -5471,7 +5472,7 @@ private function processAssignVar(
54715472 continue ;
54725473 }
54735474
5474- $ nativeValueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ offsetNativeTypes , $ offsetNativeValueType , $ nativeValueToWrite );
5475+ $ nativeValueToWrite = $ this ->produceArrayDimFetchAssignValueToWrite ($ dimFetchStack , $ offsetNativeTypes , $ offsetNativeValueType , $ nativeValueToWrite, $ scope );
54755476 $ rewritten = true ;
54765477 break ;
54775478 }
@@ -5784,9 +5785,10 @@ static function (): void {
57845785 }
57855786
57865787 /**
5788+ * @param list<ArrayDimFetch> $dimFetchStack
57875789 * @param list<Type|null> $offsetTypes
57885790 */
5789- private function produceArrayDimFetchAssignValueToWrite (array $ offsetTypes , Type $ offsetValueType , Type $ valueToWrite ): Type
5791+ private function produceArrayDimFetchAssignValueToWrite (array $ dimFetchStack , array $ offsetTypes , Type $ offsetValueType , Type $ valueToWrite, Scope $ scope ): Type
57905792 {
57915793 $ offsetValueTypeStack = [$ offsetValueType ];
57925794 foreach (array_slice ($ offsetTypes , 0 , -1 ) as $ offsetType ) {
@@ -5821,6 +5823,31 @@ private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type
58215823 $ offsetValueType = TypeCombinator::intersect ($ offsetValueType , TypeCombinator::union (...$ types ));
58225824 }
58235825 $ valueToWrite = $ offsetValueType ->setOffsetValueType ($ offsetType , $ valueToWrite , $ i === 0 );
5826+
5827+ $ arrayDimFetch = $ dimFetchStack [$ i ] ?? null ;
5828+ if ($ arrayDimFetch === null || !$ offsetValueType ->isList ()->yes ()) {
5829+ continue ;
5830+ }
5831+
5832+ if ($ scope ->hasExpressionType ($ arrayDimFetch )->yes ()) { // keep list for $list[$index] assignments
5833+ $ valueToWrite = TypeCombinator::intersect ($ valueToWrite , new AccessoryArrayListType ());
5834+ } elseif ($ arrayDimFetch ->dim instanceof BinaryOp \Plus) {
5835+ if ( // keep list for $list[$index + 1] assignments
5836+ $ arrayDimFetch ->dim ->right instanceof Variable
5837+ && $ arrayDimFetch ->dim ->left instanceof Node \Scalar \Int_
5838+ && $ arrayDimFetch ->dim ->left ->value === 1
5839+ && $ scope ->hasExpressionType (new ArrayDimFetch ($ arrayDimFetch ->var , $ arrayDimFetch ->dim ->right ))->yes ()
5840+ ) {
5841+ $ valueToWrite = TypeCombinator::intersect ($ valueToWrite , new AccessoryArrayListType ());
5842+ } elseif ( // keep list for $list[1 + $index] assignments
5843+ $ arrayDimFetch ->dim ->left instanceof Variable
5844+ && $ arrayDimFetch ->dim ->right instanceof Node \Scalar \Int_
5845+ && $ arrayDimFetch ->dim ->right ->value === 1
5846+ && $ scope ->hasExpressionType (new ArrayDimFetch ($ arrayDimFetch ->var , $ arrayDimFetch ->dim ->left ))->yes ()
5847+ ) {
5848+ $ valueToWrite = TypeCombinator::intersect ($ valueToWrite , new AccessoryArrayListType ());
5849+ }
5850+ }
58245851 }
58255852
58265853 return $ valueToWrite ;
0 commit comments