Skip to content

Commit

Permalink
Fix #2235 - improve behaviour of array_merge on lists
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 20, 2019
1 parent 1d6ca9b commit b476761
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Expand Up @@ -33,7 +33,7 @@ public static function getFunctionReturnType(
$codebase = $statements_source->getCodebase();

$generic_properties = [];
$all_lists = true;
$all_int_offsets = true;
$all_nonempty_lists = true;

foreach ($call_args as $call_arg) {
Expand Down Expand Up @@ -102,7 +102,9 @@ public static function getFunctionReturnType(
continue;
}

$all_lists = false;
if (!$unpacked_type_part->type_params[0]->isInt()) {
$all_int_offsets = false;
}
}

$inner_key_types = array_merge(
Expand Down Expand Up @@ -130,7 +132,7 @@ public static function getFunctionReturnType(
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),
Expand Down
18 changes: 18 additions & 0 deletions tests/FunctionCallTest.php
Expand Up @@ -240,6 +240,24 @@ function foo() { }
'$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]);',
Expand Down

0 comments on commit b476761

Please sign in to comment.