Passed
Push — main ( e2cf3e...ef3f7a )
by Pol
17:17
created

CachingIteratorAggregate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 26
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A __construct() 0 3 1
A hasNext() 0 3 1
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
// phpcs:disable Generic.Files.LineLength.TooLong
17
18
/**
19
 * @template TKey
20
 * @template T
21
 *
22
 * @implements IteratorAggregate<TKey, T>
23
 */
24
final class CachingIteratorAggregate implements IteratorAggregate
25
{
26
    /**
27
     * @var SimpleCachingIteratorAggregate<int, array{0: TKey, 1:T}>
28
     */
29
    private SimpleCachingIteratorAggregate $cachingIterator;
30
31
    /**
32
     * @param Iterator<TKey, T> $iterator
33
     */
34 6
    public function __construct(Iterator $iterator)
35
    {
36 6
        $this->cachingIterator = new SimpleCachingIteratorAggregate((new PackIterableAggregate($iterator))->getIterator());
37
    }
38
39
    /**
40
     * @return Generator<TKey, T>
41
     */
42 6
    public function getIterator(): Generator
43
    {
44 6
        yield from (new UnpackIterableAggregate($this->cachingIterator))->getIterator();
45
    }
46
47 1
    public function hasNext(): bool
48
    {
49 1
        return $this->cachingIterator->hasNext();
50
    }
51
}
52