Skip to content

Commit

Permalink
refactor: Fix a couple of PSalm issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Apr 17, 2021
1 parent b913a6b commit 89db061
Show file tree
Hide file tree
Showing 24 changed files with 39 additions and 49 deletions.
24 changes: 7 additions & 17 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection;

use Closure;
use Generator;
use Iterator;
use loophp\collection\Contract\Collection as CollectionInterface;
use loophp\collection\Contract\Operation;
Expand Down Expand Up @@ -139,9 +138,9 @@ final class Collection implements CollectionInterface
private array $parameters;

/**
* @psalm-var callable(...mixed): (Generator<TKey, T>|Iterator<TKey, T>)
* @psalm-var callable(...mixed): (\Generator<TKey, T>|Iterator<TKey, T>)
*/
private Closure $source;
private $source;

/**
* @param callable|Closure $callable
Expand Down Expand Up @@ -184,10 +183,7 @@ public function associate(
*/
static fn ($carry) => $carry;

$callbackForKeys ??= $defaultCallback;
$callbackForValues ??= $defaultCallback;

return new self(Associate::of()($callbackForKeys)($callbackForValues), $this->getIterator());
return new self(Associate::of()($callbackForKeys ?? $defaultCallback)($callbackForValues ?? $defaultCallback), $this->getIterator());
}

public function asyncMap(callable ...$callbacks): CollectionInterface
Expand Down Expand Up @@ -442,15 +438,15 @@ public function head(): CollectionInterface

public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface
{
$else ??=
$identity =
/**
* @psalm-param T $value
*
* @psalm-return T
*/
static fn ($value) => $value;

return new self(IfThenElse::of()($condition)($then)($else), $this->getIterator());
return new self(IfThenElse::of()($condition)($then)($else ?? $identity), $this->getIterator());
}

public function implode(string $glue = ''): CollectionInterface
Expand Down Expand Up @@ -599,11 +595,7 @@ public function product(iterable ...$iterables): CollectionInterface

public function random(int $size = 1, ?int $seed = null): CollectionInterface
{
if (null === $seed) {
$seed = random_int(PHP_INT_MIN, PHP_INT_MAX);
}

return new self(Random::of()($seed)($size), $this->getIterator());
return new self(Random::of()($seed ?? random_int(PHP_INT_MIN, PHP_INT_MAX))($size), $this->getIterator());
}

public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): CollectionInterface
Expand Down Expand Up @@ -658,9 +650,7 @@ public function scanRight1(callable $callback): CollectionInterface

public function shuffle(?int $seed = null): CollectionInterface
{
$seed ??= random_int(PHP_INT_MIN, PHP_INT_MAX);

return new self(Shuffle::of()($seed), $this->getIterator());
return new self(Shuffle::of()($seed ?? random_int(PHP_INT_MIN, PHP_INT_MAX)), $this->getIterator());
}

public function since(callable ...$callbacks): CollectionInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Operation/Appendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Appendable
*
* @param mixed ...$items
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int|TKey, T>
*/
public function append(...$items): Collection;
}
6 changes: 3 additions & 3 deletions src/Contract/Operation/Associateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
interface Associateable
{
/**
* @psalm-param null|callable(TKey, TKey, T, Iterator<TKey, T>):TKey $callbackForKeys
* @psalm-param null|callable(T, TKey, T, Iterator<TKey, T>):T $callbackForValues
* @psalm-param null|callable(TKey, TKey, T, Iterator<TKey, T>):(T|TKey) $callbackForKeys
* @psalm-param null|callable(T, TKey, T, Iterator<TKey, T>):(T|TKey) $callbackForValues
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey|T, T|TKey>
*/
public function associate(?callable $callbackForKeys = null, ?callable $callbackForValues = null): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Chunkable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Chunkable
/**
* Chunk the collection into chunks of the given size.
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, list<T>>
*/
public function chunk(int ...$sizes): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Explodeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Explodeable
*
* @param mixed ...$explodes
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, list<T>>
*/
public function explode(...$explodes): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Keysable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Keysable
/**
* Get the keys of the items.
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, TKey>
*/
public function keys(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Normalizeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Normalizeable
/**
* Replace, reorder and use numeric keys on a collection.
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, T>
*/
public function normalize(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Padable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Padable
*
* @param mixed $value
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int|TKey, T>
*/
public function pad(int $size, $value): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Pairable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Pairable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<T|TKey, T>
*/
public function pair(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Prependable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Prependable
*
* @param mixed ...$items
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int|TKey, T>
*/
public function prepend(...$items): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Tailsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Tailsable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, list<T>>
*/
public function tails(): Collection;
}
4 changes: 2 additions & 2 deletions src/Contract/Operation/Timesable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ interface Timesable
* @psalm-template TKey of array-key
* @psalm-template T
*
* @psalm-param null|callable(): T $callback
* @psalm-param null|callable(int): (int|T) $callback
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, int|T>
*/
public static function times(int $number = 0, ?callable $callback = null): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Transposeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Transposeable
/**
* Matrix transposition.
*
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey, list<T>>
*/
public function transpose(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Unlinesable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Unlinesable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey, string>
*/
public function unlines(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Unwordsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Unwordsable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey, string>
*/
public function unwords(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Windowable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Windowable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey, T|list<T>>
*/
public function window(int $size): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Wordsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Wordsable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<TKey, string>
*/
public function words(): Collection;
}
2 changes: 1 addition & 1 deletion src/Contract/Operation/Wrapable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Wrapable
{
/**
* @psalm-return \loophp\collection\Collection<TKey, T>
* @psalm-return \loophp\collection\Collection<int, array<TKey, T>>
*/
public function wrap(): Collection;
}
2 changes: 1 addition & 1 deletion src/Operation/Associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class Associate extends AbstractOperation
{
/**
* @psalm-return Closure(callable(TKey, TKey, T, Iterator<TKey, T>): (T|TKey) ...): Closure((callable(T, TKey, T, Iterator<TKey, T>): (T|TKey))...): Closure(Iterator<TKey, T>): iterable<TKey|T, T|TKey>
* @psalm-return Closure(callable(TKey, TKey, T, Iterator<TKey, T>): (T|TKey) ...): Closure((callable(T, TKey, T, Iterator<TKey, T>): (T|TKey))...): Closure(Iterator<TKey, T>): Generator<TKey|T, T|TKey>
*/
public function __invoke(): Closure
{
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class Pair extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>):(Generator<T|TKey, T>)
* @psalm-return Closure(Iterator<TKey, T>): Generator<T|TKey, T>
*/
public function __invoke(): Closure
{
Expand Down Expand Up @@ -46,7 +46,7 @@ public function __invoke(): Closure
*/
static fn ($initial, $key, array $value) => $value[1];

/** @psalm-var Closure(Iterator<TKey, T>):(Generator<T|TKey, T>) $pipe */
/** @psalm-var Closure(Iterator<TKey, T>): Generator<T|TKey, T> $pipe */
$pipe = Pipe::of()(
Chunk::of()(2),
Map::of()(
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Times.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
final class Times extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(null|callable(int): T): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
* @psalm-return Closure(int): Closure(null|callable(int): (int|T)): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(null|callable(int): int|T): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
* @psalm-return Closure(null|callable(int): (int|T)): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
*/
static fn (int $number = 0): Closure =>
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Unlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
final class Unlines extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, (T|string)>): Generator<TKey, T|string, mixed, void>
* @psalm-return Closure(Iterator<TKey, (T|string)>): Generator<TKey, string, mixed, void>
*/
public function __invoke(): Closure
{
/** @psalm-var Closure(Iterator<TKey, (T|string)>):Generator<TKey, T|string> $implode */
/** @psalm-var Closure(Iterator<TKey, (T|string)>):Generator<TKey, string> $implode */
$implode = Implode::of()(PHP_EOL);

// Point free style.
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Unwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
final class Unwords extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, (T | string)>):(Generator<TKey, T|string, mixed, void>)
* @psalm-return Closure(Iterator<TKey, (T|string)>): Generator<TKey, string, mixed, void>
*/
public function __invoke(): Closure
{
/** @psalm-var Closure(Iterator<TKey, (T | string)>):Generator<TKey, string> $implode */
/** @psalm-var Closure(Iterator<TKey, (T | string)>): Generator<TKey, string> $implode */
$implode = Implode::of()(' ');

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
final class Window extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, list<T>>
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T|list<T>>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, list<T>>
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T|list<T>>
*/
static fn (int $size): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, list<T>>
* @psalm-return Generator<TKey, T|list<T>>
*/
static function (Iterator $iterator) use ($size): Generator {
if (0 === $size) {
Expand Down

0 comments on commit 89db061

Please sign in to comment.