1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Merge pull request #9651 from tuqqu/array-flip-non-empty-fix

Fix `array_flip` to preserve non-empty array type
This commit is contained in:
orklah 2023-04-13 22:54:12 +02:00 committed by GitHub
commit 6834f5dca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -2574,6 +2574,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);
',
],
];
}
@ -2865,6 +2876,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',
],
];
}
}