Skip to content

Commit

Permalink
Merge pull request #7908 from hirokinoue/fix-sort-assert-annotation
Browse files Browse the repository at this point in the history
Fix sort assert annotation
  • Loading branch information
orklah authored Apr 28, 2022
2 parents 9c153de + 4bb5c38 commit f1fe6ff
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
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

0 comments on commit f1fe6ff

Please sign in to comment.