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 cd0d389
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php
Expand Up @@ -81,7 +81,7 @@ public function __construct()
$this->registerClass(ArraySliceReturnTypeProvider::class);
$this->registerClass(ArraySpliceReturnTypeProvider::class);
$this->registerClass(ArrayReverseReturnTypeProvider::class);
$this->registerClass(ArrayUniqueReturnTypeProvider::class);
//$this->registerClass(ArrayUniqueReturnTypeProvider::class);
$this->registerClass(ArrayFillReturnTypeProvider::class);
$this->registerClass(ArrayFillKeysReturnTypeProvider::class);
$this->registerClass(FilterVarReturnTypeProvider::class);
Expand Down
12 changes: 12 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Expand Up @@ -129,6 +129,18 @@ function array_flip(array $array)
{
}

/**
* @psalm-template TArray as array
*
* @param TArray $array
*
* @return TArray
* @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
17 changes: 17 additions & 0 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -2536,6 +2536,14 @@ function consumeArray(array $_input): void {}
consumeArray([makeKey() => null]);
',
],
'arrayUniquePreservesNonEmptyInput' => [
'code' => '<?php
/** @param non-empty-array<string, object> $input */
function takes_non_empty(array $input): void {}
takes_non_empty(array_unique(["test" => (object)[]]));
',
],
];
}

Expand Down Expand Up @@ -2809,6 +2817,15 @@ 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 $input): void {}
takes_non_empty(array_unique([]));
',
'error_message' => 'InvalidArgument',
],
];
}
}

0 comments on commit cd0d389

Please sign in to comment.