Skip to content

Commit

Permalink
Merge pull request #9651 from tuqqu/array-flip-non-empty-fix
Browse files Browse the repository at this point in the history
Fix `array_flip` to preserve non-empty array type
  • Loading branch information
orklah committed Apr 13, 2023
2 parents cee88f2 + d3a21ac commit 6834f5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stubs/CoreGenericFunctions.phpstub
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -2574,6 +2574,17 @@ function takes_non_empty_int_array(array $input): void {}
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);
',
],
];
}

Expand Down Expand Up @@ -2865,6 +2876,15 @@ function takes_non_empty_list(array $input): void {}
',
'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',
],
];
}
}

0 comments on commit 6834f5d

Please sign in to comment.