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

SimpleCachingIteratorAggregate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 10
dl 0
loc 35
ccs 9
cts 9
cp 1
rs 10
c 5
b 2
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 8 2
A __construct() 0 5 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 CachingIterator;
13
use Generator;
14
use Iterator;
15
use IteratorAggregate;
16
17
// phpcs:disable Generic.Files.LineLength.TooLong
18
19
/**
20
 * @template TKey of array-key
21
 * @template T
22
 *
23
 * @implements IteratorAggregate<array-key, T>
24
 */
25
final class SimpleCachingIteratorAggregate implements IteratorAggregate
26
{
27
    /**
28
     * @var CachingIterator<array-key, T>
29
     */
30
    private CachingIterator $iterator;
31
32
    /**
33
     * @param Iterator<array-key, T> $iterator
34
     */
35 9
    public function __construct(Iterator $iterator)
36
    {
37 9
        $this->iterator = new CachingIterator(
38
            $iterator,
39
            CachingIterator::FULL_CACHE
40
        );
41
    }
42
43
    /**
44
     * @return Generator<array-key, T>
45
     */
46 9
    public function getIterator(): Generator
47
    {
48 9
        yield from $this->iterator->getCache();
49
50 9
        while ($this->iterator->hasNext()) {
51 9
            $this->iterator->next();
52
53 9
            yield $this->iterator->key() => $this->iterator->current();
54
        }
55
    }
56
57 2
    public function hasNext(): bool
58
    {
59 2
        return $this->iterator->hasNext();
60
    }
61
}
62