Skip to content

Commit 0315dfa

Browse files
committed
More SPL iterator stubs
1 parent e671cc0 commit 0315dfa

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

stubs/iterable.stub

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,32 @@ class RecursiveIteratorIterator
159159
}
160160

161161
}
162+
163+
/**
164+
* @template-covariant TKey
165+
* @template-covariant TValue
166+
*
167+
* @template-extends Iterator<TKey, TValue>
168+
*/
169+
interface OuterIterator extends Iterator {
170+
/**
171+
* @return Iterator<TKey, TValue>
172+
*/
173+
public function getInnerIterator();
174+
}
175+
176+
/**
177+
* @template-covariant TKey
178+
* @template-covariant TValue
179+
* @template TIterator as Traversable<TKey, TValue>
180+
*
181+
* @template-implements OuterIterator<TKey, TValue>
182+
*
183+
* @mixin TIterator
184+
*/
185+
class IteratorIterator implements OuterIterator {
186+
/**
187+
* @param TIterator $iterator
188+
*/
189+
public function __construct(Traversable $iterator) {}
190+
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10956,6 +10956,11 @@ public function dataNestedGenericIncompleteConstructor(): array
1095610956
return $this->gatherAssertTypes(__DIR__ . '/data/nested-generic-incomplete-constructor.php');
1095710957
}
1095810958

10959+
public function dataIteratorIterator(): array
10960+
{
10961+
return $this->gatherAssertTypes(__DIR__ . '/data/iterator-iterator.php');
10962+
}
10963+
1095910964
/**
1096010965
* @param string $file
1096110966
* @return array<string, mixed[]>
@@ -11208,6 +11213,7 @@ private function gatherAssertTypes(string $file): array
1120811213
* @dataProvider dataBug3922
1120911214
* @dataProvider dataNestedGenericTypesUnwrapping
1121011215
* @dataProvider dataNestedGenericIncompleteConstructor
11216+
* @dataProvider dataIteratorIterator
1121111217
* @param string $assertType
1121211218
* @param string $file
1121311219
* @param mixed ...$args
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace IteratorIteratorTest;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param \ArrayIterator<int, string> $it
12+
*/
13+
public function doFoo(\ArrayIterator $it): void
14+
{
15+
$iteratorIterator = new \IteratorIterator($it);
16+
assertType('Iterator<int, string>', $iteratorIterator->getInnerIterator());
17+
assertType('array<int, string>', $iteratorIterator->getArrayCopy());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)