diff --git a/stubs/CoreGenericClasses.phpstub b/stubs/CoreGenericClasses.phpstub index acab9ac00ef..2a131d2f94c 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 4cda5d87f60..b59512b4a82 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' => [ '