Skip to content
Permalink
Browse files

Fix #2472 - array_map should retain non-emptiness when passed string …

…callables
  • Loading branch information
muglug committed Dec 15, 2019
1 parent 3ef93fd commit 1d0ac3df2908b09aac53022104da1110c18fdaf4
@@ -266,13 +266,38 @@ function (Type\Union $_) use ($mapping_return_type) {
return new Type\Union([$atomic_type]);
}

return new Type\Union([
count($call_args) === 2 && !($array_arg_type->is_list ?? false)
? new Type\Atomic\TArray([
if ($array_arg_atomic_type instanceof Type\Atomic\TList
|| count($call_args) !== 2
) {
if ($array_arg_atomic_type instanceof Type\Atomic\TNonEmptyList) {
return new Type\Union([
new Type\Atomic\TNonEmptyList(
$generic_key_type
),
]);
}

return new Type\Union([
new Type\Atomic\TList(
$generic_key_type
),
]);
}

if ($array_arg_atomic_type instanceof Type\Atomic\TNonEmptyArray) {
return new Type\Union([
new Type\Atomic\TNonEmptyArray([
$generic_key_type,
$mapping_return_type,
])
: new Type\Atomic\TList($mapping_return_type)
$inner_type,
]),
]);
}

return new Type\Union([
new Type\Atomic\TArray([
$generic_key_type,
$mapping_return_type,
])
]);
}
}
@@ -2310,6 +2310,16 @@ function getSize(): int { return random_int(1, 10); }
'$result' => 'list<array<array-key, mixed>>',
],
],
'arrayMapPreserveNonEmptiness' => [
'<?php
/**
* @psalm-param non-empty-list<string> $strings
* @psalm-return non-empty-list<int>
*/
function foo(array $strings): array {
return array_map("intval", $strings);
}'
],
];
}

0 comments on commit 1d0ac3d

Please sign in to comment.
You can’t perform that action at this time.