From 2b437ad6729f72754b9fb2339843e1fd59d52b9c Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 13 Oct 2020 17:35:49 +0200 Subject: [PATCH] Add more standard iterators (#4320) * Add CachingIterator, LimitIterator, InfiniteIterator, CallbackFilterIterator, NoRewindIterator. Signed-off-by: Pol Dellaiera * Add related Iterator tests. Signed-off-by: Pol Dellaiera --- stubs/CoreGenericClasses.phpstub | 128 +++++++++++++++++++++++++++ tests/Template/ClassTemplateTest.php | 80 +++++++++++++++++ 2 files changed, 208 insertions(+) diff --git a/stubs/CoreGenericClasses.phpstub b/stubs/CoreGenericClasses.phpstub index acab9ac00..2a131d2f9 100644 --- a/stubs/CoreGenericClasses.phpstub +++ b/stubs/CoreGenericClasses.phpstub @@ -205,6 +205,134 @@ class FilterIterator extends IteratorIterator { } +/** + * @template-covariant TKey + * @template-covariant TValue + * + * @template-implements OuterIterator + * @template-implements ArrayAccess + * + * @template-extends IteratorIterator + */ +class CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable { + /** + * @param Iterator $iterator + */ + public function __construct(Iterator $iterator) {} + + /** @return bool */ + public function hasNext () {} + + /** + * @return TValue Can return any type. + */ + public function current() {} + + /** + * @return TKey scalar on success, or null on failure. + */ + public function key() {} +} + +/** + * @template-covariant TKey + * @template-covariant TValue + * + * @template-implements OuterIterator + * + * @template-extends IteratorIterator + */ +class InfiniteIterator extends IteratorIterator implements OuterIterator { + /** + * @param Iterator $iterator + */ + public function __construct(Iterator $iterator) {} + + /** + * @return TValue Can return any type. + */ + public function current() {} + + /** + * @return TKey scalar on success, or null on failure. + */ + public function key() {} +} + +/** + * @template-covariant TKey + * @template-covariant TValue + * + * @template-implements OuterIterator + * + * @template-extends IteratorIterator + */ +class LimitIterator extends IteratorIterator implements OuterIterator { + /** + * @param Iterator $iterator + */ + public function __construct(Iterator $iterator, int $offset = 0, int $count = -1) {} + + /** + * @return TValue Can return any type. + */ + public function current() {} + + /** + * @return TKey scalar on success, or null on failure. + */ + public function key() {} +} + +/** + * @template-covariant TKey + * @template-covariant TValue + * + * @template-implements OuterIterator + * + * @template-extends FilterIterator + */ +class CallbackFilterIterator extends FilterIterator implements OuterIterator { + /** + * @param Iterator $iterator + * @param callable(TValue, TKey, Iterator): bool $callback + */ + public function __construct(Iterator $iterator, callable $callback) {} + + /** + * @return TValue Can return any type. + */ + public function current() {} + + /** + * @return TKey scalar on success, or null on failure. + */ + public function key() {} +} + +/** + * @template-covariant TKey + * @template-covariant TValue + * + * @template-extends IteratorIterator + */ +class NoRewindIterator extends IteratorIterator { + /** + * @param Iterator $iterator + */ + public function __construct(Iterator $iterator) {} + + /** + * @return TValue Can return any type. + */ + public function current() {} + + /** + * @return TKey scalar on success, or null on failure. + */ + public function key() {} +} + /** * @template-covariant TKey * @template-covariant TValue diff --git a/tests/Template/ClassTemplateTest.php b/tests/Template/ClassTemplateTest.php index 4cda5d87f..b59512b4a 100644 --- a/tests/Template/ClassTemplateTest.php +++ b/tests/Template/ClassTemplateTest.php @@ -16,6 +16,86 @@ class ClassTemplateTest extends TestCase public function providerValidCodeParse(): iterable { return [ + 'cachingIterator' => [ + 'hasNext(); + $key = $decoratorIterator->key(); + $value = $decoratorIterator->current(); + ', + 'assertions' => [ + '$key' => 'int', + '$value' => 'string', + '$next' => 'bool', + ], + ], + 'infiniteIterator' => [ + 'key(); + $value = $decoratorIterator->current(); + ', + 'assertions' => [ + '$key' => 'int', + '$value' => 'string', + ], + ], + 'limitIterator' => [ + 'key(); + $value = $decoratorIterator->current(); + ', + 'assertions' => [ + '$key' => 'int', + '$value' => 'string', + ], + ], + 'callbackFilterIterator' => [ + 'key(); + $value = $decoratorIterator->current(); + ', + 'assertions' => [ + '$key' => 'int', + '$value' => 'string', + ], + ], + 'noRewindIterator' => [ + 'key(); + $value = $decoratorIterator->current(); + ', + 'assertions' => [ + '$key' => 'int', + '$value' => 'string', + ], + ], 'classTemplate' => [ '