Skip to content

Commit

Permalink
Improve static analysis phpdoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 29, 2020
1 parent 32bb7bf commit f87758e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
61 changes: 37 additions & 24 deletions src/Operation/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

/**
* @template TKey
* @psalm-template TKey of array-key
* @template T
*/
final class Cache extends AbstractOperation implements Operation
{
public function __construct(?CacheItemPoolInterface $cache = null)
Expand All @@ -22,39 +27,47 @@ public function __invoke(): Closure
{
$iteratorIndex = 0;

return static function (Iterator $iterator, CacheItemPoolInterface $cache) use (&$iteratorIndex): Generator {
for ($index = 0; true; ++$index) {
$item = $cache->getItem((string) $index);
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
*
* @psalm-return \Generator<TKey, T>
*/
static function (Iterator $iterator, CacheItemPoolInterface $cache) use (&$iteratorIndex): Generator {
for ($index = 0; true; ++$index) {
$item = $cache->getItem((string) $index);

if ($item->isHit()) {
$value = $item->get();
if ($item->isHit()) {
/** @psalm-var array{TKey, T} $value */
$value = $item->get();

yield $value[0] => $value[1];
yield $value[0] => $value[1];

continue;
}
continue;
}

if ($iteratorIndex < $index) {
$iterator->next();
if ($iteratorIndex < $index) {
$iterator->next();

++$iteratorIndex;
}
++$iteratorIndex;
}

if (!$iterator->valid()) {
break;
}
if (!$iterator->valid()) {
break;
}

$item->set([
$iterator->key(),
$iterator->current(),
]);
$item->set([
$iterator->key(),
$iterator->current(),
]);

$cache->save($item);
$cache->save($item);

$value = $item->get();
/** @psalm-var array{TKey, T} $value */
$value = $item->get();

yield $value[0] => $value[1];
}
};
yield $value[0] => $value[1];
}
};
}
}
1 change: 1 addition & 0 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __invoke(): Closure
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
* @psalm-param callable(TKey, T):(TKey) $callable
*
* @psalm-return \Generator<int, list<T>>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function __invoke(): Closure
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
*
* @param mixed $element
* @psalm-param T $element
*
* @psalm-return \Generator<int, T>
*
* @param mixed $element
*/
static function (Iterator $iterator, $element, int $every, int $startAt): Generator {
foreach ($iterator as $value) {
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function __invoke(): Closure
* @psalm-return \Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
/**
* @var TKey $key
* @var T $value
*/
foreach (new InfiniteIterator($iterator) as $key => $value) {
yield $key => $value;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class Reduction extends AbstractOperation implements Operation
/**
* @param mixed|null $initial
* @psalm-param T|null $initial
* @psalm-param callable(T, T, TKey):(T|null) $callback
*/
public function __construct(callable $callback, $initial = null)
{
Expand All @@ -34,11 +35,11 @@ public function __invoke(): Closure
/**
* @psalm-param \Iterator<TKey, T> $iterator
* @psalm-param callable(T, T, TKey):(T|null) $callable
*
* @param mixed $initial
* @psalm-param T|null $initial
*
* @psalm-return \Generator<int, T|null>
*
* @param mixed $initial
*/
static function (Iterator $iterator, callable $callback, $initial): Generator {
foreach ($iterator as $key => $value) {
Expand Down
1 change: 1 addition & 0 deletions src/Operation/Shuffle.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __invoke(): Closure
* @psalm-return \Generator<TKey, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
/** @psalm-var array<TKey, T> $data */
$data = iterator_to_array((new Run(new Wrap()))($iterator));

while ([] !== $data) {
Expand Down

0 comments on commit f87758e

Please sign in to comment.