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

@ -14,9 +14,10 @@ use Psl\Str;
*
* @psalm-template Tv as array-key
*
* @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
{

View File

@ -17,10 +17,11 @@ use Psl\Str;
* @psalm-template Tv
* @psalm-template Ts as array-key
*
* @psalm-param iterable<Tk, Tv> $iterable
* @psalm-param iterable<Tk, Tv> $iterable
* @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
{

View File

@ -69,6 +69,7 @@ final class ImmMap implements ConstMap
* Get an array copy of the the collection.
*
* @psalm-return array<Tk, Tv>
* @return 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`.
*
* 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-param iterable<Tu> $iterable - The `iterable` to concatenate to the current
* `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
{

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`.
*
* 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-param iterable<Tu> $iterable - The `iterable` to concatenate to the current
* `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
{

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.
*
* @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.
*
* @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
* ImmVector. Essentially a copy of the current ImmVector.
* Returns a Vector containing the values of the current
* Vector. Essentially a copy of the current Vector.
*
* @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>
*/
@ -162,19 +162,19 @@ final class Vector implements MutableVector
}
/**
* Returns a `ImmVector` containing the values after an operation has been
* applied to each key and value in the current `ImmVector`.
* Returns a `Vector` containing the values after an operation has been
* 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
* certain criteria are affected.
*
* @psalm-template Tu
*
* @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.
*/
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
* element of the current `ImmVector` and the provided `iterable`.
* Returns a `Vector` where each element is a `Pair` that combines the
* 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
* up to and including the final element of the one with the least number of
* elements is included.
@ -194,7 +194,7 @@ final class Vector implements MutableVector
* @psalm-template Tu
*
* @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
* `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
* `ImmVector`.
* Returns a `Vector` containing the first `n` values of the current
* `Vector`.
*
* The returned `ImmVector` will always be a proper subset of the current
* `ImmVector`.
* The returned `Vector` will always be a proper subset of the current
* `Vector`.
*
* `$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
* `ImmVector`
* `Vector`
*
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current
* `ImmVector` up to `n` elements.
* @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `Vector` up to `n` elements.
*/
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
* 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
* condition.
*
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current
* `ImmVector` up until the callback returns `false`.
* @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `Vector` up until the callback returns `false`.
*/
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
* the current `ImmVector`.
*
* The returned `ImmVector` will always be a proper subset of the current
* `ImmVector`.
* Returns a `Vector` containing the values after the `n`-th element of
* the current `Vector`.
*
* `$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
* 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
* `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
* 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
* returned `ImmVector`.
* returned `Vector`.
*
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current
* `ImmVector` starting after the callback returns `true`.
* @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `Vector` starting after the callback returns `true`.
*/
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
* key.
*
* `$start` is 0-based. $len is 1-based. So `slice(0, 2)` would return the
* 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
* `ImmVector`
* @psalm-param int $len - The length of the returned `ImmVector`
* `Vector`
* @psalm-param int $len - The length of the returned `Vector`
*
* @psalm-return Vector<Tv> - A `ImmVector` that is a proper subset of the current
* `ImmVector` starting at `$start` up to but not including the
* @psalm-return Vector<Tv> - A `Vector` that is a proper subset of the current
* `Vector` starting at `$start` up to but not including the
* element `$start + $len`.
*/
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
* current `ImmVector` 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`.
* Returns a `Vector` that is the concatenation of the values of the
* current `Vector` and the values of the provided `iterable`.
*
* @psalm-template Tu of Tv
*
* @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
{
@ -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
* current `ImmVector` is empty.
* @psalm-return null|Tv - The first value in the current `Vector`, or `null` if the
* current `Vector` is empty.
*/
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
* current `ImmVector` is empty
* @psalm-return int|null - The first key in the current `Vector`, or `null` if the
* current `Vector` is empty
*/
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
* current `ImmVector` is empty.
* @psalm-return null|Tv - The last value in the current `Vector`, or `null` if the
* current `Vector` is empty.
*/
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
* current `ImmVector` is empty
* @psalm-return int|null - The last key in the current `Vector`, or `null` if the
* current `Vector` is empty
*/
public function lastKey(): ?int
{
@ -404,7 +389,7 @@ final class Vector implements MutableVector
* 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
* `ImmVector`.
* `Vector`.
*
* @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 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) {
if ($predicate($k, $v)) {

View File

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