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 sort assert annotation #7908

Merged
merged 14 commits into from
Apr 28, 2022
30 changes: 18 additions & 12 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -186,40 +186,44 @@ function array_search($needle, array $haystack, bool $strict = false)

/**
* @psalm-template T
* @psalm-template TArray as array<T>
*
* @param T[] $array
* @param-out list<T> $array
* @param TArray $array
* @param-out (TArray is non-empty-array ? non-empty-list<T> : list<T>) $array
*/
function shuffle(array &$array): bool
{
}

/**
* @psalm-template T
* @psalm-template TArray as array<T>
*
* @param T[] $array
* @param-out list<T> $array
* @param TArray $array
* @param-out (TArray is non-empty-array ? non-empty-list<T> : list<T>) $array
*/
function sort(array &$array, int $flags = SORT_REGULAR): bool
{
}

/**
* @psalm-template T
* @psalm-template TArray as array<T>
*
* @param T[] $array
* @param-out list<T> $array
* @param TArray $array
* @param-out (TArray is non-empty-array ? non-empty-list<T> : list<T>) $array
*/
function rsort(array &$array, int $flags = SORT_REGULAR): bool
{
}

/**
* @psalm-template T
* @psalm-template TArray as array<T>
*
* @param T[] $array
* @param TArray $array
* @param callable(T,T):int $callback
* @param-out list<T> $array
* @param-out (TArray is non-empty-array ? non-empty-list<T> : list<T>) $array
*/
function usort(array &$array, callable $callback): bool
{
Expand All @@ -228,10 +232,11 @@ function usort(array &$array, callable $callback): bool
/**
* @psalm-template TKey
* @psalm-template T
* @psalm-template TArray as array<TKey,T>
*
* @param array<TKey,T> $array
* @param TArray $array
* @param callable(T,T):int $callback
* @param-out array<TKey,T> $array
* @param-out (TArray is non-empty-array ? non-empty-array<TKey,T> : array<TKey,T>) $array
*/
function uasort(array &$array, callable $callback): bool
{
Expand All @@ -240,10 +245,11 @@ function uasort(array &$array, callable $callback): bool
/**
* @psalm-template TKey
* @psalm-template T
* @psalm-template TArray as array<TKey,T>
*
* @param array<TKey,T> $array
* @param TArray $array
* @param callable(TKey,TKey):int $callback
* @param-out array<TKey,T> $array
* @param-out (TArray is non-empty-array ? non-empty-array<TKey,T> : array<TKey,T>) $array
*/
function uksort(array &$array, callable $callback): bool
{
Expand Down
5 changes: 4 additions & 1 deletion tests/ArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ class Hello {}
ksort($a);
$b = ["b" => 5, "a" => 8];
sort($b);
$c = [];
sort($c);
',
'assertions' => [
'$a' => 'array{a: int, b: int}',
'$b' => 'list<int>',
'$b' => 'non-empty-list<int>',
'$c' => 'list<empty>',
],
],
'arrayModificationFunctions' => [
Expand Down
62 changes: 45 additions & 17 deletions tests/ArrayFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,28 +666,43 @@ function foo(array $arr) {
],
'uasort' => [
'<?php
function foo (int $a, int $b): int {
return $a > $b ? 1 : -1;
}
$manifest = ["a" => 1, "b" => 2];
uasort(
$manifest,
function (int $a, int $b) {
return $a > $b ? 1 : -1;
}
);',
"foo"
);
$emptyManifest = [];
uasort(
$emptyManifest,
"foo"
);
',
'assertions' => [
'$manifest' => 'array<string, int>'
'$manifest' => 'non-empty-array<string, int>',
'$emptyManifest' => 'array<empty, empty>',
],
],
'uksort' => [
'<?php
function foo (string $a, string $b): int {
return $a <=> $b;
}
$array = ["b" => 1, "a" => 2];
uksort(
$array,
function (string $a, string $b) {
return $a <=> $b;
}
"foo"
);
$emptyArray = [];
uksort(
$emptyArray,
"foo"
);',
'assertions' => [
'$array' => 'array<string, int>',
'$array' => 'non-empty-array<string, int>',
'$emptyArray' => 'array<empty, empty>',
],
],
'arrayMergeTKeyedArray' => [
Expand Down Expand Up @@ -1762,33 +1777,46 @@ function getCharPairs(string $line) : array {
'shuffle' => [
'<?php
$array = ["foo" => 123, "bar" => 456];
shuffle($array);',
shuffle($array);
$emptyArray = [];
shuffle($emptyArray);',
'assertions' => [
'$array' => 'list<int>',
'$array' => 'non-empty-list<int>',
'$emptyArray' => 'list<empty>',
],
],
'sort' => [
'<?php
$array = ["foo" => 123, "bar" => 456];
sort($array);',
sort($array);
$emptyArray = [];
sort($emptyArray);',
'assertions' => [
'$array' => 'list<int>',
'$array' => 'non-empty-list<int>',
'$emptyArray' => 'list<empty>',
],
],
'rsort' => [
'<?php
$array = ["foo" => 123, "bar" => 456];
sort($array);',
rsort($array);
$emptyArray = [];
rsort($emptyArray);',
'assertions' => [
'$array' => 'list<int>',
'$array' => 'non-empty-list<int>',
'$emptyArray' => 'list<empty>',
],
],
'usort' => [
'<?php
function baz (int $a, int $b): int { return $a <=> $b; }
$array = ["foo" => 123, "bar" => 456];
usort($array, function (int $a, int $b) { return $a <=> $b; });',
usort($array, "baz");
$emptyArray = [];
usort($emptyArray, "baz");',
'assertions' => [
'$array' => 'list<int>',
'$array' => 'non-empty-list<int>',
'$emptyArray' => 'list<empty>',
],
],
'closureParamConstraintsMet' => [
Expand Down