Merge pull request #209 from tjmmm/iterate_collection

feat: support foreach with Support\Collection
This commit is contained in:
feek 2022-01-18 16:30:18 -05:00 committed by GitHub
commit da41cde783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 5 deletions

View File

@ -86,4 +86,11 @@ class Collection implements \ArrayAccess, Enumerable
* @return $this
*/
public function put($key, $value) {}
/**
* Get an iterator for the items.
*
* @return \ArrayIterator<TKey, TValue>
*/
public function getIterator() {}
}

View File

@ -15,14 +15,16 @@ Feature: Collection types
</plugins>
</psalm>
"""
And I have the following code preamble
"""
<?php declare(strict_types=1);
Scenario:
use Illuminate\Support\Collection;
"""
Scenario: Collection has TKey, TValue
Given I have the following code
"""
<?php declare(strict_types=1);
use Illuminate\Support\Collection;
final class CollectionTypes
{
/**
@ -109,3 +111,33 @@ Feature: Collection types
When I run Psalm
Then I see no errors
Scenario: Dict like Collection can iterate with TKey, TValue
Given I have the following code
"""
/** @var Collection<string, string> */
$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<int, string> */
$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