SingletonStream   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 226
ccs 60
cts 60
cp 1
rs 9.84
wmc 32

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reduce() 0 7 3
A map() 0 3 1
A skip() 0 7 2
A matchOne() 0 3 1
A first() 0 3 1
A filter() 0 5 2
A limit() 0 7 3
A flatMap() 0 7 2
A concat() 0 3 1
A sort() 0 3 3
A forEach() 0 4 1
A collect() 0 5 1
A toArray() 0 3 2
A mapKey() 0 3 1
A matchAll() 0 3 1
A distinct() 0 3 1
A key() 0 4 1
A next() 0 3 1
A rewind() 0 3 1
A current() 0 4 1
A valid() 0 3 1
1
<?php
2
3
namespace Bdf\Collection\Stream;
4
5
use Bdf\Collection\Stream\Accumulator\AccumulatorInterface;
6
use Bdf\Collection\Stream\Collector\CollectorInterface;
7
use Bdf\Collection\Util\Optional;
8
use Bdf\Collection\Util\OptionalInterface;
9
10
/**
11
 * Wrap single value into a stream
12
 *
13
 * Unlike other streams, for optimisation reasons, the transformation methods will be called directly, before the terminal call
14
 *
15
 * @template T
16
 * @template K
17
 *
18
 * @implements StreamInterface<T, K>
19
 */
20
final class SingletonStream implements StreamInterface
21
{
22
    /**
23
     * @var T
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...
24
     */
25
    private $value;
26
27
    /**
28
     * @var K
0 ignored issues
show
Bug introduced by
The type Bdf\Collection\Stream\K 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...
29
     */
30
    private $key;
31
32
    /**
33
     * @var bool
34
     */
35
    private $closed = false;
36
37
38
    /**
39
     * SingletonStream constructor.
40
     *
41
     * @param T $value
42
     * @param K $key
43
     */
44 22
    public function __construct($value, $key = 0)
45
    {
46 22
        $this->value = $value;
47 22
        $this->key = $key;
0 ignored issues
show
Documentation Bug introduced by
It seems like $key can also be of type integer. However, the property $key is declared as type Bdf\Collection\Stream\K. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48 22
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function map(callable $transformer): StreamInterface
54
    {
55 1
        return new SingletonStream($transformer($this->value, $this->key), $this->key);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function mapKey(callable $function): StreamInterface
62
    {
63 1
        return new SingletonStream($this->value, $function($this->value, $this->key));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function filter(callable $predicate): StreamInterface
70
    {
71 1
        return $predicate($this->value, $this->key)
72 1
            ? $this
73 1
            : EmptyStream::instance()
74
        ;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function distinct(callable $hashFunction = null): StreamInterface
81
    {
82 1
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    public function sort(callable $comparator = null, bool $preserveKeys = false): StreamInterface
89
    {
90 2
        return $preserveKeys || $this->key === 0 ? $this : new self($this->value);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function concat(StreamInterface $stream, bool $preserveKeys = true): StreamInterface
97
    {
98 1
        return new ConcatStream([$this, $stream], $preserveKeys);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     *
104
     * @psalm-suppress InvalidArgument
105
     */
106 1
    public function flatMap(callable $transformer, bool $preserveKeys = false): StreamInterface
107
    {
108 1
        if ($preserveKeys) {
109 1
            return Streams::wrap($transformer($this->value, $this->key));
110
        }
111
112 1
        return new FlatMapStream($this, $transformer, false);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function skip(int $count): StreamInterface
119
    {
120 1
        if ($count > 0) {
121 1
            return EmptyStream::instance();
122
        }
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function limit(int $count, int $offset = 0): StreamInterface
131
    {
132 1
        if ($offset > 0 || $count < 1) {
133 1
            return EmptyStream::instance();
134
        }
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function forEach(callable $consumer): void
143
    {
144 1
        $consumer($this->value, $this->key);
145 1
        $this->closed = true;
146 1
    }
147
148
    /**
149
     * {@inheritdoc}
150
     *
151
     * @template PK as bool
152
     * @psalm-param PK $preserveKeys
153
     * @psalm-return (PK is true ? array<K, T> : array{0:T})
154
     */
155 3
    public function toArray(bool $preserveKeys = true): array
156
    {
157 3
        return $preserveKeys ? [$this->key => $this->value] : [$this->value];
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 2
    public function first(): OptionalInterface
164
    {
165 2
        return Optional::nullable($this->value);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    public function reduce(callable $accumulator, $initial = null)
172
    {
173 1
        if ($initial === null && $accumulator instanceof AccumulatorInterface) {
174 1
            $initial = $accumulator->initial();
175
        }
176
177 1
        return $accumulator($initial, $this->value);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function collect(CollectorInterface $collector)
184
    {
185 1
        $collector->aggregate($this->value, $this->key);
186
187 1
        return $collector->finalize();
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 1
    public function matchAll(callable $predicate): bool
194
    {
195 1
        return $predicate($this->value, $this->key);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201 1
    public function matchOne(callable $predicate): bool
202
    {
203 1
        return $predicate($this->value, $this->key);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    #[\ReturnTypeWillChange]
210 6
    public function current()
211
    {
212 6
        return $this->value;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 6
    public function next(): void
219
    {
220 6
        $this->closed = true;
221 6
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    #[\ReturnTypeWillChange]
227 4
    public function key()
228
    {
229 4
        return $this->key;
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 7
    public function valid(): bool
236
    {
237 7
        return !$this->closed;
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243 6
    public function rewind(): void
244
    {
245 6
        $this->closed = false;
246 6
    }
247
}
248