Skip to content

Commit

Permalink
refactor: Use new keyword on some operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 30, 2022
1 parent 16e220a commit 6f82a91
Show file tree
Hide file tree
Showing 47 changed files with 121 additions and 148 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Method loophp\\\\collection\\\\Collection\\:\\:partition\\(\\) should return loophp\\\\collection\\\\Contract\\\\Collection\\<int, loophp\\\\collection\\\\Contract\\\\Collection\\<TKey, T\\>\\> but returns loophp\\\\collection\\\\Contract\\\\Collection\\<mixed, loophp\\\\collection\\\\Collection\\<mixed, mixed\\>\\>\\.$#"
count: 1
path: src/Collection.php

-
message: "#^Method loophp\\\\collection\\\\Collection\\:\\:span\\(\\) should return loophp\\\\collection\\\\Contract\\\\Collection\\<int, loophp\\\\collection\\\\Contract\\\\Collection\\<TKey, T\\>\\> but returns loophp\\\\collection\\\\Contract\\\\Collection\\<mixed, loophp\\\\collection\\\\Collection\\<mixed, mixed\\>\\>\\.$#"
count: 1
path: src/Collection.php

-
message: "#^Template type NewT of method loophp\\\\collection\\\\Operation\\\\Associate\\:\\:__invoke\\(\\) is not referenced in a parameter\\.$#"
count: 1
Expand Down
6 changes: 0 additions & 6 deletions phpstan-unsupported-baseline.neon

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ parameters:
reportUnmatchedIgnoredErrors: true
includes:
- phpstan-baseline.neon
- phpstan-unsupported-baseline.neon
6 changes: 2 additions & 4 deletions src/Contract/Operation/Mapable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ interface Mapable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#map
*
* @template V
* @param callable(T=, TKey=, Iterator<TKey, T>=): mixed $callback
*
* @param callable(T=, TKey=, Iterator<TKey, T>=): V $callback
*
* @return Collection<TKey, V>
* @return Collection<TKey, mixed>
*/
public function map(callable $callback): Collection;
}
6 changes: 2 additions & 4 deletions src/Contract/Operation/ScanLeft1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ interface ScanLeft1able
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#scanleft1
*
* @template V
* @param callable(T|mixed, T, TKey, Iterator<TKey, T>): mixed $callback
*
* @param callable(T|V, T, TKey, Iterator<TKey, T>): V $callback
*
* @return Collection<int|TKey, T|V>
* @return Collection<int|TKey, T|mixed>
*/
public function scanLeft1(callable $callback): Collection;
}
9 changes: 3 additions & 6 deletions src/Contract/Operation/ScanLeftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ interface ScanLeftable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#scanleft
*
* @template V
* @template W
* @param callable(mixed=, T=, TKey=, Iterator<TKey, T>=): mixed $callback
* @param mixed $initial
*
* @param callable((V|W)=, T=, TKey=, Iterator<TKey, T>=): W $callback
* @param V $initial
*
* @return Collection<int|TKey, V|W>
* @return Collection<int|TKey, mixed>
*/
public function scanLeft(callable $callback, $initial = null): Collection;
}
6 changes: 2 additions & 4 deletions src/Contract/Operation/ScanRight1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ interface ScanRight1able
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#scanright1
*
* @template V
* @param callable(T|mixed, T, TKey, Iterator<TKey, T>): mixed $callback
*
* @param callable(T|V, T, TKey, Iterator<TKey, T>): V $callback
*
* @return Collection<int|TKey, T|V>
* @return Collection<int|TKey, T|mixed>
*/
public function scanRight1(callable $callback): Collection;
}
9 changes: 3 additions & 6 deletions src/Contract/Operation/ScanRightable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ interface ScanRightable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#scanright
*
* @template V
* @template W
* @param callable(mixed=, T=, TKey=, Iterator<TKey, T>=): mixed $callback
* @param mixed $initial
*
* @param callable((V|W)=, T=, TKey=, Iterator<TKey, T>=): W $callback
* @param V $initial
*
* @return Collection<int|TKey, V|W>
* @return Collection<int|TKey, mixed>
*/
public function scanRight(callable $callback, $initial = null): Collection;
}
7 changes: 2 additions & 5 deletions src/Contract/Operation/Unpackable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

/**
* @template TKey
* @template T of array{0: NewTKey, 1: NewT}
*
* @template NewTKey
* @template NewT
* @template T
*/
interface Unpackable
{
Expand All @@ -25,7 +22,7 @@ interface Unpackable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#unpack
*
* @return Collection<NewTKey, NewT>
* @return Collection<mixed, mixed>
*/
public function unpack(): Collection;
}
2 changes: 1 addition & 1 deletion src/Operation/AsyncMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static function (Iterator $iterator) use ($callback): Generator {
*/
static fn (array $value): array => [$value[0], $callback($value[1], $value[0])];

$iter = map(fromIterable(Pack::of()($iterator)), new LocalSemaphore(32), parallel($parallelCallBack));
$iter = map(fromIterable((new Pack())()($iterator)), new LocalSemaphore(32), parallel($parallelCallBack));

while (wait($iter->advance())) {
/** @var array{0: TKey, 1: V} $item */
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/AsyncMapN.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*/
static fn (array $value): array => [$value[0], array_reduce($callbacks, $callbackFactory($value[0]), $value[1])];

$iter = map(fromIterable(Pack::of()($iterator)), new LocalSemaphore(32), parallel($callback));
$iter = map(fromIterable((new Pack())()($iterator)), new LocalSemaphore(32), parallel($callback));

while (wait($iter->advance())) {
/** @var array{0: mixed, 1: mixed} $item */
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __invoke(): Closure
*/
static function (Iterator $iterator) use ($sizes): Generator {
/** @var Iterator<int, int> $sizesIterator */
$sizesIterator = Cycle::of()(new ArrayIterator($sizes));
$sizesIterator = (new Cycle())()(new ArrayIterator($sizes));

$values = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(): Closure
static fn ($value): bool => is_iterable($value);

/** @var Closure(Iterator<TKey, (T|iterable<TKey, T>)>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
$pipe = (new Pipe())()(
(new Filter())()($filterCallback),
(new Flatten())()(1),
);
Expand Down
8 changes: 4 additions & 4 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ static function ($column): Closure {
static fn ($value, $key, Iterator $iterator): bool => $key === $column;

/** @var Closure(Iterator<TKey, T>): Generator<int, mixed> $pipe */
$pipe = Pipe::of()(
Transpose::of(),
$pipe = (new Pipe())()(
(new Transpose())(),
(new Filter())()($filterCallbackBuilder($column)),
Head::of(),
Flatten::of()(1)
(new Head())(),
(new Flatten())()(1)
);

// Point free style.
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ static function (...$keys): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<null|U, null|T> $pipe */
$pipe = Pipe::of()(
$buildMultipleIterable($keys),
Flatten::of()(1),
Pair::of(),
(new Flatten())()(1),
(new Pair())(),
);

// Point free style.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (...$values): Closure {
static fn ($right): bool => $left === $right;

/** @var Closure(Iterator<TKey, T>): Generator<TKey, bool> $matchOne */
$matchOne = MatchOne::of()(static fn (): bool => true)(...array_map($callback, $values));
$matchOne = (new MatchOne())()(static fn (): bool => true)(...array_map($callback, $values));

// Point free style.
return $matchOne;
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function __invoke(): Closure
*/
static function ($default) use ($index): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int, T|V> $pipe */
$pipe = Pipe::of()(
$pipe = (new Pipe())()(
(new Normalize())(),
Get::of()($index)($default)
(new Get())()($index)($default)
);

// Point free style.
Expand Down
10 changes: 5 additions & 5 deletions src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ static function (callable ...$callbacks) use ($matchers): Closure {
static fn ($value, $key, Iterator $iterator): bool => $reducer1($value, $key, $iterator) !== $reducer2($value, $key, $iterator);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback($callbackReducer($callbacks))($callbackReducer($matchers))),
DropWhile::of()(static fn (bool $value): bool => $value),
Append::of()(true),
Head::of(),
$pipe = (new Pipe())()(
(new Map())()($mapCallback($callbackReducer($callbacks))($callbackReducer($matchers))),
(new DropWhile())()(static fn (bool $value): bool => $value),
(new Append())()(true),
(new Head())(),
);

// Point free style.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(): Closure
*/
static function (...$explodes): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int, list<T>> $split */
$split = Split::of()(Splitable::REMOVE)(
$split = (new Split())()(Splitable::REMOVE)(
...array_map(
/**
* @param T $explode
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function __invoke(): Closure
*/
static function (callable ...$callbacks) use ($default): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T|V> $pipe */
$pipe = Pipe::of()(
$pipe = (new Pipe())()(
(new Filter())()(...$callbacks),
Append::of()($default),
Head::of(),
(new Append())()($default),
(new Head())(),
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FlatMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function __invoke(): Closure
*/
static function (callable $callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<IKey, IValue> $flatMap */
$flatMap = Pipe::of()(
Map::of()($callback),
Flatten::of()(1)
$flatMap = (new Pipe())()(
(new Map())()($callback),
(new Flatten())()(1)
);

// Point free style
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FoldLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function __invoke(): Closure
*/
static function ($initial = null) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
ScanLeft::of()($callback)($initial),
Last::of()
$pipe = (new Pipe())()(
(new ScanLeft())()($callback)($initial),
(new Last())()
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FoldLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __invoke(): Closure
*/
static function (callable $callback): Closure {
/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, T|null>) $pipe */
$pipe = Pipe::of()(
ScanLeft1::of()($callback),
Last::of()
$pipe = (new Pipe())()(
(new ScanLeft1())()($callback),
(new Last())()
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FoldRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __invoke(): Closure
*/
static fn (callable $callback): Closure => static function ($initial = null) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
ScanRight::of()($callback)($initial),
Head::of()
$pipe = (new Pipe())()(
(new ScanRight())()($callback)($initial),
(new Head())()
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/FoldRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function __invoke(): Closure
*/
static function (callable $callback): Closure {
/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, T|null>) $pipe */
$pipe = Pipe::of()(
ScanRight1::of()($callback),
Head::of()
$pipe = (new Pipe())()(
(new ScanRight1())()($callback),
(new Head())()
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ static function ($default) use ($keyToGet): Closure {
static fn ($value, $key): bool => $key === $keyToGet;

/** @var Closure(Iterator<TKey, T>): (Generator<TKey, T|V>) $pipe */
$pipe = Pipe::of()(
$pipe = (new Pipe())()(
(new Filter())()($filterCallback),
Append::of()($default),
Head::of()
(new Append())()($default),
(new Head())()
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ static function (array $collect, $value, $key) use ($callback): array {
};

/** @var Closure(Iterator<TKey, T>): Generator<NewTKey, non-empty-list<T>> $pipe */
$pipe = Pipe::of()(
Reduce::of()($reducerFactory($callable))([]),
Flatten::of()(1)
$pipe = (new Pipe())()(
(new Reduce())()($reducerFactory($callable))([]),
(new Flatten())()(1)
);

// Point free style.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __invoke(): Closure
*/
static function (callable ...$callbacks): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = MatchOne::of()(static fn (): bool => true)(
$pipe = (new MatchOne())()(static fn (): bool => true)(
...array_map(
static fn (callable $callback): callable =>
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/IfThenElse.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __invoke(): Closure
*/
static function (callable $else) use ($condition, $then): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $map */
$map = Map::of()(
$map = (new Map())()(
/**
* @param T $value
* @param TKey $key
Expand Down
8 changes: 4 additions & 4 deletions src/Operation/Implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ static function (string $glue): Closure {
static fn (string $carry, $item): string => $carry .= $item;

/** @var Closure(Iterator<TKey, T>): Generator<TKey, string> $pipe */
$pipe = Pipe::of()(
Intersperse::of()($glue)(1)(0),
Drop::of()(1),
Reduce::of()($reducer)('')
$pipe = (new Pipe())()(
(new Intersperse())()($glue)(1)(0),
(new Drop())()(1),
(new Reduce())()($reducer)('')
);

// Point free style.
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function __invoke(): Closure
*/
static function (int $index): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int, TKey> $pipe */
$pipe = Pipe::of()(
Limit::of()(1)($index),
Flip::of()
$pipe = (new Pipe())()(
(new Limit())()(1)($index),
(new Flip())()
);

// Point free style.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class Map extends AbstractOperation
*
* @template V
*
* @return Closure(callable(T=, TKey=, Iterator<TKey, T>=): V): Closure(Iterator<TKey, T>): Generator<TKey, V>
* @return Closure(callable(T=, TKey=, Iterator<TKey, T>=): mixed): Closure(Iterator<TKey, T>): Generator<TKey, mixed>
*/
public function __invoke(): Closure
{
Expand Down
Loading

0 comments on commit 6f82a91

Please sign in to comment.