Skip to content

Commit 27ad1c2

Browse files
drupolmuglug
authored andcommitted
Add more standard iterators (#4320)
* Add CachingIterator, LimitIterator, InfiniteIterator, CallbackFilterIterator, NoRewindIterator. Signed-off-by: Pol Dellaiera <[email protected]> * Add related Iterator tests. Signed-off-by: Pol Dellaiera <[email protected]>
1 parent eb63e0c commit 27ad1c2

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

stubs/CoreGenericClasses.phpstub

+128
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,134 @@ class FilterIterator extends IteratorIterator {
205205
}
206206

207207

208+
/**
209+
* @template-covariant TKey
210+
* @template-covariant TValue
211+
*
212+
* @template-implements OuterIterator<TKey, TValue>
213+
* @template-implements ArrayAccess<TKey, TValue>
214+
*
215+
* @template-extends IteratorIterator<TKey, TValue>
216+
*/
217+
class CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
218+
/**
219+
* @param Iterator<TKey, TValue> $iterator
220+
*/
221+
public function __construct(Iterator $iterator) {}
222+
223+
/** @return bool */
224+
public function hasNext () {}
225+
226+
/**
227+
* @return TValue Can return any type.
228+
*/
229+
public function current() {}
230+
231+
/**
232+
* @return TKey scalar on success, or null on failure.
233+
*/
234+
public function key() {}
235+
}
236+
237+
/**
238+
* @template-covariant TKey
239+
* @template-covariant TValue
240+
*
241+
* @template-implements OuterIterator<TKey, TValue>
242+
*
243+
* @template-extends IteratorIterator<TKey, TValue>
244+
*/
245+
class InfiniteIterator extends IteratorIterator implements OuterIterator {
246+
/**
247+
* @param Iterator<TKey, TValue> $iterator
248+
*/
249+
public function __construct(Iterator $iterator) {}
250+
251+
/**
252+
* @return TValue Can return any type.
253+
*/
254+
public function current() {}
255+
256+
/**
257+
* @return TKey scalar on success, or null on failure.
258+
*/
259+
public function key() {}
260+
}
261+
262+
/**
263+
* @template-covariant TKey
264+
* @template-covariant TValue
265+
*
266+
* @template-implements OuterIterator<TKey, TValue>
267+
*
268+
* @template-extends IteratorIterator<TKey, TValue>
269+
*/
270+
class LimitIterator extends IteratorIterator implements OuterIterator {
271+
/**
272+
* @param Iterator<TKey, TValue> $iterator
273+
*/
274+
public function __construct(Iterator $iterator, int $offset = 0, int $count = -1) {}
275+
276+
/**
277+
* @return TValue Can return any type.
278+
*/
279+
public function current() {}
280+
281+
/**
282+
* @return TKey scalar on success, or null on failure.
283+
*/
284+
public function key() {}
285+
}
286+
287+
/**
288+
* @template-covariant TKey
289+
* @template-covariant TValue
290+
*
291+
* @template-implements OuterIterator<TKey, TValue>
292+
*
293+
* @template-extends FilterIterator<TKey, TValue>
294+
*/
295+
class CallbackFilterIterator extends FilterIterator implements OuterIterator {
296+
/**
297+
* @param Iterator<TKey, TValue> $iterator
298+
* @param callable(TValue, TKey, Iterator<TKey, TValue>): bool $callback
299+
*/
300+
public function __construct(Iterator $iterator, callable $callback) {}
301+
302+
/**
303+
* @return TValue Can return any type.
304+
*/
305+
public function current() {}
306+
307+
/**
308+
* @return TKey scalar on success, or null on failure.
309+
*/
310+
public function key() {}
311+
}
312+
313+
/**
314+
* @template-covariant TKey
315+
* @template-covariant TValue
316+
*
317+
* @template-extends IteratorIterator<TKey, TValue>
318+
*/
319+
class NoRewindIterator extends IteratorIterator {
320+
/**
321+
* @param Iterator<TKey, TValue> $iterator
322+
*/
323+
public function __construct(Iterator $iterator) {}
324+
325+
/**
326+
* @return TValue Can return any type.
327+
*/
328+
public function current() {}
329+
330+
/**
331+
* @return TKey scalar on success, or null on failure.
332+
*/
333+
public function key() {}
334+
}
335+
208336
/**
209337
* @template-covariant TKey
210338
* @template-covariant TValue

tests/Template/ClassTemplateTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,86 @@ class ClassTemplateTest extends TestCase
1616
public function providerValidCodeParse(): iterable
1717
{
1818
return [
19+
'cachingIterator' => [
20+
'<?php
21+
22+
$input = range("a", "z");
23+
24+
$arrayIterator = new ArrayIterator($input);
25+
$decoratorIterator = new CachingIterator($arrayIterator);
26+
$next = $decoratorIterator->hasNext();
27+
$key = $decoratorIterator->key();
28+
$value = $decoratorIterator->current();
29+
',
30+
'assertions' => [
31+
'$key' => 'int',
32+
'$value' => 'string',
33+
'$next' => 'bool',
34+
],
35+
],
36+
'infiniteIterator' => [
37+
'<?php
38+
39+
$input = range("a", "z");
40+
41+
$arrayIterator = new ArrayIterator($input);
42+
$decoratorIterator = new InfiniteIterator($arrayIterator);
43+
$key = $decoratorIterator->key();
44+
$value = $decoratorIterator->current();
45+
',
46+
'assertions' => [
47+
'$key' => 'int',
48+
'$value' => 'string',
49+
],
50+
],
51+
'limitIterator' => [
52+
'<?php
53+
54+
$input = range("a", "z");
55+
56+
$arrayIterator = new ArrayIterator($input);
57+
$decoratorIterator = new LimitIterator($arrayIterator, 1, 1);
58+
$key = $decoratorIterator->key();
59+
$value = $decoratorIterator->current();
60+
',
61+
'assertions' => [
62+
'$key' => 'int',
63+
'$value' => 'string',
64+
],
65+
],
66+
'callbackFilterIterator' => [
67+
'<?php
68+
69+
$input = range("a", "z");
70+
71+
$arrayIterator = new ArrayIterator($input);
72+
$decoratorIterator = new CallbackFilterIterator(
73+
$arrayIterator,
74+
static function (string $value): bool {return "a" === $value;}
75+
);
76+
$key = $decoratorIterator->key();
77+
$value = $decoratorIterator->current();
78+
',
79+
'assertions' => [
80+
'$key' => 'int',
81+
'$value' => 'string',
82+
],
83+
],
84+
'noRewindIterator' => [
85+
'<?php
86+
87+
$input = range("a", "z");
88+
89+
$arrayIterator = new ArrayIterator($input);
90+
$decoratorIterator = new NoRewindIterator($arrayIterator);
91+
$key = $decoratorIterator->key();
92+
$value = $decoratorIterator->current();
93+
',
94+
'assertions' => [
95+
'$key' => 'int',
96+
'$value' => 'string',
97+
],
98+
],
1999
'classTemplate' => [
20100
'<?php
21101
class A {}

0 commit comments

Comments
 (0)