Skip to content

Commit

Permalink
fix strict type ignored when mapping or filtering arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed May 19, 2021
1 parent 963a0bb commit 20846c9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/Psl/Dict/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ function filter(iterable $iterable, ?callable $predicate = null): array
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');

if (is_array($iterable)) {
return array_filter($iterable, $predicate);
return array_filter(
$iterable,
/**
* @param Tv $v
*/
static fn($v): bool => $predicate($v)
);
}

$result = [];
Expand Down
9 changes: 8 additions & 1 deletion src/Psl/Dict/filter_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ function filter_keys(iterable $iterable, ?callable $predicate = null): array
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');

if (is_array($iterable)) {
return array_filter($iterable, $predicate, ARRAY_FILTER_USE_KEY);
return array_filter(
$iterable,
/**
* @param Tk $k
*/
static fn($k): bool => $predicate($k),
ARRAY_FILTER_USE_KEY
);
}

$result = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Iter/contains_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @template Tk
* @template Tv
*
* @param iterable<Tk, Tv> $iterable,
* @param iterable<Tk, Tv> $iterable
* @param Tk $key
*/
function contains_key(iterable $iterable, $key): bool
Expand Down
14 changes: 14 additions & 0 deletions src/Psl/Vec/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use Closure;

use function array_filter;
use function array_values;
use function is_array;

/**
* Returns a vec containing only the values for which the given predicate
* returns `true`.
Expand All @@ -31,6 +35,16 @@ function filter(iterable $iterable, ?callable $predicate = null): array
{
/** @var (callable(T): bool) $predicate */
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');
if (is_array($iterable)) {
return array_values(array_filter(
$iterable,
/**
* @param T $t
*/
static fn($t): bool => $predicate($t)
));
}

$result = [];
foreach ($iterable as $v) {
if ($predicate($v)) {
Expand Down
17 changes: 17 additions & 0 deletions src/Psl/Vec/filter_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use Closure;

use function array_filter;
use function array_values;
use function is_array;

use const ARRAY_FILTER_USE_KEY;

/**
* Returns a vec containing only the values for which the given predicate
* returns `true`.
Expand All @@ -32,6 +38,17 @@ function filter_keys(iterable $iterable, ?callable $predicate = null): array
{
/** @var (callable(Tk): bool) $predicate */
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');
if (is_array($iterable)) {
return array_values(array_filter(
$iterable,
/**
* @param Tk $t
*/
static fn($t): bool => $predicate($t),
ARRAY_FILTER_USE_KEY
));
}

$result = [];
foreach ($iterable as $k => $v) {
if ($predicate($k)) {
Expand Down
17 changes: 8 additions & 9 deletions src/Psl/Vec/filter_nulls.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
*/
function filter_nulls(iterable $iterable): array
{
/** @var list<T> $result */
$result = [];
foreach ($iterable as $value) {
if (null !== $value) {
$result[] = $value;
}
}

return $result;
/** @var list<T> */
return filter(
$iterable,
/**
* @param T|null $value
*/
static fn($value): bool => null !== $value
);
}
18 changes: 18 additions & 0 deletions src/Psl/Vec/filter_with_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use Psl;

use function array_filter;
use function array_values;
use function is_array;

use const ARRAY_FILTER_USE_BOTH;

/**
* Returns a vec containing only the values for which the given predicate
* returns `true`.
Expand Down Expand Up @@ -40,6 +46,18 @@ function filter_with_key(iterable $iterable, ?callable $predicate = null): array
*/
static fn ($_k, $v): bool => Psl\Internal\boolean($v);

if (is_array($iterable)) {
return array_values(array_filter(
$iterable,
/**
* @param Tv $v
* @param Tk $k
*/
static fn($v, $k): bool => $predicate($k, $v),
ARRAY_FILTER_USE_BOTH
));
}

$result = [];
foreach ($iterable as $k => $v) {
if ($predicate($k, $v)) {
Expand Down
14 changes: 14 additions & 0 deletions src/Psl/Vec/map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Psl\Vec;

use function array_map;
use function array_values;
use function is_array;

/**
* Applies a mapping function to all values of an iterable.
*
Expand All @@ -26,6 +30,16 @@
*/
function map(iterable $iterable, callable $function): array
{
if (is_array($iterable)) {
return array_values(array_map(
/**
* @param Tv $v
*/
static fn($v) => $function($v),
$iterable
));
}

$result = [];
foreach ($iterable as $value) {
$result[] = $function($value);
Expand Down

0 comments on commit 20846c9

Please sign in to comment.