Skip to content

Commit

Permalink
Re-add array_unique() stub which preserved array type
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Feb 20, 2023
1 parent c5ad55d commit 9033dd8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
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
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
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 9033dd8

Please sign in to comment.