From 082384bef0af271c4a4bef5f55fc4a1825b6c66b Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 6 Oct 2020 21:15:55 +0200 Subject: [PATCH] refactor: Update Has operation. BREAKING CHANGE: yes --- spec/loophp/collection/CollectionSpec.php | 4 ++-- src/Collection.php | 4 ++-- src/Contract/Operation/Hasable.php | 8 ++++++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 5c8c8d53b..54ebd1ade 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -1075,13 +1075,13 @@ public function it_can_has(): void ->has(static function ($key, $value) { return 'A'; }) - ->shouldReturn(true); + ->shouldIterateAs([true]); $this::fromIterable(range('A', 'C')) ->has(static function ($key, $value) { return 'Z'; }) - ->shouldReturn(false); + ->shouldIterateAs([false]); } public function it_can_head(): void diff --git a/src/Collection.php b/src/Collection.php index 3cdf7b876..95e2c7c36 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -517,9 +517,9 @@ public function groupBy(?callable $callable = null): CollectionInterface return $this->pipe(GroupBy::of()($callable)); } - public function has(callable $callback): bool + public function has(callable $callback): CollectionInterface { - return $this->pipe(Has::of()($callback))->getIterator()->current(); + return $this->pipe(Has::of()($callback)); } public function head(): CollectionInterface diff --git a/src/Contract/Operation/Hasable.php b/src/Contract/Operation/Hasable.php index b4804c26a..30aa088ea 100644 --- a/src/Contract/Operation/Hasable.php +++ b/src/Contract/Operation/Hasable.php @@ -4,6 +4,8 @@ namespace loophp\collection\Contract\Operation; +use loophp\collection\Contract\Collection; + /** * @psalm-template TKey * @psalm-template TKey of array-key @@ -12,7 +14,9 @@ interface Hasable { /** - * @psalm-param callable(TKey, T):(bool) $callback + * @psalm-param callable(TKey, T): bool $callback + * + * @psalm-return \loophp\collection\Contract\Collection */ - public function has(callable $callback): bool; + public function has(callable $callback): Collection; }