Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array_flip to preserve non-empty array type #9651

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2549,6 +2549,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 @@ -2840,6 +2851,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',
],
];
}
}