IteratorFactory::makeIterator()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\PartialIntersection\Util;
6
7
/**
8
 * Based on IterTools PHP's IteratorFactory.
9
 * @see https://github.com/markrogoyski/itertools-php
10
 * @see https://github.com/markrogoyski/itertools-php/blob/main/src/Util/IteratorFactory.php
11
 */
12
class IteratorFactory
13
{
14
    /**
15
     * @param iterable<mixed> $iterable
16
     *
17
     * @return \Iterator<mixed>|\IteratorIterator<mixed>|\ArrayIterator<mixed>
18
     */
19 799
    public static function makeIterator(iterable $iterable): \Iterator
20
    {
21
        switch (true) {
22 799
            case $iterable instanceof \Iterator:
23 358
                return $iterable;
24 441
            case $iterable instanceof \Traversable:
25 179
                return new \IteratorIterator($iterable);
26
            default:
27 262
                return new \ArrayIterator($iterable);
0 ignored issues
show
Bug introduced by
$iterable of type iterable is incompatible with the type array expected by parameter $array of ArrayIterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
                return new \ArrayIterator(/** @scrutinizer ignore-type */ $iterable);
Loading history...
28
        }
29
    }
30
}
31