Skip to content

Commit

Permalink
Merge pull request #7483 from ElisDN/array-replace
Browse files Browse the repository at this point in the history
Fix `array_replace` type
  • Loading branch information
orklah committed Jan 25, 2022
2 parents f1c4b62 + 9f01c16 commit ea59845
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Expand Up @@ -44,6 +44,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
return Type::getMixed();
}

$is_replace = mb_strcut($event->getFunctionId(), 6, 7) === 'replace';

$inner_value_types = [];
$inner_key_types = [];

Expand Down Expand Up @@ -104,7 +106,11 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

foreach ($unpacked_type_part->properties as $key => $type) {
if (!is_string($key)) {
$generic_properties[] = $type;
if ($is_replace) {
$generic_properties[$key] = $type;
} else {
$generic_properties[] = $type;
}
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -210,9 +210,9 @@ function getInts(): array{ return []; }
],
'arrayMergeIntArrays' => [
'<?php
$d = array_merge(["a", "b", "c"], [1, 2, 3]);',
$d = array_merge(["a", "b", "c", "d"], [1, 2, 3]);',
'assertions' => [
'$d' => 'array{0: string, 1: string, 2: string, 3: int, 4: int, 5: int}',
'$d' => 'array{0: string, 1: string, 2: string, 3: string, 4: int, 5: int, 6: int}',
],
],
'arrayMergePossiblyUndefined' => [
Expand Down Expand Up @@ -266,9 +266,9 @@ public function merge($a, $b): array
],
'arrayReplaceIntArrays' => [
'<?php
$d = array_replace(["a", "b", "c"], [1, 2, 3]);',
$d = array_replace(["a", "b", "c", "d"], [1, 2, 3]);',
'assertions' => [
'$d' => 'array{0: string, 1: string, 2: string, 3: int, 4: int, 5: int}',
'$d' => 'array{0: int, 1: int, 2: int, 3: string}',
],
],
'arrayReplacePossiblyUndefined' => [
Expand Down

0 comments on commit ea59845

Please sign in to comment.