InterruptableIterableIteratorAggregate   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 34
ccs 11
cts 11
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getIterator() 0 6 2
A getGenerator() 0 8 3
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 IteratorAggregate;
14
15
/**
16
 * @template TKey
17
 * @template T
18
 *
19
 * @implements IteratorAggregate<Generator<TKey, T>, array{0: TKey, 1: T}>
20
 */
21
final class InterruptableIterableIteratorAggregate implements IteratorAggregate
22
{
23
    public const BREAK = 'break';
24
25
    /**
26
     * @param iterable<TKey, T> $iterable
27
     */
28 2
    public function __construct(private iterable $iterable)
29
    {
30 2
    }
31
32
    /**
33
     * @return Generator<Generator<TKey, T>, array{0: TKey, 1: T}>
34
     */
35 2
    public function getIterator(): Generator
36
    {
37 2
        $generator = $this->getGenerator();
38
39 2
        foreach ($generator as $key => $value) {
40 2
            yield $generator => [$key, $value];
41
        }
42
    }
43
44
    /**
45
     * @return Generator<TKey, T>
46
     */
47 2
    private function getGenerator(): Generator
48
    {
49 2
        foreach ($this->iterable as $key => $value) {
50
            /** @var string $return */
51 2
            $return = yield $key => $value;
52
53 2
            if (self::BREAK === $return) {
54 2
                break;
55
            }
56
        }
57
    }
58
}
59