Skip to content

Commit

Permalink
Add more standard iterators (#4320)
Browse files Browse the repository at this point in the history
* Add CachingIterator, LimitIterator, InfiniteIterator, CallbackFilterIterator, NoRewindIterator.

Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>

* Add related Iterator tests.

Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol authored and muglug committed Oct 15, 2020
1 parent eb63e0c commit 27ad1c2
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
128 changes: 128 additions & 0 deletions stubs/CoreGenericClasses.phpstub
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 27ad1c2

Please sign in to comment.