FlatMapStream::loadNextStream()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Bdf\Collection\Stream;
4
5
/**
6
 * Class FlatMapStream
7
 *
8
 * @template T
9
 *
10
 * @template ST
11
 * @template SK
12
 *
13
 * @implements StreamInterface<T, mixed>
14
 */
15
final class FlatMapStream implements StreamInterface
16
{
17
    use StreamTrait;
18
19
    /**
20
     * @var StreamInterface<ST, SK>
21
     */
22
    private $stream;
23
24
    /**
25
     * @var callable(ST, SK):(T|T[]|StreamInterface<T, mixed>)
26
     */
27
    private $transformer;
28
29
    /**
30
     * @var bool
31
     */
32
    private $preserveKeys;
33
34
    /**
35
     * @var StreamInterface<T, mixed>|null
36
     */
37
    private $currentStream;
38
39
    /**
40
     * @var T|null
0 ignored issues
show
Bug introduced by
The type Bdf\Collection\Stream\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    private $currentValue;
43
44
    /**
45
     * @var int
46
     */
47
    private $index = 0;
48
49
50
    /**
51
     * FlatMapStream constructor.
52
     *
53
     * @param StreamInterface<ST, SK> $stream
54
     * @param callable(ST, SK):(StreamInterface<T, mixed>|T[]|T) $transformer
55
     * @param bool $preserveKeys
56
     */
57 34
    public function __construct(StreamInterface $stream, callable $transformer, bool $preserveKeys = false)
58
    {
59 34
        $this->stream = $stream;
60 34
        $this->transformer = $transformer;
61 34
        $this->preserveKeys = $preserveKeys;
62 34
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    #[\ReturnTypeWillChange]
68 33
    public function current()
69
    {
70 33
        return $this->currentValue;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @psalm-suppress PossiblyNullReference
77
     */
78 32
    public function next(): void
79
    {
80 32
        ++$this->index;
81 32
        $this->currentStream->next();
82
83 32
        if ($this->currentStream->valid()) {
84 31
            $this->currentValue = $this->currentStream->current();
85 31
            return;
86
        }
87
88 32
        $this->stream->next();
89 32
        $this->loadNextStream();
90 32
    }
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     * @psalm-suppress PossiblyNullReference
96
     */
97
    #[\ReturnTypeWillChange]
98 30
    public function key()
99
    {
100 30
        return $this->preserveKeys ? $this->currentStream->key() : $this->index;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 34
    public function valid(): bool
107
    {
108 34
        return $this->currentStream !== null;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 34
    public function rewind(): void
115
    {
116 34
        $this->stream->rewind();
117
118 34
        $this->index = 0;
119 34
        $this->loadNextStream();
120 34
    }
121
122 34
    private function loadNextStream(): void
123
    {
124 34
        $this->currentStream = null;
125 34
        $this->currentValue = null;
126
127 34
        while ($this->stream->valid()) {
128 33
            $currentStream = Streams::wrap(($this->transformer)($this->stream->current(), $this->stream->key()));
129 33
            $currentStream->rewind();
130
131 33
            if ($currentStream->valid()) {
132 33
                $this->currentStream = $currentStream;
133 33
                $this->currentValue = $currentStream->current();
134 33
                return;
135
            }
136
137 1
            $this->stream->next();
138
        }
139 34
    }
140
}
141