getGenerator()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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