Skip to content

Commit

Permalink
fix: update various operations static analysis annotations.
Browse files Browse the repository at this point in the history
Get rid of one psalm issue
  • Loading branch information
drupol committed Nov 7, 2022
1 parent c1998e9 commit 473f105
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 24 deletions.
12 changes: 11 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ parameters:
path: src/Collection.php

-
message: "#^Parameter \\#1 \\$ of closure expects callable\\(mixed, mixed, mixed, iterable\\)\\: mixed, callable\\(T\\|V, T, TKey, Iterator\\<TKey, T\\>\\)\\: V given\\.$#"
message: "#^Parameter \\#1 \\$ of closure expects callable\\(mixed, mixed, mixed, iterable\\)\\: mixed, callable\\(T\\|V, T, TKey, Iterator\\<TKey, T\\>\\)\\: T\\|V given\\.$#"
count: 1
path: src/Collection.php

Expand Down Expand Up @@ -319,3 +319,13 @@ parameters:
message: "#^Parameter \\#1 \\$callable of method loophp\\\\collection\\\\Collection\\<int,string\\>\\:\\:groupBy\\(\\) expects callable\\(string, int\\=\\)\\: int, Closure\\(string, int\\)\\: int given\\.$#"
count: 1
path: tests/static-analysis/groupbyable.php

-
message: "#^While loop condition is always true\\.$#"
count: 1
path: tests/static-analysis/scanLeft1.php

-
message: "#^While loop condition is always true\\.$#"
count: 1
path: tests/static-analysis/scanRight1.php
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
<code>Closure(bool): Closure(iterable&lt;TKey, T&gt;): (Generator&lt;int, T&gt;|Generator&lt;TKey, T&gt;)</code>
</InvalidReturnType>
</file>
<file src="src/Operation/Averages.php">
<InvalidScalarArgument occurrences="1">
<code>static fn (float $acc, float $value, int $key): float =&gt; ($acc * $key + $value) / ($key + 1)</code>
</InvalidScalarArgument>
</file>
<file src="src/Operation/Inits.php">
<InvalidArgument occurrences="2">
<code>[]</code>
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Operation/ScanLeft1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ interface ScanLeft1able
*
* @template V
*
* @param callable((T|V), T, TKey, iterable<TKey, T>): V $callback
* @param callable((T|V), T, TKey, iterable<TKey, T>): (T|V) $callback
*
* @return Collection<int|TKey, V>
* @return Collection<TKey, T|V>
*/
public function scanLeft1(callable $callback): Collection;
}
4 changes: 2 additions & 2 deletions src/Contract/Operation/ScanRight1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface ScanRight1able
*
* @template V
*
* @param callable((T|V), T, TKey, Iterator<TKey, T>): V $callback
* @param callable((T|V), T, TKey, Iterator<TKey, T>): (T|V) $callback
*
* @return Collection<int|TKey, V>
* @return Collection<TKey, T|V>
*/
public function scanRight1(callable $callback): Collection;
}
10 changes: 5 additions & 5 deletions src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ final class ScanLeft1 extends AbstractOperation
/**
* @template V
*
* @return Closure(callable((T|V), T, TKey, iterable<TKey, T>): V): Closure(iterable<TKey, T>): Generator<int|TKey, V>
* @return Closure(callable((T|V), T, TKey, iterable<TKey, T>): (T|V)): Closure(iterable<TKey, T>): Generator<TKey, T|V>
*/
public function __invoke(): Closure
{
return
/**
* @param callable((T|V), T, TKey, iterable<TKey, T>): V $callback
* @param callable((T|V), T, TKey, iterable<TKey, T>): (T|V) $callback
*
* @return Closure(iterable<TKey, T>): Generator<int|TKey, V>
* @return Closure(iterable<TKey, T>): Generator<TKey, T|V>
*/
static fn (callable $callback): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<int|TKey, V>
* @return Generator<TKey, T|V>
*/
static function (iterable $iterable) use ($callback): Generator {
$iteratorAggregate = new IterableIteratorAggregate($iterable);
Expand All @@ -46,7 +46,7 @@ static function (iterable $iterable) use ($callback): Generator {

$initial = $iteratorInitial->current();

/** @var Closure(iterable<TKey, T>): Generator<int|TKey, V> $pipe */
/** @var Closure(iterable<TKey, T>): Generator<TKey, T|V> $pipe */
$pipe = (new Pipe())()(
(new Tail())(),
(new Reduction())()($callback)($initial),
Expand Down
8 changes: 4 additions & 4 deletions src/Operation/ScanRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ final class ScanRight1 extends AbstractOperation
/**
* @template V
*
* @return Closure(callable((T|V), T, TKey, iterable<TKey, T>): V): Closure(iterable<TKey, T>): Generator<int|TKey, V>
* @return Closure(callable((T|V), T, TKey, iterable<TKey, T>): (T|V)): Closure(iterable<TKey, T>): Generator<TKey, T|V>
*/
public function __invoke(): Closure
{
return
/**
* @param callable((T|V), T, TKey, iterable<TKey, T>): V $callback
* @param callable((T|V), T, TKey, iterable<TKey, T>): (T|V) $callback
*
* @return Closure(iterable<TKey, T>): Generator<int|TKey, V>
* @return Closure(iterable<TKey, T>): Generator<TKey, T|V>
*/
static function (callable $callback): Closure {
/** @var Closure(iterable<TKey, T>): Generator<int|TKey, V> $pipe */
/** @var Closure(iterable<TKey, T>): Generator<TKey, T|V> $pipe */
$pipe = (new Pipe())()(
(new Reverse())(),
(new ScanLeft1())()($callback),
Expand Down
22 changes: 19 additions & 3 deletions tests/static-analysis/scanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use loophp\collection\Contract\Collection as CollectionInterface;

$concat = static fn (string $carry, string $string): string => sprintf('%s%s', $carry, $string);
$toString = static fn (int|string $carry, int $value): string => sprintf('%s', $value);
$toString =
/**
* @param int|string $carry
*/
static fn ($carry, int $value): string => sprintf('%s', $value);

/**
* @param CollectionInterface<int, string> $collection
Expand All @@ -18,12 +22,24 @@ function scanLeft1_checkListString(CollectionInterface $collection): void
}

/**
* @param CollectionInterface<int, string> $collection
* @param CollectionInterface<int, int|string> $collection
*/
function scanLeft1_checkListOfSize1String(CollectionInterface $collection): void
{
}

$intGenerator =
/**
* @return Generator<int, int>
*/
static function (): Generator {
while (true) {
$int = random_int(0, \PHP_INT_MAX);

yield $int => $int;
}
};

// see Psalm bug: https://github.com/vimeo/psalm/issues/6108
scanLeft1_checkListString(Collection::fromIterable(range('a', 'c'))->scanLeft1($concat));
scanLeft1_checkListOfSize1String(Collection::fromIterable([10])->scanLeft1($toString));
scanLeft1_checkListOfSize1String(Collection::fromIterable($intGenerator())->scanLeft1($toString));
16 changes: 14 additions & 2 deletions tests/static-analysis/scanRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ function scanRight1_checkListString(CollectionInterface $collection): void
}

/**
* @param CollectionInterface<int, string> $collection
* @param CollectionInterface<int, int|string> $collection
*/
function scanRight1_checkListOfSize1String(CollectionInterface $collection): void
{
}

$intGenerator =
/**
* @return Generator<int, int>
*/
static function (): Generator {
while (true) {
$int = random_int(0, \PHP_INT_MAX);

yield $int => $int;
}
};

// see Psalm bug: https://github.com/vimeo/psalm/issues/6108
scanRight1_checkListString(Collection::fromIterable(range('a', 'c'))->scanRight1($concat));
scanRight1_checkListOfSize1String(Collection::fromIterable([10])->scanRight1($toString));
scanRight1_checkListOfSize1String(Collection::fromIterable($intGenerator())->scanRight1($toString));

0 comments on commit 473f105

Please sign in to comment.