Skip to content

Commit

Permalink
Merge pull request #9323 from othercorey/fix-array-unique
Browse files Browse the repository at this point in the history
Fix array_unique callmap
  • Loading branch information
orklah committed Mar 28, 2023
2 parents 88f7ca0 + 730bc22 commit 94f32e9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 68 deletions.
4 changes: 1 addition & 3 deletions dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@
'array_uintersect_assoc\'1' => ['array', 'array'=>'array', 'rest'=>'array', 'arr3'=>'array', 'arg4'=>'array|callable', '...rest='=>'array|callable(mixed,mixed):int'],
'array_uintersect_uassoc' => ['array', 'array'=>'array', 'rest'=>'array', 'data_compare_func'=>'callable(mixed,mixed):int', 'key_compare_func'=>'callable(mixed,mixed):int'],
'array_uintersect_uassoc\'1' => ['array', 'array'=>'array', 'rest'=>'array', 'arr3'=>'array', 'arg4'=>'array|callable(mixed,mixed):int', 'arg5'=>'array|callable(mixed,mixed):int', '...rest='=>'array|callable(mixed,mixed):int'],
'array_unique' => ['array', 'array'=>'array', 'flags='=>'0'],
'array_unique\'1' => ['array<int|float|string|null>', 'array'=>'array<int|float|string|null>', 'flags='=>'1'],
'array_unique\'2' => ['array<int|float|string|bool|\Stringable|null>', 'array'=>'array<int|float|string|bool|\Stringable|null>', 'flags='=>'2|5'],
'array_unique' => ['array', 'array'=>'array', 'flags='=>'int'],
'array_unshift' => ['int', '&rw_array'=>'array', '...values='=>'mixed'],
'array_values' => ['list<mixed>', 'array'=>'array'],
'array_walk' => ['bool', '&rw_array'=>'array', 'callback'=>'callable', 'arg='=>'mixed'],
Expand Down
4 changes: 1 addition & 3 deletions dictionaries/CallMap_historical.php
Original file line number Diff line number Diff line change
Expand Up @@ -9381,9 +9381,7 @@
'array_uintersect_assoc\'1' => ['array', 'array'=>'array', 'rest'=>'array', 'arr3'=>'array', 'arg4'=>'array|callable', '...rest='=>'array|callable(mixed,mixed):int'],
'array_uintersect_uassoc' => ['array', 'array'=>'array', 'rest'=>'array', 'data_compare_func'=>'callable(mixed,mixed):int', 'key_compare_func'=>'callable(mixed,mixed):int'],
'array_uintersect_uassoc\'1' => ['array', 'array'=>'array', 'rest'=>'array', 'arr3'=>'array', 'arg4'=>'array|callable(mixed,mixed):int', 'arg5'=>'array|callable(mixed,mixed):int', '...rest='=>'array|callable(mixed,mixed):int'],
'array_unique' => ['array', 'array'=>'array', 'flags='=>'0'],
'array_unique\'1' => ['array<int|float|string|null>', 'array'=>'array<int|float|string|null>', 'flags='=>'1'],
'array_unique\'2' => ['array<int|float|string|bool|\Stringable|null>', 'array'=>'array<int|float|string|bool|\Stringable|null>', 'flags='=>'2|5'],
'array_unique' => ['array', 'array'=>'array', 'flags='=>'int'],
'array_unshift' => ['int', '&rw_array'=>'array', '...values'=>'mixed'],
'array_values' => ['list<mixed>', 'array'=>'array'],
'array_walk' => ['bool', '&rw_array'=>'array', 'callback'=>'callable', 'arg='=>'mixed'],
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayReverseReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArraySliceReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArraySpliceReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayUniqueReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\BasenameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\DirnameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FilterVarReturnTypeProvider;
Expand Down Expand Up @@ -81,7 +80,6 @@ public function __construct()
$this->registerClass(ArraySliceReturnTypeProvider::class);
$this->registerClass(ArraySpliceReturnTypeProvider::class);
$this->registerClass(ArrayReverseReturnTypeProvider::class);
$this->registerClass(ArrayUniqueReturnTypeProvider::class);
$this->registerClass(ArrayFillReturnTypeProvider::class);
$this->registerClass(ArrayFillKeysReturnTypeProvider::class);
$this->registerClass(FilterVarReturnTypeProvider::class);
Expand Down

This file was deleted.

14 changes: 14 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ function array_flip(array $array)
{
}

/**
* @psalm-template TKey as array-key
* @psalm-template TValue
* @psalm-template TArray as array<TKey, TValue>
*
* @param TArray $array
*
* @return (TArray is non-empty-array ? non-empty-array<TKey, TValue> : array<TKey, TValue>)
* @psalm-pure
*/
function array_unique(array $array, int $flags = 0)
{
}

/**
* @psalm-template TKey as array-key
* @psalm-template TArray as array<TKey, mixed>
Expand Down
31 changes: 31 additions & 0 deletions tests/ArrayFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,19 @@ function consumeArray(array $_input): void {}
consumeArray([makeKey() => null]);
',
],
'arrayUniquePreservesNonEmptyInput' => [
'code' => '<?php
/** @param non-empty-array<string, object> $input */
function takes_non_empty_array(array $input): void {}
takes_non_empty_array(array_unique(["test" => (object)[]]));
/** @param non-empty-array<int, object> $input */
function takes_non_empty_int_array(array $input): void {}
takes_non_empty_int_array(array_unique([(object)[]]));
',
],
];
}

Expand Down Expand Up @@ -2809,6 +2822,24 @@ function merger(array $a, array $b) : array {
',
'error_message' => 'NamedArgumentNotAllowed',
],
'arrayUniquePreservesEmptyInput' => [
'code' => '<?php
/** @param non-empty-array<string, object> $input */
function takes_non_empty_array(array $input): void {}
takes_non_empty_array(array_unique([]));
',
'error_message' => 'InvalidArgument',
],
'arrayUniqueConvertsListToArray' => [
'code' => '<?php
/** @param non-empty-list<object> $input */
function takes_non_empty_list(array $input): void {}
takes_non_empty_list(array_unique([(object)[]]));
',
'error_message' => 'ArgumentTypeCoercion',
],
];
}
}

0 comments on commit 94f32e9

Please sign in to comment.