@@ -947,10 +947,11 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
947947 $ originalValueToWrite = $ valueToWrite ;
948948
949949 $ offsetValueTypeStack = [$ offsetValueType ];
950+ $ overwrites = true ;
950951 foreach (array_slice ($ offsetTypes , 0 , -1 ) as [$ offsetType , $ dimFetch ]) {
951952 if ($ offsetType === null ) {
952953 $ offsetValueType = new ConstantArrayType ([], []);
953-
954+ $ overwrites = false ;
954955 } else {
955956 $ has = $ offsetValueType ->hasOffsetValueType ($ offsetType );
956957 if ($ has ->yes ()) {
@@ -959,9 +960,11 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
959960 if (!$ scope ->hasExpressionType ($ dimFetch )->yes ()) {
960961 $ offsetValueType = TypeCombinator::union ($ offsetValueType ->getOffsetValueType ($ offsetType ), new ConstantArrayType ([], []));
961962 } else {
963+ $ overwrites = false ;
962964 $ offsetValueType = $ offsetValueType ->getOffsetValueType ($ offsetType );
963965 }
964966 } else {
967+ $ overwrites = false ;
965968 $ offsetValueType = new ConstantArrayType ([], []);
966969 }
967970 }
@@ -1010,7 +1013,14 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
10101013 }
10111014
10121015 } else {
1013- $ valueToWrite = $ offsetValueType ->setOffsetValueType ($ offsetType , $ valueToWrite , $ i === 0 );
1016+ $ unionValues = false ;
1017+ if ($ i === 0 ) {
1018+ $ unionValues = true ;
1019+ } elseif ($ overwrites === true && $ i === count ($ offsetTypes ) - 1 ) {
1020+ $ unionValues = true ;
1021+ }
1022+
1023+ $ valueToWrite = $ offsetValueType ->setOffsetValueType ($ offsetType , $ valueToWrite , $ unionValues );
10141024 }
10151025
10161026 if ($ arrayDimFetch === null || !$ offsetValueType ->isList ()->yes ()) {
0 commit comments