Passed
Push — master ( 23f099...2790c0 )
by Smoren
02:31
created

JustifyMultipleIterator::current()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Smoren\PartialIntersection\Util;
4
5
/**
6
 * @implements \Iterator<array<mixed>>
7
 */
8
class JustifyMultipleIterator implements \Iterator
9
{
10
    /**
11
     * @var array<\Iterator<mixed>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<\Iterator<mixed>> at position 2 could not be parsed: Expected '>' at position 2, but found '\Iterator'.
Loading history...
12
     */
13
    protected array $iterators = [];
14
    /**
15
     * @var int
16
     */
17
    protected int $index = 0;
18
19
    /**
20
     * @param iterable<mixed> ...$iterables
21
     */
22 374
    public function __construct(iterable ...$iterables)
23
    {
24 374
        foreach ($iterables as $iterable) {
25 358
            $this->iterators[] = IteratorFactory::makeIterator($iterable);
26
        }
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @return array<mixed>
33
     */
34 318
    public function current(): array
35
    {
36 318
        return array_map(
37 318
            fn (\Iterator $iterator) => $iterator->valid() ? $iterator->current() : NoValueMonad::getInstance(),
38 318
            $this->iterators
39 318
        );
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 318
    public function next(): void
46
    {
47 318
        foreach ($this->iterators as $iterator) {
48 318
            if ($iterator->valid()) {
49 318
                $iterator->next();
50
            }
51
        }
52 318
        $this->index++;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     *
58
     * @return int
59
     */
60 318
    public function key(): int
61
    {
62 318
        return $this->index;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 374
    public function valid(): bool
69
    {
70 374
        foreach ($this->iterators as $iterator) {
71 358
            if ($iterator->valid()) {
72 318
                return true;
73
            }
74
        }
75
76 374
        return false;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 374
    public function rewind(): void
83
    {
84 374
        foreach ($this->iterators as $iterator) {
85 358
            $iterator->rewind();
86
        }
87 374
        $this->index = 0;
88
    }
89
}
90