1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Support non-empty-arrays in array_keys (#3168)

Closes #3160
This commit is contained in:
Valentin Udaltsov 2020-04-17 22:07:41 +03:00 committed by Brown
parent d54f9bca17
commit 9b8e8ab964
2 changed files with 24 additions and 6 deletions

View File

@ -1,12 +1,13 @@
<?php
/**
* @psalm-template T as array-key
* @psalm-template A as non-empty-array<T, mixed>|array<T, mixed>
*
* @param array<T, mixed> $arr
* @param mixed $search_value
* @param bool $strict
* @param A $arr
* @param mixed $search_value
* @param bool $strict
*
* @return list<T>
* @return (A is non-empty-array ? non-empty-list<T> : list<T>)
* @psalm-pure
*/
function array_keys(array $arr, $search_value = null, bool $strict = false)

View File

@ -93,7 +93,7 @@ class FunctionTemplateTest extends TestCase
* @template T as array-key
*
* @param array<T, mixed> $arr
* @return array<int, T>
* @return list<T>
*/
function my_array_keys($arr) {
return array_keys($arr);
@ -101,7 +101,24 @@ class FunctionTemplateTest extends TestCase
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
'assertions' => [
'$a' => 'array<int, string>',
'$a' => 'list<string>',
],
],
'genericNonEmptyArrayKeys' => [
'<?php
/**
* @template T as array-key
*
* @param non-empty-array<T, mixed> $arr
* @return non-empty-list<T>
*/
function my_array_keys($arr) {
return array_keys($arr);
}
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
'assertions' => [
'$a' => 'non-empty-list<string>',
],
],
'genericArrayFlip' => [