Passed
Push — master ( eb3ca2...e5242e )
by Arnold
01:55
created

iterable_slice()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 8
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny;
6
7
/**
8
 * Get a limited subset of the elements using an offset and (optionally) a limit.
9
 *
10
 * @param iterable $iterable
11
 * @param int      $offset
12
 * @param int|null $limit
13
 * @return \Generator
14
 */
15
function iterable_slice(iterable $iterable, int $offset, ?int $limit = null): \Generator
16
{
17 16
    $counter = 0;
18 16
    $end = isset($limit) ? ($offset + $limit) : PHP_INT_MAX;
19
20 16
    foreach ($iterable as $key => $value) {
21 15
        if ($counter === $end) {
22 10
            return;
23
        }
24
25 15
        if ($counter >= $offset) {
26 15
            yield $key => $value;
27
        }
28
29 15
        $counter++;
30
    }
31
}
32