Skip to content

Commit

Permalink
refactor: Rename TypedIteratorAggregate into TypedIterableAggregate.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
drupol committed Jul 30, 2022
1 parent 64685ee commit 684a018
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The missing PHP iterators.
* `ResourceIteratorAggregate`
* `SimpleCachingIteratorAggregate`
* `StringIteratorAggregate`
* `TypedIteratorAggregate`
* `TypedIterableAggregate`
* `UniqueIterableAggregate`
* `UnpackIterableAggregate`

Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
<directory name="./src" />
<directory name="./tests/static-analysis" />
<ignoreFiles>
<directory name="vendor" />
<directory name="vendor" />
<directory name="tests/unit/" />
<file name="src/TypedIteratorAggregate.php" />
</ignoreFiles>
</projectFiles>
</psalm>
97 changes: 97 additions & 0 deletions src/TypedIterableAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace loophp\iterators;

use Generator;
use InvalidArgumentException;
use IteratorAggregate;

use function get_class;
use function gettype;
use function is_object;

/**
* @template TKey
* @template T
*
* @implements IteratorAggregate<TKey, T>
*/
final class TypedIterableAggregate implements IteratorAggregate
{
/**
* @var callable(mixed): string
*/
private $getType;

/**
* @var iterable<TKey, T>
*/
private iterable $iterable;

/**
* @param iterable<TKey, T> $iterable
* @param callable(mixed): string $getType
*/
public function __construct(iterable $iterable, ?callable $getType = null)
{
$this->iterable = $iterable;

$this->getType = $getType ??
/**
* @param mixed $variable
*/
static function ($variable): string {
if (!is_object($variable)) {
return gettype($variable);
}

$interfaces = class_implements($variable);

if ([] === $interfaces || false === $interfaces) {
return get_class($variable);
}

sort($interfaces);

return implode(',', $interfaces);
};
}

/**
* @return Generator<TKey, T>
*/
public function getIterator(): Generator
{
$previousType = null;

foreach ($this->iterable as $key => $value) {
if (null === $value) {
yield $key => $value;

continue;
}

$currentType = ($this->getType)($value);
$previousType ??= $currentType;

if ($currentType !== $previousType) {
throw new InvalidArgumentException(
sprintf(
"Detected mixed types: '%s' and '%s' !",
$previousType,
$currentType
)
);
}

yield $key => $value;
}
}
}
63 changes: 6 additions & 57 deletions src/TypedIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
namespace loophp\iterators;

use Generator;
use InvalidArgumentException;
use Iterator;
use IteratorAggregate;

use function get_class;
use function gettype;
use function is_object;

/**
* @deprecated Use TypedIterableAggregate for now on.
*
* @template TKey
* @template T
*
Expand All @@ -27,72 +24,24 @@
final class TypedIteratorAggregate implements IteratorAggregate
{
/**
* @var callable(mixed): string
*/
private $getType;

/**
* @var Iterator<TKey, T>
* @var TypedIterableAggregate<TKey, T>
*/
private Iterator $iterator;
private TypedIterableAggregate $iteratorAggregate;

/**
* @param Iterator<TKey, T> $iterator
* @param callable(mixed): string $getType
*/
public function __construct(Iterator $iterator, ?callable $getType = null)
{
$this->iterator = $iterator;

$this->getType = $getType ??
/**
* @param mixed $variable
*/
static function ($variable): string {
if (!is_object($variable)) {
return gettype($variable);
}

$interfaces = class_implements($variable);

if ([] === $interfaces || false === $interfaces) {
return get_class($variable);
}

sort($interfaces);

return implode(',', $interfaces);
};
$this->iteratorAggregate = new TypedIterableAggregate($iterator, $getType);
}

/**
* @return Generator<TKey, T>
*/
public function getIterator(): Generator
{
$previousType = null;

foreach ($this->iterator as $key => $value) {
if (null === $value) {
yield $key => $value;

continue;
}

$currentType = ($this->getType)($value);
$previousType ??= $currentType;

if ($currentType !== $previousType) {
throw new InvalidArgumentException(
sprintf(
"Detected mixed types: '%s' and '%s' !",
$previousType,
$currentType
)
);
}

yield $key => $value;
}
return $this->iteratorAggregate->getIterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace benchmarks\loophp\iterators;

use Generator;
use loophp\iterators\TypedIteratorAggregate;
use loophp\iterators\TypedIterableAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use PhpBench\Benchmark\Metadata\Annotations\Sleep;
Expand All @@ -19,7 +19,7 @@
* @Groups({"ci", "local"})
* @Sleep(500)
*/
final class TypedIteratorAggregateBench extends IteratorBenchmark
final class TypedIterableAggregateBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
Expand All @@ -36,8 +36,8 @@ public function provideGenerators(): Generator
{
$items = 2500;

yield 'TypedIteratorAggregate' => [
'class' => TypedIteratorAggregate::class,
yield 'TypedIterableAggregate' => [
'class' => TypedIterableAggregate::class,
'size' => $items,
];
}
Expand Down
Loading

0 comments on commit 684a018

Please sign in to comment.