CachingIteratorAggregate::getIterator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 4
Bugs 3 Features 0
Metric Value
cc 2
eloc 2
c 4
b 3
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\iterators;
11
12
use Generator;
13
use Iterator;
14
use IteratorAggregate;
15
16
/**
17
 * @template TKey
18
 * @template T
19
 *
20
 * @implements IteratorAggregate<TKey, T>
21
 */
22
final class CachingIteratorAggregate implements IteratorAggregate
23
{
24
    /**
25
     * @var SimpleCachingIteratorAggregate<int, array{0: TKey, 1: T}>
26
     */
27
    private SimpleCachingIteratorAggregate $iterator;
28
29
    /**
30
     * @param Iterator<TKey, T> $iterator
31
     */
32 6
    public function __construct(Iterator $iterator)
33
    {
34 6
        $this->iterator = new SimpleCachingIteratorAggregate((new PackIterableAggregate($iterator))->getIterator());
35
    }
36
37
    /**
38
     * @return Generator<TKey, T>
39
     */
40 6
    public function getIterator(): Generator
41
    {
42 6
        foreach ($this->iterator as [$key, $current]) {
43 6
            yield $key => $current;
44
        }
45
    }
46
47 1
    public function hasNext(): bool
48
    {
49 1
        return $this->iterator->hasNext();
50
    }
51
}
52