JustifyMultipleIterator::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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