Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more standard iterators #4320

Merged
merged 2 commits into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 128 additions & 0 deletions stubs/CoreGenericClasses.phpstub
Expand Up @@ -205,6 +205,134 @@ class FilterIterator extends IteratorIterator {
}


/**
* @template-covariant TKey
* @template-covariant TValue
*
* @template-implements OuterIterator<TKey, TValue>
* @template-implements ArrayAccess<TKey, TValue>
*
* @template-extends IteratorIterator<TKey, TValue>
*/
class CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/**
* @param Iterator<TKey, TValue> $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<TKey, TValue>
*
* @template-extends IteratorIterator<TKey, TValue>
*/
class InfiniteIterator extends IteratorIterator implements OuterIterator {
/**
* @param Iterator<TKey, TValue> $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<TKey, TValue>
*
* @template-extends IteratorIterator<TKey, TValue>
*/
class LimitIterator extends IteratorIterator implements OuterIterator {
/**
* @param Iterator<TKey, TValue> $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<TKey, TValue>
*
* @template-extends FilterIterator<TKey, TValue>
*/
class CallbackFilterIterator extends FilterIterator implements OuterIterator {
/**
* @param Iterator<TKey, TValue> $iterator
* @param callable(TValue, TKey, Iterator<TKey, TValue>): 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<TKey, TValue>
*/
class NoRewindIterator extends IteratorIterator {
/**
* @param Iterator<TKey, TValue> $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
Expand Down
80 changes: 80 additions & 0 deletions tests/Template/ClassTemplateTest.php
Expand Up @@ -16,6 +16,86 @@ class ClassTemplateTest extends TestCase
public function providerValidCodeParse(): iterable
{
return [
'cachingIterator' => [
'<?php

$input = range("a", "z");

$arrayIterator = new ArrayIterator($input);
$decoratorIterator = new CachingIterator($arrayIterator);
$next = $decoratorIterator->hasNext();
$key = $decoratorIterator->key();
$value = $decoratorIterator->current();
',
'assertions' => [
'$key' => 'int',
'$value' => 'string',
'$next' => 'bool',
],
],
'infiniteIterator' => [
'<?php

$input = range("a", "z");

$arrayIterator = new ArrayIterator($input);
$decoratorIterator = new InfiniteIterator($arrayIterator);
$key = $decoratorIterator->key();
$value = $decoratorIterator->current();
',
'assertions' => [
'$key' => 'int',
'$value' => 'string',
],
],
'limitIterator' => [
'<?php

$input = range("a", "z");

$arrayIterator = new ArrayIterator($input);
$decoratorIterator = new LimitIterator($arrayIterator, 1, 1);
$key = $decoratorIterator->key();
$value = $decoratorIterator->current();
',
'assertions' => [
'$key' => 'int',
'$value' => 'string',
],
],
'callbackFilterIterator' => [
'<?php

$input = range("a", "z");

$arrayIterator = new ArrayIterator($input);
$decoratorIterator = new CallbackFilterIterator(
$arrayIterator,
static function (string $value): bool {return "a" === $value;}
);
$key = $decoratorIterator->key();
$value = $decoratorIterator->current();
',
'assertions' => [
'$key' => 'int',
'$value' => 'string',
],
],
'noRewindIterator' => [
'<?php

$input = range("a", "z");

$arrayIterator = new ArrayIterator($input);
$decoratorIterator = new NoRewindIterator($arrayIterator);
$key = $decoratorIterator->key();
$value = $decoratorIterator->current();
',
'assertions' => [
'$key' => 'int',
'$value' => 'string',
],
],
'classTemplate' => [
'<?php
class A {}
Expand Down