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

PHPLIB-1184: Improve CallbackIterator #1126

Merged
merged 6 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Model/CallbackIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,54 @@

namespace MongoDB\Model;

use Closure;
use Iterator;
use IteratorIterator;
use ReturnTypeWillChange;
use Traversable;

use function call_user_func;

/**
* Iterator to apply a callback before returning an element
*
* @internal
*
* @template TKey
* @template TValue
* @template TCallbackValue
* @template-implements Iterator<TKey, TCallbackValue>
*/
class CallbackIterator implements Iterator
{
/** @var Closure */
private $callback;
/** @var callable(TValue, TKey): TCallbackValue */
private $callable;
GromNaN marked this conversation as resolved.
Show resolved Hide resolved

/** @var Iterator */
/** @var Iterator<TKey, TValue> */
private $iterator;

public function __construct(Traversable $traversable, Closure $callback)
/**
* @param Traversable<TKey, TValue> $traversable
* @param callable(TValue, TKey): TCallbackValue $callable
*/
public function __construct(Traversable $traversable, callable $callable)
{
$this->iterator = $traversable instanceof Iterator ? $traversable : new IteratorIterator($traversable);
$this->callback = $callback;
$this->callable = $callable;
}

/**
* @see https://php.net/iterator.current
* @return mixed
* @return TCallbackValue
*/
#[ReturnTypeWillChange]
public function current()
{
return ($this->callback)($this->iterator->current());
return call_user_func($this->callable, $this->iterator->current(), $this->iterator->key());
}

/**
* @see https://php.net/iterator.key
* @return mixed
* @return TKey
*/
#[ReturnTypeWillChange]
public function key()
Expand Down
70 changes: 70 additions & 0 deletions tests/Model/CallbackIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MongoDB\Tests\Model;

use ArrayIterator;
use MongoDB\Model\CallbackIterator;
use MongoDB\Tests\TestCase;

use function array_keys;
use function iterator_to_array;
use function strrev;

class CallbackIteratorTest extends TestCase
{
public function testArrayIteration(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose rewriting all of these tests to use the following matrix:

  • Callbacks in various forms that apply the same transformation (*= 2 seems fine)
    • Closure
    • Object method (e.g. static method on this class)
  • Iterables
    • ArrayIterator constructed from an indexed array
    • ArrayIterator constructed from an associative array (to show that keys are preserved)
    • IteratorAggregate for both of the above (per @GromNaN's suggestion below)

Using a data provider for iterables and expected arrays would probably be most efficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alcaeus I can make the changes if it frees you up.

{
$expectedKey = 0;

$original = [1, 2, 3];

$callbackIterator = new CallbackIterator(
new ArrayIterator($original),
function ($value, $key) use (&$expectedKey) {
$this->assertSame($expectedKey, $key);
$expectedKey++;

return $value * 2;
}
);

$this->assertSame([2, 4, 6], iterator_to_array($callbackIterator));
}

public function testHashIteration(): void
{
$expectedKey = 0;

$original = ['a' => 1, 'b' => 2, 'c' => 3];
$expectedKeys = array_keys($original);

$callbackIterator = new CallbackIterator(
new ArrayIterator($original),
function ($value, $key) use (&$expectedKey, $expectedKeys) {
$this->assertSame($expectedKeys[$expectedKey], $key);
$expectedKey++;

return $value * 2;
}
);

$this->assertSame(['a' => 2, 'b' => 4, 'c' => 6], iterator_to_array($callbackIterator));
}

public function testWithCallable(): void
{
$original = ['foo', 'bar', 'baz'];

$callbackIterator = new CallbackIterator(
new ArrayIterator($original),
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
[self::class, 'reverseValue']
);

$this->assertSame(['oof', 'rab', 'zab'], iterator_to_array($callbackIterator));
}

public static function reverseValue($value, $key)
{
return strrev($value);
}
}