1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix array_merge edge case

This commit is contained in:
Daniil Gentili 2022-12-16 18:10:46 +01:00
parent 5132de22e0
commit 992f26a8e7
2 changed files with 29 additions and 3 deletions

View File

@ -130,10 +130,18 @@ class ArrayMergeReturnTypeProvider implements FunctionReturnTypeProviderInterfac
$class_strings[$key] = true; $class_strings[$key] = true;
} }
if (!isset($generic_properties[$key]) || !$type->possibly_undefined) { if (!isset($generic_properties[$key]) || (
!$type->possibly_undefined
&& !$unpacking_possibly_empty
&& $is_replace
)) {
if ($unpacking_possibly_empty) {
$type = $type->setPossiblyUndefined(true);
}
$generic_properties[$key] = $type; $generic_properties[$key] = $type;
} else { } else {
$was_possibly_undefined = $generic_properties[$key]->possibly_undefined; $was_possibly_undefined = $generic_properties[$key]->possibly_undefined
|| $unpacking_possibly_empty;
$generic_properties[$key] = Type::combineUnionTypes( $generic_properties[$key] = Type::combineUnionTypes(
$generic_properties[$key], $generic_properties[$key],
@ -147,7 +155,7 @@ class ArrayMergeReturnTypeProvider implements FunctionReturnTypeProviderInterfac
} }
} }
if (!$unpacked_type_part->is_list && !$unpacking_possibly_empty) { if (!$unpacked_type_part->is_list) {
$all_nonempty_lists = false; $all_nonempty_lists = false;
} }

View File

@ -211,6 +211,24 @@ class ArrayFunctionCallTest extends TestCase
'ignored_issues' => [], 'ignored_issues' => [],
'php_version' => '8.0', 'php_version' => '8.0',
], ],
'arrayMergeListOfShapes' => [
'code' => '<?php
/** @var list<array{a: int}> */
$a = [];
$b = array_merge(...$a);
/** @var non-empty-list<array{a: int}> */
$c = [];
$d = array_merge(...$c);
',
'assertions' => [
'$b' => 'array{a?: int}',
'$d' => 'array{a: int}',
]
],
'arrayMergeIntArrays' => [ 'arrayMergeIntArrays' => [
'code' => '<?php 'code' => '<?php
$d = array_merge(["a", "b", "c", "d"], [1, 2, 3]);', $d = array_merge(["a", "b", "c", "d"], [1, 2, 3]);',