From 9186239477a0d826e515cfc3adf50c1bcd17ff90 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 17 Apr 2024 13:22:52 +0200 Subject: [PATCH 1/2] Repeat closure type --- src/ArrayCollection.php | 2 ++ src/Collection.php | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ArrayCollection.php b/src/ArrayCollection.php index 43b57541..2e7d0e3b 100644 --- a/src/ArrayCollection.php +++ b/src/ArrayCollection.php @@ -371,6 +371,8 @@ public function reduce(Closure $func, $initial = null) /** * {@inheritDoc} * + * @psalm-param Closure(T, TKey):bool $p + * * @return static * @psalm-return static */ diff --git a/src/Collection.php b/src/Collection.php index 5a763cea..c3c62aa5 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -96,6 +96,8 @@ public function map(Closure $func); /** * {@inheritDoc} * + * @psalm-param Closure(T, TKey):bool $p + * * @return Collection A collection with the results of the filter operation. * @psalm-return Collection */ @@ -103,7 +105,9 @@ public function filter(Closure $p); /** * {@inheritDoc} - + * + * @psalm-param Closure(TKey, T):bool $p + * * @return Collection[] An array with two elements. The first element contains the collection * of elements where the predicate returned TRUE, the second element * contains the collection of elements where the predicate returned FALSE. From 5dec3fd8069ce62ffe3373d8afcc1bca69ab0052 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 17 Apr 2024 15:38:14 +0200 Subject: [PATCH 2/2] Add tests --- phpstan.neon.dist | 1 + psalm.xml.dist | 1 + tests/StaticAnalysis/CustomCollection.php | 46 +++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/StaticAnalysis/CustomCollection.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 65e87120..8fdcca0d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,6 +2,7 @@ parameters: level: 8 paths: - src + - tests/StaticAnalysis ignoreErrors: - diff --git a/psalm.xml.dist b/psalm.xml.dist index bf24a173..aaa96c85 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -9,6 +9,7 @@ > + diff --git a/tests/StaticAnalysis/CustomCollection.php b/tests/StaticAnalysis/CustomCollection.php new file mode 100644 index 00000000..1aaee7e7 --- /dev/null +++ b/tests/StaticAnalysis/CustomCollection.php @@ -0,0 +1,46 @@ + + */ +abstract class CustomCollection implements Collection +{ + /** @var ArrayCollection */ + private ArrayCollection $collection; + + /** @param ArrayCollection $arrayCollection */ + public function __construct(ArrayCollection $arrayCollection) + { + $this->collection = $arrayCollection; + } + + /** + * @psalm-param Closure(T, TKey):bool $p + * + * @return Collection + */ + public function filter(Closure $p) + { + return $this->collection->filter($p); + } + + /** + * @psalm-param Closure(TKey, T):bool $p + * + * @psalm-return array{0: Collection, 1: Collection} + */ + public function partition(Closure $p) + { + return $this->collection->partition($p); + } +}