[Iter] cs fix

This commit is contained in:
azjezz 2020-09-01 02:10:27 +01:00
parent 1aad753ca2
commit 4e8c2ffaf7
4 changed files with 33 additions and 32 deletions

View File

@ -159,7 +159,7 @@ final class Iterator implements SeekableIterator, Countable
} }
if ($this->generator) { if ($this->generator) {
while($this->position !== $position) { while ($this->position !== $position) {
Psl\invariant($this->generator->valid(), 'Position is out-of-bounds.'); Psl\invariant($this->generator->valid(), 'Position is out-of-bounds.');
$this->next(); $this->next();
} }

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Psl\Iter; namespace Psl\Iter;
use Psl\Internal;
use Generator; use Generator;
use Psl\Internal;
/** /**
* Filter out null values from the given iterable. * Filter out null values from the given iterable.
@ -22,9 +22,9 @@ use Generator;
*/ */
function filter_nulls(iterable $iterable): Iterator function filter_nulls(iterable $iterable): Iterator
{ {
return Internal\lazy_iterator(static function() use($iterable): Generator { return Internal\lazy_iterator(static function () use ($iterable): Generator {
foreach($iterable as $value) { foreach ($iterable as $value) {
if ($value !== null) { if (null !== $value) {
yield $value; yield $value;
} }
} }

View File

@ -43,7 +43,7 @@ function range($start, $end, $step = null): Iterator
return Internal\lazy_iterator( return Internal\lazy_iterator(
/** /**
* @return Generator<int, T, mixed, void> * @return Generator<int, T, mixed, void>
* *
* @see https://github.com/vimeo/psalm/issues/2152#issuecomment-533363310 * @see https://github.com/vimeo/psalm/issues/2152#issuecomment-533363310
* *
* @psalm-suppress InvalidReturnType * @psalm-suppress InvalidReturnType
@ -52,36 +52,37 @@ function range($start, $end, $step = null): Iterator
* @psalm-suppress DocblockTypeContradiction * @psalm-suppress DocblockTypeContradiction
*/ */
static function () use ($start, $end, $step): Generator { static function () use ($start, $end, $step): Generator {
if ((float) $start === (float) $end) { if ((float) $start === (float) $end) {
yield $start; yield $start;
} elseif ($start < $end) { } elseif ($start < $end) {
if (null === $step) { if (null === $step) {
/** @psalm-var T $step */ /** @psalm-var T $step */
$step = 1; $step = 1;
} else { } else {
Psl\invariant($step > 0, 'If start < end, the step must be positive'); Psl\invariant($step > 0, 'If start < end, the step must be positive');
} }
Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
for ($i = $start; $i <= $end; $i += $step) {
Psl\invariant(is_int($i) || is_float($i), '$i must be either an integer or a float.');
Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.'); Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
yield $i; for ($i = $start; $i <= $end; $i += $step) {
} Psl\invariant(is_int($i) || is_float($i), '$i must be either an integer or a float.');
} else { Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
if (null === $step) { yield $i;
/** @psalm-var T $step */ }
$step = -1;
} else { } else {
Psl\invariant($step < 0, 'If start > end, the step must be negative'); if (null === $step) {
} /** @psalm-var T $step */
$step = -1;
} else {
Psl\invariant($step < 0, 'If start > end, the step must be negative');
}
Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
for ($i = $start; $i >= $end; $i += $step) {
Psl\invariant(is_int($i) || is_float($i), '$i must be either an integer or a float.');
Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.'); Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
yield $i; for ($i = $start; $i >= $end; $i += $step) {
Psl\invariant(is_int($i) || is_float($i), '$i must be either an integer or a float.');
Psl\invariant(is_int($step) || is_float($step), '$step must be either an integer or a float.');
yield $i;
}
} }
} }
}); );
} }

View File

@ -34,7 +34,7 @@ function repeat($value, ?int $num = null): Iterator
{ {
Psl\invariant(null === $num || $num >= 0, 'Number of repetitions must be non-negative'); Psl\invariant(null === $num || $num >= 0, 'Number of repetitions must be non-negative');
return Internal\lazy_iterator(static function () use($value, $num): Generator { return Internal\lazy_iterator(static function () use ($value, $num): Generator {
if (null === $num) { if (null === $num) {
/** @var int $num */ /** @var int $num */
$num = Math\INFINITY; $num = Math\INFINITY;