mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #541 - make array_map on object-like more accurate
This commit is contained in:
parent
9d165bee7b
commit
93b714d7c8
@ -369,6 +369,23 @@ class FunctionChecker extends FunctionLikeChecker
|
||||
|
||||
$inner_type = clone $closure_return_type;
|
||||
|
||||
if ($array_arg_type instanceof Type\Atomic\ObjectLike && count($call_args) === 2) {
|
||||
return new Type\Union([
|
||||
new Type\Atomic\ObjectLike(
|
||||
array_map(
|
||||
/**
|
||||
* @return Type\Union
|
||||
* @psalm-suppress UnusedParam
|
||||
*/
|
||||
function(Type\Union $t) use ($inner_type) {
|
||||
return clone $inner_type;
|
||||
},
|
||||
$array_arg_type->properties
|
||||
)
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
return new Type\Union([
|
||||
new Type\Atomic\TArray([
|
||||
$generic_key_type,
|
||||
@ -459,6 +476,23 @@ class FunctionChecker extends FunctionLikeChecker
|
||||
}
|
||||
|
||||
if ($mapping_return_type) {
|
||||
if ($array_arg_type instanceof Type\Atomic\ObjectLike && count($call_args) === 2) {
|
||||
return new Type\Union([
|
||||
new Type\Atomic\ObjectLike(
|
||||
array_map(
|
||||
/**
|
||||
* @return Type\Union
|
||||
* @psalm-suppress UnusedParam
|
||||
*/
|
||||
function(Type\Union $t) use ($mapping_return_type) {
|
||||
return clone $mapping_return_type;
|
||||
},
|
||||
$array_arg_type->properties
|
||||
)
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
return new Type\Union([
|
||||
new Type\Atomic\TArray([
|
||||
$generic_key_type,
|
||||
|
@ -139,12 +139,12 @@ class ClosureTest extends TestCase
|
||||
$e = array_map([$a_instance, "bar"], ["one", "two"]);
|
||||
$f = array_map("baz", ["one", "two"]);',
|
||||
'assertions' => [
|
||||
'$a' => 'array<int, string>',
|
||||
'$b' => 'array<int, string>',
|
||||
'$c' => 'array<int, string>',
|
||||
'$d' => 'array<int, string>',
|
||||
'$e' => 'array<int, string>',
|
||||
'$f' => 'array<int, string>',
|
||||
'$a' => 'array{0:string, 1:string}',
|
||||
'$b' => 'array{0:string, 1:string}',
|
||||
'$c' => 'array{0:string, 1:string}',
|
||||
'$d' => 'array{0:string, 1:string}',
|
||||
'$e' => 'array{0:string, 1:string}',
|
||||
'$f' => 'array{0:string, 1:string}',
|
||||
],
|
||||
],
|
||||
'arrayCallableMethod' => [
|
||||
|
Loading…
Reference in New Issue
Block a user