Passed
Push — master ( 2cd07b...f0c27e )
by Arnold
02:47
created

_iterable_chunk_generate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Improved;
4
5
/**
6
 * Divide iterable into chunks of specified size.
7
 *
8
 * @param iterable $iterable
9
 * @param int      $size
10
 * @return \Generator
11
 */
12
function iterable_chunk(iterable $iterable, int $size): \Generator
13
{
14 12
    $iterator = iterable_to_iterator($iterable);
15 12
    $iterator->rewind();
16
17 12
    while ($iterator->valid()) {
18 11
        yield _iterable_chunk_generate($iterator, $size);
19
    }
20 12
}
21
22
/**
23
 * @noinspection PhpFunctionNamingConventionInspection
24
 * @internal
25
 *
26
 * @param \Iterator $iterator
27
 * @param int       $size
28
 * @return \Generator
29
 */
30
function _iterable_chunk_generate(\Iterator $iterator, int $size): \Generator
31
{
32 11
    for ($i = 0; $i < $size && $iterator->valid(); $i++) {
33 11
        yield $iterator->key() => $iterator->current();
34
35 11
        $iterator->next();
36
    }
37
}
38