From 58f610ab2b115f6cf4b2792ac1cd140851b57de9 Mon Sep 17 00:00:00 2001 From: Seiya Tajima Date: Sun, 16 Jan 2022 21:27:07 +0900 Subject: [PATCH] feat: support foreach with Support\Collection --- stubs/Collection.stubphp | 7 ++++ tests/acceptance/CollectionTypes.feature | 42 +++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/stubs/Collection.stubphp b/stubs/Collection.stubphp index 8f395ff..f6ead61 100644 --- a/stubs/Collection.stubphp +++ b/stubs/Collection.stubphp @@ -86,4 +86,11 @@ class Collection implements \ArrayAccess, Enumerable * @return $this */ public function put($key, $value) {} + + /** + * Get an iterator for the items. + * + * @return \ArrayIterator + */ + public function getIterator() {} } diff --git a/tests/acceptance/CollectionTypes.feature b/tests/acceptance/CollectionTypes.feature index 89f46f7..93c8a60 100644 --- a/tests/acceptance/CollectionTypes.feature +++ b/tests/acceptance/CollectionTypes.feature @@ -15,14 +15,16 @@ Feature: Collection types """ + And I have the following code preamble + """ + */ + $collection = new Collection(["key" => "value"]); + + foreach ($collection as $key => $value) { + /** @psalm-suppress UnusedFunctionCall we need type-check only */ + substr($key, 0); + + /** @psalm-suppress UnusedFunctionCall we need type-check only */ + substr($value, 0); + } + """ + When I run Psalm + Then I see no errors + + Scenario: Array like Collection can iterate with TKey, TValue + Given I have the following code + """ + /** @var Collection */ + $collection = new Collection(["data"]); + + foreach ($collection as $key => $value) { + /** @psalm-suppress UnusedFunctionCall we need type-check only */ + substr($value, $key); + } + """ + When I run Psalm + Then I see no errors