1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #2235 - improve behaviour of array_merge on lists

This commit is contained in:
Matthew Brown 2019-10-20 15:47:00 -04:00
parent 1d6ca9bd7d
commit b476761109
2 changed files with 23 additions and 3 deletions

View File

@ -33,7 +33,7 @@ class ArrayMergeReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturnT
$codebase = $statements_source->getCodebase();
$generic_properties = [];
$all_lists = true;
$all_int_offsets = true;
$all_nonempty_lists = true;
foreach ($call_args as $call_arg) {
@ -102,7 +102,9 @@ class ArrayMergeReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturnT
continue;
}
$all_lists = false;
if (!$unpacked_type_part->type_params[0]->isInt()) {
$all_int_offsets = false;
}
}
$inner_key_types = array_merge(
@ -130,7 +132,7 @@ class ArrayMergeReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturnT
if ($inner_value_types) {
$inner_value_type = TypeCombination::combineTypes($inner_value_types, $codebase, true);
if ($all_lists) {
if ($all_int_offsets) {
if ($all_nonempty_lists) {
return new Type\Union([
new Type\Atomic\TNonEmptyList($inner_value_type),

View File

@ -240,6 +240,24 @@ class FunctionCallTest extends TestCase
'$d' => 'array{0: string, 1: string, 2: string, 3: int, 4: int, 5: int}',
],
],
'arrayMergeListResult' => [
'<?php
/**
* @param list<string> $list
* @return list<string>
*/
function foo(array $list) : array {
return array_merge($list, ["test"]);
}
/**
* @param array<int, string> $list
* @return list<string>
*/
function bar(array $list) : array {
return array_merge($list, ["test"]);
}',
],
'arrayReverseDontPreserveKey' => [
'<?php
$d = array_reverse(["a", "b", 1, "d" => 4]);',