Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8806 #8809

Merged
merged 3 commits into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,7 +38,6 @@
use function array_shift;
use function array_slice;
use function array_values;
use function assert;
use function count;
use function explode;
use function in_array;
Expand Down Expand Up @@ -79,6 +78,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
array_shift($call_args);

$array_arg_types = [];
$orig_types = [];

foreach ($call_args as $call_arg) {
$call_arg_type = $statements_source->node_data->getType($call_arg->value);
Expand All @@ -89,11 +89,23 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
&& $call_arg_atomic->fallback_params === null
) {
$array_arg_types []= array_values($call_arg_atomic->properties);
$orig_types []= $call_arg_type;
} elseif ($call_arg_type
&& $call_arg_type->isSingle()
&& ($call_arg_atomic = $call_arg_type->getSingleAtomic()) instanceof TArray
&& $call_arg_atomic->isEmptyArray()
) {
$array_arg_types []= [];
$orig_types []= $call_arg_type;
} else {
return Type::getArray();
}
}

if (count($orig_types) === 1) {
return $orig_types[0];
}

$null = Type::getNull();
$array_arg_types = array_map(null, ...$array_arg_types);
$array_arg_types = array_map(
Expand All @@ -107,7 +119,10 @@ function (array $sub) use ($null) {
},
$array_arg_types
);
assert(count($array_arg_types));

if (!$array_arg_types) {
return Type::getEmptyArray();
}

return new Union([new TKeyedArray($array_arg_types, null, null, true)]);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -2063,6 +2063,22 @@ function takesString(string $string): void {}
'ignored_issues' => [],
'php_version' => '7.4',
],
'arrayMapMoreZip' => [
'code' => '<?php
$a = array_map(null, []);
$b = array_map(null, [1]);
$c = array_map(null, ["test" => 1]);
$d = array_map(null, [], []);
',
'assertions' => [
'$a===' => 'array<never, never>',
'$b===' => 'list{1}',
'$c===' => 'array{test: 1}',
'$d===' => 'array<never, never>',
],
'ignored_issues' => [],
'php_version' => '7.4',
],
'arrayMapExplicitZip' => [
'code' => '<?php
$as = ["key"];
Expand Down