ChunkIterableAggregate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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
use function count;
16
17
/**
18
 * @template TKey
19
 * @template T
20
 *
21
 * @implements IteratorAggregate<int, list<T>>
22
 */
23
final class ChunkIterableAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @param iterable<TKey, T> $iterable
27
     */
28 1
    public function __construct(private iterable $iterable, private int $chunkSize)
29
    {
30 1
    }
31
32
    /**
33
     * @return Generator<int, list<T>>
34
     */
35 1
    public function getIterator(): Generator
36
    {
37 1
        $values = [];
38
39 1
        foreach ($this->iterable as $value) {
40 1
            if (count($values) !== $this->chunkSize) {
41 1
                $values[] = $value;
42
43 1
                continue;
44
            }
45
46 1
            yield $values;
47
48 1
            $values = [$value];
49
        }
50
51 1
        yield $values;
52
    }
53
}
54