Skip to content

Commit

Permalink
Update annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 8, 2020
1 parent 47b2f55 commit 3830e38
Show file tree
Hide file tree
Showing 42 changed files with 345 additions and 200 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"psr/cache": "^1.0",
"symfony/cache": "^4.4 || ^5.1",
"symfony/polyfill-mbstring": "^1.18",
"vimeo/psalm": "^3.14.2"
"vimeo/psalm": "dev-master"
},
"suggest": {
"symfony/polyfill-mbstring": "Use it if you do not have the PHP mbstring extension."
Expand Down
30 changes: 24 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,31 @@ public function associate(
?callable $callbackForKeys = null,
?callable $callbackForValues = null
): CollectionInterface {
$callbackForKeys = $callbackForKeys ?? static function ($key, $value) {
return $key;
};
$callbackForKeys = $callbackForKeys ??
/**
* @psalm-param TKey $key
* @psalm-param T $value
* @psalm-return TKey
*
* @param mixed $key
* @param mixed $value
*/
static function ($key, $value) {
return $key;
};

$callbackForValues = $callbackForValues ?? static function ($key, $value) {
return $value;
};
$callbackForValues = $callbackForValues ??
/**
* @psalm-param TKey $key
* @psalm-param T $value
* @psalm-return T
*
* @param mixed $key
* @param mixed $value
*/
static function ($key, $value) {
return $value;
};

return $this->run(Associate::of()($callbackForKeys)($callbackForValues));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public function __invoke(): Closure
return
/**
* @psalm-param T ...$items
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
static function (...$items): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($items): Generator {
foreach ($iterator as $key => $value) {
Expand Down
2 changes: 2 additions & 0 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __invoke(): Closure
return
/**
* @psalm-param callable(T, TKey):(bool) ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable ...$callbacks): Closure {
return
Expand Down
26 changes: 15 additions & 11 deletions src/Operation/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ final class Cache extends AbstractOperation
*/
public function __invoke(): Closure
{
return static function (CacheItemPoolInterface $cache): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($cache): Generator {
return yield from new CacheIterator($iterator, $cache);
};
};
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (CacheItemPoolInterface $cache): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($cache): Generator {
return yield from new CacheIterator($iterator, $cache);
};
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param int ...$sizes
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
static function (int ...$sizes): Closure {
return
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class Collapse extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<array-key, T|iterable<TKey, T>>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class Combine extends AbstractOperation
{
/**
* @return Closure(T...): Closure(Iterator<TKey, T>): Generator<T, T>
* @psalm-return Closure(T...): Closure(Iterator<TKey, T>): Generator<T, T>
*/
public function __invoke(): Closure
{
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public function __invoke(): Closure
return
/**
* @psalm-param T ...$values
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public function __invoke(): Closure
return
/**
* @psalm-param callable(Iterator<TKey, T>): Generator<TKey, T> ...$operations
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable ...$operations): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($operations): Generator {
Expand Down
5 changes: 5 additions & 0 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
final class Contains extends AbstractOperation
{
/**
* @psalm-return Closure(T...): Closure(Iterator<TKey, T>): Generator<int, bool>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param T ...$values
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, bool>
*/
static function (...$values): Closure {
return
Expand Down
2 changes: 2 additions & 0 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __invoke(): Closure
return
/**
* @psalm-param T ...$values
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
Expand Down
4 changes: 4 additions & 0 deletions src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public function __invoke(): Closure
return
/**
* @psalm-param T ...$values
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
$filterCallbackFactory = static function (array $values): Closure {
Expand Down
26 changes: 15 additions & 11 deletions src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ final class Drop extends AbstractOperation
*/
public function __invoke(): Closure
{
return static function (int ...$offsets): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
return yield from new LimitIterator($iterator, (int) array_sum($offsets));
};
};
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (int ...$offsets): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
return yield from new LimitIterator($iterator, (int) array_sum($offsets));
};
};
}
}
5 changes: 5 additions & 0 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
final class Explode extends AbstractOperation
{
/**
* @psalm-return Closure(T...): Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param T ...$explodes
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
static function (...$explodes): Closure {
return
Expand Down
1 change: 1 addition & 0 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __invoke(): Closure
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, bool> $iterator
*/
static function (Iterator $iterator): Generator {
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public function __invoke(): Closure
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable ...$callbacks): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
Expand Down
21 changes: 15 additions & 6 deletions src/Operation/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@
*/
final class First extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return static function (Iterator $iterator): Generator {
if (!$iterator->valid()) {
return yield from [];
}
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
if (!$iterator->valid()) {
return yield from [];
}

return yield $iterator->key() => $iterator->current();
};
return yield $iterator->key() => $iterator->current();
};
}
}
62 changes: 33 additions & 29 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,41 @@ final class Flatten extends AbstractOperation
*/
public function __invoke(): Closure
{
return static function (int $depth): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, T>
*/
static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
} elseif (1 === $depth) {
/** @psalm-var TKey $subKey */
/** @psalm-var T $subValue */
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
} else {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $flatten */
$flatten = Flatten::of()($depth - 1);
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, T>
*/
static function (int $depth): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, T>
*/
static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
} elseif (1 === $depth) {
/** @psalm-var TKey $subKey */
/** @psalm-var T $subValue */
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
} else {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $flatten */
$flatten = Flatten::of()($depth - 1);

/**
* @psalm-var TKey $subKey
* @psalm-var T $subValue
*/
foreach ($flatten(new IterableIterator($value)) as $subKey => $subValue) {
yield $subKey => $subValue;
/**
* @psalm-var TKey $subKey
* @psalm-var T $subValue
*/
foreach ($flatten(new IterableIterator($value)) as $subKey => $subValue) {
yield $subKey => $subValue;
}
}
}
}
};
};
};
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class Flip extends AbstractOperation
{
/**
* @return Closure(Iterator<TKey, T>): Generator<T, TKey>
* @psalm-return Closure(Iterator<TKey, T>): Generator<T, TKey>
*/
public function __invoke(): Closure
{
Expand Down

0 comments on commit 3830e38

Please sign in to comment.