1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix array_flip to preserve non-empty array type

This commit is contained in:
tuqqu 2023-04-13 22:19:44 +02:00
parent cb6508e63c
commit d3a21acc24
2 changed files with 21 additions and 1 deletions

View File

@ -122,7 +122,7 @@ function array_diff_assoc(array $array, array ...$arrays)
*
* @param array<TKey, TValue> $array
*
* @return array<TValue, TKey>
* @return ($array is non-empty-array ? non-empty-array<TValue, TKey> : array<TValue, TKey>)
* @psalm-pure
*/
function array_flip(array $array)

View File

@ -2549,6 +2549,17 @@ class ArrayFunctionCallTest extends TestCase
takes_non_empty_int_array(array_unique([(object)[]]));
',
],
'arrayFlipPreservesNonEmptyInput' => [
'code' => '<?php
/** @param non-empty-array<string, int> $input */
function takes_non_empty_array(array $input): void {}
$array = ["hi", "there"];
$flipped = array_flip($array);
takes_non_empty_array($flipped);
',
],
];
}
@ -2840,6 +2851,15 @@ class ArrayFunctionCallTest extends TestCase
',
'error_message' => 'ArgumentTypeCoercion',
],
'arrayFlipPreservesEmptyInput' => [
'code' => '<?php
/** @param non-empty-array<string, int> $input */
function takes_non_empty_array(array $input): void {}
takes_non_empty_array(array_flip([]));
',
'error_message' => 'InvalidArgument',
],
];
}
}