1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Keep generic part of shape on array_map return type

Fixes vimeo/psalm#6038
This commit is contained in:
Bruce Weirdan 2021-07-03 06:24:18 +03:00
parent 78f3e81b58
commit 6db79be207
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 22 additions and 0 deletions

View File

@ -187,6 +187,8 @@ class ArrayMapReturnTypeProvider implements \Psalm\Plugin\EventHandler\FunctionR
);
$atomic_type->is_list = $array_arg_atomic_type->is_list;
$atomic_type->sealed = $array_arg_atomic_type->sealed;
$atomic_type->previous_key_type = $array_arg_atomic_type->previous_key_type;
$atomic_type->previous_value_type = $mapping_return_type;
return new Type\Union([$atomic_type]);
}

View File

@ -1930,6 +1930,26 @@ class ArrayFunctionCallTest extends TestCase
);
}'
],
'arrayMapShapeAndGenericArray' => [
'<?php
/** @return string[] */
function getLine(): array { return ["a", "b"]; }
$line = getLine();
if (empty($line[0])) { // converts array<string> to array{0:string}<string>
throw new InvalidArgumentException;
}
$line = array_map( // should not destroy <string> part
function($val) { return (int)$val; },
$line
);
',
'assertions' => [
'$line===' => 'array{0: int}<array-key, int>',
],
],
];
}