From 2119817003b44c9bd1ccc0657ea1a735ad5362cb Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 4 Feb 2021 10:30:14 +0100 Subject: [PATCH] Chore: Add types to iterable() and IterableObject --- phpstan.neon.dist | 7 +++++++ psalm-baseline.xml | 13 +++++++++++++ psalm.xml.dist | 1 + src/IterableObject.php | 29 ++++++++++++++++++++--------- src/iterable-functions.php | 7 ++++++- 5 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 psalm-baseline.xml diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 8ea502f..7e29c96 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,3 +3,10 @@ parameters: paths: - %currentWorkingDirectory%/src - %currentWorkingDirectory%/tests + + ignoreErrors: + # https://github.com/phpstan/phpstan/issues/4498 + - '~Method BenTools\\IterableFunctions\\IterableObject::filter\(\) should return BenTools\\IterableFunctions\\IterableObject but returns BenTools\\IterableFunctions\\IterableObject~' + - '~Method BenTools\\IterableFunctions\\IterableObject::map\(\) should return BenTools\\IterableFunctions\\IterableObject but returns BenTools\\IterableFunctions\\IterableObject~' + + - '~Function BenTools\\IterableFunctions\\iterable_map\(\) should return iterable but returns array\|BenTools\\IterableFunctions\\IterableObject~' diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 0000000..555826d --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,13 @@ + + + + + is_array($iterable) ? $filtered->asArray() : $filtered + is_array($iterable) ? $mapped->asArray() : $mapped + + + iterable<TKey, TResult> + iterable<TKey, TValue> + + + diff --git a/psalm.xml.dist b/psalm.xml.dist index 3e6a115..02ceeb4 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -3,6 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + errorBaseline="psalm-baseline.xml" > diff --git a/src/IterableObject.php b/src/IterableObject.php index 59cc296..b0e4796 100644 --- a/src/IterableObject.php +++ b/src/IterableObject.php @@ -16,21 +16,27 @@ /** * @internal * - * @implements IteratorAggregate + * @template TKey + * @template TValue + * + * @implements IteratorAggregate */ final class IterableObject implements IteratorAggregate { - /** @var iterable */ + /** @var iterable */ private $iterable; - /** - * @param iterable $iterable - */ + /** @param iterable $iterable */ public function __construct(iterable $iterable) { $this->iterable = $iterable; } + /** + * @param (callable(TValue):bool)|null $filter + * + * @return self + */ public function filter(?callable $filter = null): self { if ($this->iterable instanceof Traversable) { @@ -48,6 +54,13 @@ static function ($value): bool { return new self($filtered); } + /** + * @param callable(TValue):TResult $mapper + * + * @return self + * + * @template TResult + */ public function map(callable $mapper): self { if ($this->iterable instanceof Traversable) { @@ -57,15 +70,13 @@ public function map(callable $mapper): self return new self(array_map($mapper, $this->iterable)); } - /** - * @return Traversable - */ + /** @return Traversable */ public function getIterator(): Traversable { yield from $this->iterable; } - /** @return array */ + /** @return array */ public function asArray(): array { return $this->iterable instanceof Traversable ? iterator_to_array($this->iterable) : $this->iterable; diff --git a/src/iterable-functions.php b/src/iterable-functions.php index e244236..f7f379c 100644 --- a/src/iterable-functions.php +++ b/src/iterable-functions.php @@ -115,7 +115,12 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null) } /** - * @param iterable $iterable + * @param iterable|null $iterable + * + * @return IterableObject + * + * @template TKey + * @template TValue */ function iterable(?iterable $iterable): IterableObject {