Skip to content

Commit

Permalink
Add more Psalm annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 5, 2020
1 parent aed338c commit e7e7898
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
* @template-extends Splitable<TKey, T>
* @template-extends Tailable<TKey, T>
* @template-extends Transposeable<TKey, T>
* @template-extends Unpairable<TKey, T>
* @template-extends Untilable<TKey, T>
* @template-extends Unwrapable<TKey, T>
* @template-extends Walkable<TKey, T>
Expand Down
21 changes: 17 additions & 4 deletions src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace loophp\collection\Iterator;

use Iterator;
use OuterIterator;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

Expand All @@ -14,8 +15,9 @@
* @psalm-template T
*
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class CacheIterator implements Iterator
final class CacheIterator implements Iterator, OuterIterator
{
/**
* @var CacheItemPoolInterface
Expand All @@ -34,7 +36,7 @@ final class CacheIterator implements Iterator
private $key;

/**
* @psalm-param \Iterator<TKey, T> $iterator
* @psalm-param Iterator<TKey, T> $iterator
*/
public function __construct(Iterator $iterator, CacheItemPoolInterface $cache)
{
Expand All @@ -48,15 +50,26 @@ public function __construct(Iterator $iterator, CacheItemPoolInterface $cache)
*/
public function current()
{
return $this->getItemOrSave((string) $this->key)->get()[1];
/** @psalm-var array{TKey, T} $data */
$data = $this->getItemOrSave((string) $this->key)->get();

return $data[1];
}

public function getInnerIterator(): Iterator
{
return $this->inner;
}

/**
* @psalm-return TKey
*/
public function key()
{
return $this->getItemOrSave((string) $this->key)->get()[0];
/** @psalm-var array{TKey, T} $data */
$data = $this->getItemOrSave((string) $this->key)->get();

return $data[0];
}

public function next(): void
Expand Down
10 changes: 6 additions & 4 deletions src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class ClosureIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand All @@ -26,20 +28,20 @@ final class ClosureIterator extends ProxyIterator implements Iterator, OuterIter

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

/**
* @var Closure
* @psalm-var Closure(callable(T...):Generator<TKey, T>, list<T>):(Generator<TKey, T>)
* @psalm-var Closure(callable(T...):\Generator<TKey, T>, list<T>):(\Generator<TKey, T>)
*/
private $generator;

/**
* @param mixed ...$arguments
* @psalm-param T ...$arguments
* @psalm-param callable(mixed...):(Generator<TKey,T>) $callable
* @psalm-param callable(T...):(\Generator<TKey,T>) $callable
*/
public function __construct(callable $callable, ...$arguments)
{
Expand All @@ -60,7 +62,7 @@ public function rewind(): void
/**
* Init the generator if not initialized yet.
*
* @return Generator<Generator<TKey, T>>
* @psalm-return \Generator<TKey, T>
*/
private function getGenerator(): Generator
{
Expand Down
5 changes: 5 additions & 0 deletions src/Iterator/IterableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class IterableIterator extends ProxyIterator implements Iterator, OuterIterator
{
Expand All @@ -24,6 +26,9 @@ final class IterableIterator extends ProxyIterator implements Iterator, OuterIte
public function __construct(iterable $iterable)
{
$this->iterator = new ClosureIterator(
/**
* @psalm-param iterable<TKey, T> $iterable
*/
static function (iterable $iterable): Generator {
foreach ($iterable as $key => $value) {
yield $key => $value;
Expand Down
11 changes: 9 additions & 2 deletions src/Iterator/ProxyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@
use Generator;
use Iterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
abstract class ProxyIterator
{
/**
* @var Generator<mixed>|\loophp\collection\Iterator\ClosureIterator
* @var Generator<TKey, T>|Iterator<TKey, T>
*/
protected $iterator;

/**
* @return mixed
* @psalm-return T
*/
public function current()
{
return $this->iterator->current();
}

/**
* @return Iterator<mixed>
* @psalm-return \Iterator<TKey, T>
*/
public function getInnerIterator(): Iterator
{
Expand All @@ -32,6 +38,7 @@ public function getInnerIterator(): Iterator

/**
* @return mixed
* @psalm-return TKey
*/
public function key()
{
Expand Down
12 changes: 9 additions & 3 deletions src/Iterator/ResourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
use OuterIterator;

/**
* @implements Iterator<mixed>
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class ResourceIterator extends ProxyIterator implements Iterator, OuterIterator
{
/**
* ResourceIterator constructor.
*
* @param resource $resource
*/
public function __construct($resource)
{
$closure =
/**
* @param resource $resource
*
* @psalm-return \Generator<int, T>
*/
static function ($resource): Generator {
while (false !== $chunk = fgetc($resource)) {
Expand Down
14 changes: 10 additions & 4 deletions src/Iterator/SortableIterableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
use IteratorAggregate;

/**
* Class SortableIterableIterator.
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @implements IteratorAggregate<ArrayIterator>
* @implements IteratorAggregate<ArrayIterator<TKey, T>>
*/
final class SortableIterableIterator implements IteratorAggregate
{
/**
* @var callable
* @psalm-var callable(T, T):(int) $callable
*/
private $callable;

/**
* @var \loophp\collection\Iterator\IterableIterator
* @psalm-var \loophp\collection\Iterator\IterableIterator<TKey, T>
*/
private $iterator;

/**
* SortableIterator constructor.
*
* @param iterable<mixed> $iterable
* @psalm-param iterable<TKey, T> $iterable
* @psalm-param callable(T, T):(int) $callable
*/
public function __construct(iterable $iterable, callable $callable)
{
Expand All @@ -37,6 +41,8 @@ public function __construct(iterable $iterable, callable $callable)

/**
* {@inheritdoc}
*
* @psalm-return \ArrayIterator<TKey, T>
*/
public function getIterator()
{
Expand Down
11 changes: 8 additions & 3 deletions src/Iterator/StringIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
* @psalm-template T of string
*
* @extends ProxyIterator<TKey, T>
* @implements \Iterator<TKey, T>
* @implements \OuterIterator<TKey, T>
*/
final class StringIterator extends ProxyIterator implements Iterator, OuterIterator
{
public function __construct(string $data, string $delimiter = '')
{
$this->iterator = new ClosureIterator(
/**
* @psalm-return \Generator<int, string>
*/
static function (string $input, string $delimiter): Generator {
$offset = 0;

Expand All @@ -28,7 +33,7 @@ static function (string $input, string $delimiter): Generator {
1;

while (mb_strlen($input) > $offset && false !== $nextOffset) {
yield mb_substr($input, $offset, $nextOffset - $offset);
yield (string) mb_substr($input, $offset, $nextOffset - $offset);
$offset = $nextOffset + mb_strlen($delimiter);

$nextOffset = '' !== $delimiter ?
Expand All @@ -37,7 +42,7 @@ static function (string $input, string $delimiter): Generator {
}

if ('' !== $delimiter) {
yield mb_substr($input, $offset);
yield (string) mb_substr($input, $offset);
}
},
$data,
Expand Down

0 comments on commit e7e7898

Please sign in to comment.