Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,54 @@ function array_key_last($array)
{
}

/**
* @psalm-template TArray as array
*
* @param TArray $array
*
* @return (TArray is array<never, never> ? null : (TArray is non-empty-array ? value-of<TArray> : value-of<TArray>|null))
* @psalm-pure
*/
function array_first($array)
{
}

/**
* @psalm-template TArray as array
*
* @param TArray $array
*
* @return (TArray is array<never, never> ? null : (TArray is non-empty-array ? value-of<TArray> : value-of<TArray>|null))
* @psalm-pure
*/
function array_last($array)
{
}

/**
* @psalm-template TKey of array-key
* @psalm-template TValue
*
* @param array<TKey, TValue> $array
* @param callable(TValue, TKey): bool $callback
* @psalm-pure
Comment thread
delolmo marked this conversation as resolved.
*/
function array_any(array $array, callable $callback): bool
{
}

/**
* @psalm-template TKey of array-key
* @psalm-template TValue
*
* @param array<TKey, TValue> $array
* @param callable(TValue, TKey): bool $callback
* @psalm-pure
*/
function array_all(array $array, callable $callback): bool
{
}

/**
* @psalm-template TArray as array
*
Expand Down
68 changes: 68 additions & 0 deletions tests/ArrayFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,74 @@ function makeArray(): array { return ["one" => 1, "two" => 3]; }
'$b' => 'null',
],
],
'arrayFirst' => [
'code' => '<?php
/** @return array<string, int> */
function makeArray(): array { return ["one" => 1, "two" => 3]; }
$a = makeArray();
$b = array_first($a);',
'assertions' => [
'$b' => 'int|null',
],
],
'arrayFirstNonEmpty' => [
'code' => '<?php
$a = ["one" => 1, "two" => 3];
$b = array_first($a);',
'assertions' => [
'$b' => 'int',
],
],
'arrayFirstEmpty' => [
'code' => '<?php
$a = [];
$b = array_first($a);',
'assertions' => [
'$b' => 'null',
],
],
'arrayLast' => [
'code' => '<?php
/** @return array<string, int> */
function makeArray(): array { return ["one" => 1, "two" => 3]; }
$a = makeArray();
$b = array_last($a);',
'assertions' => [
'$b' => 'int|null',
],
],
'arrayLastNonEmpty' => [
'code' => '<?php
$a = ["one" => 1, "two" => 3];
$b = array_last($a);',
'assertions' => [
'$b' => 'int',
],
],
'arrayLastEmpty' => [
'code' => '<?php
$a = [];
$b = array_last($a);',
'assertions' => [
'$b' => 'null',
],
],
'arrayAnyReturnsBoolWithTypedCallback' => [
'code' => '<?php
$a = ["one" => 1, "two" => 3];
$b = array_any($a, fn (int $value, string $key): bool => $value > 1);',
'assertions' => [
'$b' => 'bool',
],
],
'arrayAllReturnsBoolWithTypedCallback' => [
'code' => '<?php
$a = ["one" => 1, "two" => 3];
$b = array_all($a, fn (int $value, string $key): bool => $value > 1);',
'assertions' => [
'$b' => 'bool',
],
],
'arrayResetNonEmptyArray' => [
'code' => '<?php
/** @return non-empty-array<string, int> */
Expand Down
Loading