@@ -1499,18 +1499,45 @@ public function getPlusTypeFromTypes(Expr $left, Expr $right, Type $leftType, Ty
14991499 $ keyType = TypeCombinator::union (...$ keyTypes );
15001500 }
15011501
1502+ $ leftIterableValueType = $ leftType ->getIterableValueType ();
15021503 $ arrayType = new ArrayType (
15031504 $ keyType ,
1504- TypeCombinator::union ($ leftType -> getIterableValueType () , $ rightType ->getIterableValueType ()),
1505+ TypeCombinator::union ($ leftIterableValueType , $ rightType ->getIterableValueType ()),
15051506 );
15061507
15071508 $ accessories = [];
1508- foreach ($ leftType ->getConstantArrays () as $ type ) {
1509- foreach ($ type ->getKeyTypes () as $ i => $ offsetType ) {
1510- if ($ type ->isOptionalKey ($ i )) {
1509+ if ($ leftCount > 0 ) {
1510+ // Use the first constant array as a reference to list potential offsets.
1511+ // We only need to check the first array because we're looking for offsets that exist in ALL arrays.
1512+ $ constantArray = $ leftConstantArrays [0 ];
1513+ foreach ($ constantArray ->getKeyTypes () as $ i => $ offsetType ) {
1514+ if ($ constantArray ->isOptionalKey ($ i )) {
15111515 continue ;
15121516 }
1513- $ valueType = $ type ->getValueTypes ()[$ i ];
1517+
1518+ if (!$ leftType ->hasOffsetValueType ($ offsetType )->yes ()) {
1519+ continue ;
1520+ }
1521+
1522+ $ valueType = $ leftType ->getOffsetValueType ($ offsetType );
1523+ $ accessories [] = new HasOffsetValueType ($ offsetType , $ valueType );
1524+ }
1525+ }
1526+
1527+ if ($ rightCount > 0 ) {
1528+ // Use the first constant array as a reference to list potential offsets.
1529+ // We only need to check the first array because we're looking for offsets that exist in ALL arrays.
1530+ $ constantArray = $ rightConstantArrays [0 ];
1531+ foreach ($ constantArray ->getKeyTypes () as $ i => $ offsetType ) {
1532+ if ($ constantArray ->isOptionalKey ($ i )) {
1533+ continue ;
1534+ }
1535+
1536+ if (!$ rightType ->hasOffsetValueType ($ offsetType )->yes ()) {
1537+ continue ;
1538+ }
1539+
1540+ $ valueType = TypeCombinator::union ($ leftIterableValueType , $ rightType ->getOffsetValueType ($ offsetType ));
15141541 $ accessories [] = new HasOffsetValueType ($ offsetType , $ valueType );
15151542 }
15161543 }
0 commit comments