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

SimpleCachingIteratorAggregate::hasNext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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