iterable_slice()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 8
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved;
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 16
function iterable_slice(iterable $iterable, int $offset, ?int $limit = null): \Generator
0 ignored issues
show
introduced by
Function Improved\iterable_slice() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
introduced by
Function Improved\iterable_slice() return type has no value type specified in iterable type Generator.
Loading history...
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