Skip to content

Commit

Permalink
Merge aae374d into 75e3b94
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Feb 15, 2021
2 parents 75e3b94 + aae374d commit 2e333dc
Show file tree
Hide file tree
Showing 115 changed files with 2,298 additions and 374 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Expand Up @@ -22,4 +22,4 @@ jobs:
run: "composer update --no-interaction --no-progress"

- name: "running static analysis ( psalm )"
run: "vendor/bin/psalm --output-format=github --shepherd --stats"
run: "vendor/bin/psalm --shepherd --stats"
9 changes: 6 additions & 3 deletions src/Psl/Arr/drop.php
Expand Up @@ -5,6 +5,7 @@
namespace Psl\Arr;

use Psl;
use Psl\Dict;

/**
* Drops the first n items from an array.
Expand All @@ -24,9 +25,11 @@
*
* @throws Psl\Exception\InvariantViolationException If the $n is negative
*
* @psalm-pure
* @deprecated use `Dict\drop` instead.
*
* @see Dict\drop()
*/
function drop(array $array, int $n): array
function drop(iterable $array, int $n): array
{
return slice($array, $n);
return Dict\drop($array, $n);
}
20 changes: 7 additions & 13 deletions src/Psl/Arr/drop_while.php
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Arr;

use Psl\Dict;

/**
* Drops items from an iterable until the predicate fails for the first time.
*
Expand All @@ -22,20 +24,12 @@
* @psalm-param (callable(Tv): bool) $predicate
*
* @psalm-return array<Tk, Tv>
*
* @deprecated use `Dict\drop_while` instead.
*
* @see Dict\drop_while()
*/
function drop_while(iterable $iterable, callable $predicate): array
{
$result = [];
$failed = false;
foreach ($iterable as $key => $value) {
if (!$failed && !$predicate($value)) {
$failed = true;
}

if ($failed) {
$result[$key] = $value;
}
}

return $result;
return Dict\drop_while($iterable, $predicate);
}
22 changes: 6 additions & 16 deletions src/Psl/Arr/equal.php
Expand Up @@ -4,7 +4,7 @@

namespace Psl\Arr;

use Psl\Iter;
use Psl\Dict;

/**
* Returns whether the two given arrays have the same entries, using strict
Expand All @@ -15,22 +15,12 @@
*
* @psalm-param array<Tk, Tv> $array
* @psalm-param array<Tk, Tv> $array2
*
* @deprecated use `Dict\equal` instead.
*
* @see Dict\equal()
*/
function equal(array $array, array $array2): bool
{
if ($array === $array2) {
return true;
}

if (Iter\count($array) !== Iter\count($array2)) {
return false;
}

foreach ($array as $key => $value) {
if (!contains_key($array2, $key) || $array2[$key] !== $value) {
return false;
}
}

return true;
return Dict\equal($array, $array2);
}
17 changes: 6 additions & 11 deletions src/Psl/Arr/filter.php
Expand Up @@ -4,7 +4,7 @@

namespace Psl\Arr;

use Closure;
use Psl\Dict;

/**
* Returns an array containing only the values for which the given predicate
Expand All @@ -27,17 +27,12 @@
* @psalm-param (callable(Tv): bool)|null $predicate
*
* @psalm-return array<Tk, Tv>
*
* @deprecated use `Dict\filter` instead.
*
* @see Dict\filter()
*/
function filter(iterable $iterable, ?callable $predicate = null): array
{
/** @psalm-var (callable(Tv): bool) $predicate */
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');
$result = [];
foreach ($iterable as $k => $v) {
if ($predicate($v)) {
$result[$k] = $v;
}
}

return $result;
return Dict\filter($iterable, $predicate);
}
19 changes: 7 additions & 12 deletions src/Psl/Arr/filter_keys.php
Expand Up @@ -4,10 +4,10 @@

namespace Psl\Arr;

use Closure;
use Psl\Dict;

/**
* Returns an array containing only the keys for which the given predicate
* Returns a dict containing only the keys for which the given predicate
* returns `true`.
*
* The default predicate is casting the key to boolean.
Expand All @@ -27,17 +27,12 @@
* @psalm-param (callable(Tk): bool)|null $predicate
*
* @psalm-return array<Tk, Tv>
*
* @deprecated use `Dict\filter_keys` instead.
*
* @see Dict\filter_keys()
*/
function filter_keys(iterable $iterable, ?callable $predicate = null): array
{
/** @psalm-var (callable(Tk): bool) $predicate */
$predicate = $predicate ?? Closure::fromCallable('Psl\Internal\boolean');
$result = [];
foreach ($iterable as $k => $v) {
if ($predicate($k)) {
$result[$k] = $v;
}
}

return $result;
return Dict\filter_keys($iterable, $predicate);
}
23 changes: 6 additions & 17 deletions src/Psl/Arr/filter_with_key.php
Expand Up @@ -4,7 +4,7 @@

namespace Psl\Arr;

use Psl;
use Psl\Dict;

/**
* Returns an array containing only the keys and values for which the given predicate
Expand All @@ -30,23 +30,12 @@
* @psalm-param (callable(Tk, Tv): bool)|null $predicate
*
* @psalm-return array<Tk, Tv>
*
* @deprecated use `Dict\filter_with_key` instead.
*
* @see Dict\filter_with_key()
*/
function filter_with_key(iterable $iterable, ?callable $predicate = null): array
{
$predicate = $predicate ??
/**
* @psalm-param Tk $k
* @psalm-param Tv $v
*/
static fn ($k, $v): bool => Psl\Internal\boolean($v);

/** @psalm-var array<Tk, Tv> $result */
$result = [];
foreach ($iterable as $k => $v) {
if ($predicate($k, $v)) {
$result[$k] = $v;
}
}

return $result;
return Dict\filter_with_key($iterable, $predicate);
}
15 changes: 7 additions & 8 deletions src/Psl/Arr/flatten.php
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Arr;

use Psl\Dict;

/**
* Returns a new array formed by merging the iterable elements of the
* given iterable.
Expand All @@ -24,15 +26,12 @@
* @psalm-param iterable<iterable<Tk, Tv>> $iterables
*
* @psalm-return array<Tk, Tv>
*
* @deprecated use `Dict\flatten` instead.
*
* @see Dict\flatten
*/
function flatten(iterable $iterables): array
{
$result = [];
foreach ($iterables as $iterable) {
foreach ($iterable as $key => $value) {
$result[$key] = $value;
}
}

return $result;
return Dict\flatten($iterables);
}
21 changes: 6 additions & 15 deletions src/Psl/Arr/flip.php
Expand Up @@ -4,8 +4,7 @@

namespace Psl\Arr;

use Psl;
use Psl\Type;
use Psl\Dict;

/**
* Flips the keys and values of an array. In case of
Expand All @@ -22,20 +21,12 @@
* @psalm-param array<Tk, Tv> $array
*
* @psalm-return array<Tv, Tk>
*
* @deprecated use `Dict\flip` instead.
*
* @see Dict\flip
*/
function flip(array $array): array
{
$result = [];
foreach ($array as $k => $v) {
Psl\invariant(
Type\is_arraykey($v),
'Expected all values to be of type array-key, value of type (%s) provided.',
gettype($v)
);

$result[$v] = $k;
}

/** @psalm-var array<Tv, Tk> $result*/
return $result;
return Dict\flip($array);
}
26 changes: 6 additions & 20 deletions src/Psl/Arr/group_by.php
Expand Up @@ -4,8 +4,7 @@

namespace Psl\Arr;

use Psl;
use Psl\Type;
use Psl\Dict;

/**
* Returns a new array where
Expand Down Expand Up @@ -37,25 +36,12 @@
* @psalm-param (callable(Tv): ?Tk) $key_func
*
* @psalm-return array<Tk, list<Tv>>
*
* @deprecated use Dict\group_by
*
* @see Dict\group_by
*/
function group_by(iterable $values, callable $key_func): array
{
$result = [];
foreach ($values as $value) {
$key = $key_func($value);
if (null === $key) {
continue;
}

Psl\invariant(
Type\is_arraykey($key),
'Expected $key_func to return a value of type array-key, value of type (%s) returned.',
gettype($key)
);
/** @psalm-var Tk $key */
$result[$key] = $result[$key] ?? [];
$result[$key][] = $value;
}

return $result;
return Dict\group_by($values, $key_func);
}
2 changes: 2 additions & 0 deletions src/Psl/Arr/keys.php
Expand Up @@ -17,6 +17,8 @@
* @psalm-return list<Tk>
*
* @deprecated since 1.2, use Vec\keys instead.
*
* @see Vec\keys()
*/
function keys(array $arr): array
{
Expand Down
13 changes: 7 additions & 6 deletions src/Psl/Arr/map.php
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Arr;

use Psl\Dict;

/**
* Applies a mapping function to all values of an iterable.
*
Expand All @@ -25,13 +27,12 @@
* @psalm-param (callable(Tv): T) $function
*
* @psalm-return array<Tk, T>
*
* @deprecated use `Dict\map` instead.
*
* @see Dict\map()
*/
function map(iterable $iterable, callable $function): array
{
$result = [];
foreach ($iterable as $key => $value) {
$result[$key] = $function($value);
}

return $result;
return Dict\map($iterable, $function);
}
13 changes: 7 additions & 6 deletions src/Psl/Arr/map_keys.php
Expand Up @@ -4,6 +4,8 @@

namespace Psl\Arr;

use Psl\Dict;

/**
* Applies a mapping function to all keys of an iterable.
*
Expand All @@ -25,13 +27,12 @@
* @psalm-param (callable(Tk1): Tk2) $function
*
* @psalm-return array<Tk2, Tv>
*
* @deprecated use `Dict\map_keys` instead.
*
* @see Dict\map()
*/
function map_keys(iterable $iterable, callable $function): array
{
$result = [];
foreach ($iterable as $key => $value) {
$result[$function($key)] = $value;
}

return $result;
return Dict\map_keys($iterable, $function);
}

0 comments on commit 2e333dc

Please sign in to comment.