diff --git a/src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php b/src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php index 2922f3b8d32..46d028351f4 100644 --- a/src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php @@ -117,7 +117,7 @@ public static function replace( $is_mixed = true; } - $new_types[$template_type_part->getKey()] = $template_type_part; + $new_types[] = $template_type_part; } } } elseif ($atomic_type instanceof Atomic\TTemplateParamClass) { @@ -153,7 +153,7 @@ public static function replace( if ($class_template_type) { $keys_to_unset[] = $key; - $new_types[$class_template_type->getKey()] = $class_template_type; + $new_types[] = $class_template_type; } } elseif ($atomic_type instanceof Atomic\TTemplateIndexedAccess) { $keys_to_unset[] = $key; @@ -194,10 +194,10 @@ public static function replace( $is_mixed = true; } - $new_types[$template_type_part->getKey()] = $template_type_part; + $new_types[] = $template_type_part; } } else { - $new_types[$key] = new Atomic\TMixed(); + $new_types[] = new Atomic\TMixed(); } } elseif ($atomic_type instanceof Atomic\TConditional && $codebase @@ -347,7 +347,7 @@ public static function replace( $keys_to_unset[] = $key; foreach ($class_template_type->getAtomicTypes() as $class_template_atomic_type) { - $new_types[$class_template_atomic_type->getKey()] = $class_template_atomic_type; + $new_types[] = $class_template_atomic_type; } } } @@ -359,7 +359,12 @@ public static function replace( throw new \UnexpectedValueException('This array should be full'); } - $union->replaceTypes($new_types); + $union->replaceTypes( + TypeCombiner::combine( + $new_types, + $codebase + )->getAtomicTypes() + ); return; } diff --git a/tests/Template/ClassTemplateTest.php b/tests/Template/ClassTemplateTest.php index 90398a89b63..5b8824612bc 100644 --- a/tests/Template/ClassTemplateTest.php +++ b/tests/Template/ClassTemplateTest.php @@ -3371,6 +3371,32 @@ function writesLikeAnArray(WeakMap $wm): void { } ', ], + 'combineTwoTemplatedArrays' => [ + 'v : $else; + } + } + + $opt = new Option([1, 3]); + + $b = $opt->getOrElse([2, 4])[0];', + [ + '$b===' => '1|2' + ] + ], ]; }