fix few type isseus using psalm

This commit is contained in:
azjezz 2019-12-25 20:38:27 +01:00
parent 9b1c2e9834
commit ab6d022618
7 changed files with 74 additions and 82 deletions

View File

@ -16,7 +16,8 @@ use Psl\Str;
* *
* @psalm-param iterable<Tv> $values * @psalm-param iterable<Tv> $values
* *
* @psalm-return array<Tv, int> * @psalm-return array<Tv|Tv, int>
* @return int[]
*/ */
function count_values(iterable $values): array function count_values(iterable $values): array
{ {

View File

@ -21,6 +21,7 @@ use Psl\Str;
* @psalm-param (callable(Tv): Ts) $scalar_func * @psalm-param (callable(Tv): Ts) $scalar_func
* *
* @psalm-return array<Tk, Tv> * @psalm-return array<Tk, Tv>
* @return array
*/ */
function unique_by(iterable $iterable, callable $scalar_func): array function unique_by(iterable $iterable, callable $scalar_func): array
{ {

View File

@ -69,6 +69,7 @@ final class ImmMap implements ConstMap
* Get an array copy of the the collection. * Get an array copy of the the collection.
* *
* @psalm-return array<Tk, Tv> * @psalm-return array<Tk, Tv>
* @return array
*/ */
public function toArray(): array public function toArray(): array
{ {
@ -517,18 +518,19 @@ final class ImmMap implements ConstMap
} }
/** /**
* Returns a `MutableVector` that is the concatenation of the values of the * Returns a `Vector` that is the concatenation of the values of the
* current `ImmMap` and the values of the provided `iterable`. * current `ImmMap` and the values of the provided `iterable`.
* *
* The provided `iterable` is concatenated to the end of the current * The provided `iterable` is concatenated to the end of the current
* `ImmMap` to produce the returned `MutableVector`. * `ImmMap` to produce the returned `Vector`.
* *
* @psalm-template Tu of Tv * @psalm-template Tu of Tv
* *
* @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current * @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current
* `ImmMap` * `ImmMap`
* *
* @psalm-return Vector<Tu> - The integer-indexed concatenated `MutableVector` * @psalm-return Vector<Tv>
* @return Vector - The integer-indexed concatenated `Vector`
*/ */
public function concat(iterable $iterable): Vector public function concat(iterable $iterable): Vector
{ {

View File

@ -550,18 +550,19 @@ final class Map implements MutableMap
} }
/** /**
* Returns a `MutableVector` that is the concatenation of the values of the * Returns a `Vector` that is the concatenation of the values of the
* current `Map` and the values of the provided `iterable`. * current `Map` and the values of the provided `iterable`.
* *
* The provided `iterable` is concatenated to the end of the current * The provided `iterable` is concatenated to the end of the current
* `Map` to produce the returned `MutableVector`. * `Map` to produce the returned `Vector`.
* *
* @psalm-template Tu of Tv * @psalm-template Tu of Tv
* *
* @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current * @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current
* `Map` * `Map`
* *
* @psalm-return Vector<Tu> - The integer-indexed concatenated `MutableVector` * @psalm-return Vector<Tv>
* @return Vector - The integer-indexed concatenated `Vector`
*/ */
public function concat(iterable $iterable): Vector public function concat(iterable $iterable): Vector
{ {

View File

@ -89,7 +89,7 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a ImmVector containing the values of the current ImmVector that meet a supplied * Returns a Vector containing the values of the current Vector that meet a supplied
* condition. * condition.
* *
* @psalm-param (callable(Tv): bool) $fn * @psalm-param (callable(Tv): bool) $fn
@ -104,7 +104,7 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a ImmVector containing the values of the current ImmVector that meet a supplied * Returns a Vector containing the values of the current Vector that meet a supplied
* condition applied to its keys and values. * condition applied to its keys and values.
* *
* @psalm-param (callable(int, Tv): bool) $fn * @psalm-param (callable(int, Tv): bool) $fn
@ -119,8 +119,8 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a ImmVector containing the values of the current * Returns a Vector containing the values of the current
* ImmVector. Essentially a copy of the current ImmVector. * Vector. Essentially a copy of the current Vector.
* *
* @psalm-return Vector<Tv> * @psalm-return Vector<Tv>
*/ */
@ -130,7 +130,7 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the keys of the current `ImmVector`. * Returns a `Vector` containing the keys of the current `Vector`.
* *
* @psalm-return Vector<int> * @psalm-return Vector<int>
*/ */
@ -162,19 +162,19 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the values after an operation has been * Returns a `Vector` containing the values after an operation has been
* applied to each key and value in the current `ImmVector`. * applied to each key and value in the current `Vector`.
* *
* Every key and value in the current `ImmVector` is affected by a call to * Every key and value in the current `Vector` is affected by a call to
* `mapWithKey()`, unlike `filterWithKey()` where only values that meet a * `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
* certain criteria are affected. * certain criteria are affected.
* *
* @psalm-template Tu * @psalm-template Tu
* *
* @psalm-param (callable(int, Tv): Tu) $fn - The callback containing the operation to apply to the * @psalm-param (callable(int, Tv): Tu) $fn - The callback containing the operation to apply to the
* `ImmVector` keys and values. * `Vector` keys and values.
* *
* @psalm-return Vector<Tu> - a `ImmVector` containing the values after a user-specified * @psalm-return Vector<Tu> - a `Vector` containing the values after a user-specified
* operation on the current Vector's keys and values is applied. * operation on the current Vector's keys and values is applied.
*/ */
public function mapWithKey(callable $fn): Vector public function mapWithKey(callable $fn): Vector
@ -183,10 +183,10 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` where each element is a `Pair` that combines the * Returns a `Vector` where each element is a `Pair` that combines the
* element of the current `ImmVector` and the provided `iterable`. * element of the current `Vector` and the provided `iterable`.
* *
* If the number of elements of the `ImmVector` are not equal to the * If the number of elements of the `Vector` are not equal to the
* number of elements in the `iterable`, then only the combined elements * number of elements in the `iterable`, then only the combined elements
* up to and including the final element of the one with the least number of * up to and including the final element of the one with the least number of
* elements is included. * elements is included.
@ -194,7 +194,7 @@ final class Vector implements MutableVector
* @psalm-template Tu * @psalm-template Tu
* *
* @psalm-param iterable<Tu> $iterable - The `iterable` to use to combine with the * @psalm-param iterable<Tu> $iterable - The `iterable` to use to combine with the
* elements of this `ImmVector`. * elements of this `Vector`.
* *
* @psalm-return Vector<Pair<Tv, Tu>> - The `Vector` that combines the values of the current * @psalm-return Vector<Pair<Tv, Tu>> - The `Vector` that combines the values of the current
* `Vector` with the provided `iterable`. * `Vector` with the provided `iterable`.
@ -220,19 +220,19 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the first `n` values of the current * Returns a `Vector` containing the first `n` values of the current
* `ImmVector`. * `Vector`.
* *
* The returned `ImmVector` will always be a proper subset of the current * The returned `Vector` will always be a proper subset of the current
* `ImmVector`. * `Vector`.
* *
* `$n` is 1-based. So the first element is 1, the second 2, etc. * `$n` is 1-based. So the first element is 1, the second 2, etc.
* *
* @psalm-param int $n - The last element that will be included in the returned * @psalm-param int $n - The last element that will be included in the returned
* `ImmVector` * `Vector`
* *
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current * @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `ImmVector` up to `n` elements. * `Vector` up to `n` elements.
*/ */
public function take(int $n): Vector public function take(int $n): Vector
{ {
@ -242,18 +242,15 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the values of the current `ImmVector` * Returns a `Vector` containing the values of the current `Vector`
* up to but not including the first value that produces `false` when passed * up to but not including the first value that produces `false` when passed
* to the specified callback. * to the specified callback.
* *
* The returned `ImmVector` will always be a proper subset of the current
* `ImmVector`.
*
* @psalm-param (callable(Tv): bool) $fn - The callback that is used to determine the stopping * @psalm-param (callable(Tv): bool) $fn - The callback that is used to determine the stopping
* condition. * condition.
* *
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current * @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `ImmVector` up until the callback returns `false`. * `Vector` up until the callback returns `false`.
*/ */
public function takeWhile(callable $fn): Vector public function takeWhile(callable $fn): Vector
{ {
@ -263,16 +260,13 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the values after the `n`-th element of * Returns a `Vector` containing the values after the `n`-th element of
* the current `ImmVector`. * the current `Vector`.
*
* The returned `ImmVector` will always be a proper subset of the current
* `ImmVector`.
* *
* `$n` is 1-based. So the first element is 1, the second 2, etc. * `$n` is 1-based. So the first element is 1, the second 2, etc.
* *
* @psalm-param int $n - The last element to be skipped; the $n+1 element will be the * @psalm-param int $n - The last element to be skipped; the $n+1 element will be the
* first one in the returned `ImmVector`. * first one in the returned `Vector`.
* *
* @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current * @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `Vector` containing values after the specified `n`-th * `Vector` containing values after the specified `n`-th
@ -286,18 +280,15 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` containing the values of the current `ImmVector` * Returns a `Vector` containing the values of the current `Vector`
* starting after and including the first value that produces `true` when * starting after and including the first value that produces `true` when
* passed to the specified callback. * passed to the specified callback.
* *
* The returned `ImmVector` will always be a proper subset of the current
* `ImmVector`.
*
* @psalm-param (callable(Tv): bool) $fn - The callback used to determine the starting element for the * @psalm-param (callable(Tv): bool) $fn - The callback used to determine the starting element for the
* returned `ImmVector`. * returned `Vector`.
* *
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current * @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `ImmVector` starting after the callback returns `true`. * `Vector` starting after the callback returns `true`.
*/ */
public function dropWhile(callable $fn): Vector public function dropWhile(callable $fn): Vector
{ {
@ -307,22 +298,19 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a subset of the current `ImmVector` starting from a given key up * Returns a subset of the current `Vector` starting from a given key up
* to, but not including, the element at the provided length from the starting * to, but not including, the element at the provided length from the starting
* key. * key.
* *
* `$start` is 0-based. $len is 1-based. So `slice(0, 2)` would return the * `$start` is 0-based. $len is 1-based. So `slice(0, 2)` would return the
* elements at key 0 and 1. * elements at key 0 and 1.
* *
* The returned `ImmVector` will always be a proper subset of this
* `ImmVector`.
*
* @psalm-param int $start - The starting key of this Vector to begin the returned * @psalm-param int $start - The starting key of this Vector to begin the returned
* `ImmVector` * `Vector`
* @psalm-param int $len - The length of the returned `ImmVector` * @psalm-param int $len - The length of the returned `Vector`
* *
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current * @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `ImmVector` starting at `$start` up to but not including the * `Vector` starting at `$start` up to but not including the
* element `$start + $len`. * element `$start + $len`.
*/ */
public function slice(int $start, int $len): Vector public function slice(int $start, int $len): Vector
@ -333,18 +321,15 @@ final class Vector implements MutableVector
} }
/** /**
* Returns a `ImmVector` that is the concatenation of the values of the * Returns a `Vector` that is the concatenation of the values of the
* current `ImmVector` and the values of the provided `iterable`. * current `Vector` and the values of the provided `iterable`.
*
* The values of the provided `iterable` is concatenated to the end of the
* current `ImmVector` to produce the returned `ImmVector`.
* *
* @psalm-template Tu of Tv * @psalm-template Tu of Tv
* *
* @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current * @psalm-param iterable<Tu> $iterable - The `iterable` to concatenate to the current
* `ImmVector`. * `Vector`.
* *
* @psalm-return Vector<Tu> - The concatenated `ImmVector`. * @psalm-return Vector<Tu> - The concatenated `Vector`.
*/ */
public function concat(iterable $iterable): Vector public function concat(iterable $iterable): Vector
{ {
@ -355,10 +340,10 @@ final class Vector implements MutableVector
} }
/** /**
* Returns the first value in the current `ImmVector`. * Returns the first value in the current `Vector`.
* *
* @psalm-return null|Tv - The first value in the current `ImmVector`, or `null` if the * @psalm-return null|Tv - The first value in the current `Vector`, or `null` if the
* current `ImmVector` is empty. * current `Vector` is empty.
*/ */
public function first() public function first()
{ {
@ -366,10 +351,10 @@ final class Vector implements MutableVector
} }
/** /**
* Returns the first key in the current `ImmVector`. * Returns the first key in the current `Vector`.
* *
* @psalm-return int|null - The first key in the current `ImmVector`, or `null` if the * @psalm-return int|null - The first key in the current `Vector`, or `null` if the
* current `ImmVector` is empty * current `Vector` is empty
*/ */
public function firstKey(): ?int public function firstKey(): ?int
{ {
@ -377,10 +362,10 @@ final class Vector implements MutableVector
} }
/** /**
* Returns the last value in the current `ImmVector`. * Returns the last value in the current `Vector`.
* *
* @psalm-return null|Tv - The last value in the current `ImmVector`, or `null` if the * @psalm-return null|Tv - The last value in the current `Vector`, or `null` if the
* current `ImmVector` is empty. * current `Vector` is empty.
*/ */
public function last() public function last()
{ {
@ -388,10 +373,10 @@ final class Vector implements MutableVector
} }
/** /**
* Returns the last key in the current `ImmVector`. * Returns the last key in the current `Vector`.
* *
* @psalm-return int|null - The last key in the current `ImmVector`, or `null` if the * @psalm-return int|null - The last key in the current `Vector`, or `null` if the
* current `ImmVector` is empty * current `Vector` is empty
*/ */
public function lastKey(): ?int public function lastKey(): ?int
{ {
@ -404,7 +389,7 @@ final class Vector implements MutableVector
* If no element matches the search value, this function returns null. * If no element matches the search value, this function returns null.
* *
* @psalm-param Tv $search_value - The value that will be search for in the current * @psalm-param Tv $search_value - The value that will be search for in the current
* `ImmVector`. * `Vector`.
* *
* @psalm-return int|null - The key (index) where that value is found; null if it is not found * @psalm-return int|null - The key (index) where that value is found; null if it is not found
*/ */

View File

@ -32,8 +32,10 @@ function filter_with_key(iterable $iterable, ?callable $predicate = null): itera
/** /**
* @psalm-param Tk $k * @psalm-param Tk $k
* @psalm-param Tv $v * @psalm-param Tv $v
*
* @return bool
*/ */
fn ($k, $v) => Psl\Internal\boolean($k) && Psl\Internal\boolean($v); fn ($k, $v) => Psl\Internal\boolean($v);
foreach ($iterable as $k => $v) { foreach ($iterable as $k => $v) {
if ($predicate($k, $v)) { if ($predicate($k, $v)) {

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Psl\Tests\Random; namespace Psl\Tests\Random;
use Psl\Str;
use Psl\Random;
use Psl\Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psl\Exception;
use Psl\Random;
use Psl\Str;
class StringTest extends TestCase class StringTest extends TestCase
{ {