Passed
Branch develop (d49db1)
by Baptiste
01:49
created

Stream::equals()   A

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 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Lazy;
5
6
use Innmind\Compose\Lazy;
7
use Innmind\Immutable\{
8
    StreamInterface,
9
    Stream as Base,
10
    MapInterface,
11
    Str,
12
    Type,
13
    Exception\LogicException
14
};
15
16
final class Stream implements StreamInterface
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
17
{
18
    use Type;
19
20
    private $type;
21
    private $spec;
22
    private $values;
23
24 43
    public function __construct(string $type, ...$values)
25
    {
26 43
        $this->type = Str::of($type);
27 43
        $this->spec = $this->getSpecificationFor($type);
28 43
        $this->values = Base::of('mixed', ...$values);
29 43
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 25
    public function type(): Str
35
    {
36 25
        return $this->type;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function size(): int
43
    {
44 1
        return $this->values->size();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    public function count(): int
51
    {
52 4
        return $this->values->size();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 29
    public function toPrimitive()
59
    {
60 29
        return $this->values->reduce(
61 29
            [],
62 29
            function(array $values, $value): array {
63 29
                $values[] = $this->load($value);
64
65 29
                return $values;
66 29
            }
67
        );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function current()
74
    {
75 2
        return $this->load($this->values->current());
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1
    public function key()
82
    {
83 1
        return $this->values->key();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function next()
90
    {
91 1
        $this->values->next();
92 1
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function rewind()
98
    {
99 1
        $this->values->rewind();
100 1
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function valid()
106
    {
107 1
        return $this->values->valid();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function offsetExists($offset): bool
114
    {
115 1
        return $this->values->offsetExists($offset);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function offsetGet($offset)
122
    {
123 1
        return $this->get($offset);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1
    public function offsetSet($offset, $value)
130
    {
131 1
        throw new LogicException('You can\'t modify a stream');
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1
    public function offsetUnset($offset)
138
    {
139 1
        throw new LogicException('You can\'t modify a stream');
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 3
    public function get(int $index)
146
    {
147 3
        return $this->load($this->values->get($index));
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 1
    public function diff(StreamInterface $stream): StreamInterface
154
    {
155 1
        return $this->stream()->diff($stream);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function distinct(): StreamInterface
162
    {
163 1
        return $this->stream()->distinct();
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 1
    public function drop(int $size): StreamInterface
170
    {
171 1
        $self = clone $this;
172 1
        $self->values = $self->values->drop($size);
173
174 1
        return $self;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1
    public function dropEnd(int $size): StreamInterface
181
    {
182 1
        $self = clone $this;
183 1
        $self->values = $self->values->dropEnd($size);
184
185 1
        return $self;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 2
    public function equals(StreamInterface $stream): bool
192
    {
193 2
        return $this->stream()->equals($stream);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public function filter(callable $predicate): StreamInterface
200
    {
201 1
        return $this->stream()->filter($predicate);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function foreach(callable $function): StreamInterface
208
    {
209 1
        $this->values->foreach(function($value) use ($function): void {
210 1
            $function($this->load($value));
211 1
        });
212
213 1
        return $this;
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 1
    public function groupBy(callable $discriminator): MapInterface
220
    {
221
        return $this
222 1
            ->values
223 1
            ->groupBy(function($value) use ($discriminator) {
224 1
                return $discriminator($this->load($value));
225 1
            })
226 1
            ->map(function($key, Base $stream): Base {
227 1
                return Base::of(
228 1
                    (string) $this->type,
229 1
                    ...$stream->map(function($value) {
230 1
                        return $this->load($value);
231 1
                    })
232
                );
233 1
            });
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239 1
    public function first()
240
    {
241 1
        return $this->load($this->values->first());
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247 1
    public function last()
248
    {
249 1
        return $this->load($this->values->last());
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255 1
    public function contains($element): bool
256
    {
257 1
        return $this->stream()->contains($element);
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263 1
    public function indexOf($element): int
264
    {
265 1
        return $this->stream()->indexOf($element);
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271 1
    public function indices(): StreamInterface
272
    {
273 1
        return $this->values->indices();
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279 1
    public function map(callable $function): StreamInterface
280
    {
281 1
        return $this->stream()->map($function);
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 2
    public function pad(int $size, $element): StreamInterface
288
    {
289 2
        $this->spec->validate($element);
290
291 1
        $self = clone $this;
292 1
        $self->values = $self->values->pad($size, $element);
293
294 1
        return $self;
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 1
    public function partition(callable $predicate): MapInterface
301
    {
302
        return $this
303 1
            ->values
304 1
            ->partition(function($value) use ($predicate): bool {
305 1
                return $predicate($this->load($value));
306 1
            })
307 1
            ->map(function($key, Base $stream): Base {
308 1
                return Base::of(
309 1
                    (string) $this->type,
310 1
                    ...$stream->map(function($value) {
311 1
                        return $this->load($value);
312 1
                    })
313
                );
314 1
            });
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320 1
    public function slice(int $from, int $until): StreamInterface
321
    {
322 1
        $self = clone $this;
323 1
        $self->values = $self->values->slice($from, $until);
324
325 1
        return $self;
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331 1
    public function splitAt(int $position): StreamInterface
332
    {
333 1
        $splitted = $this->values->splitAt($position);
334 1
        $first = clone $this;
335 1
        $last = clone $this;
336 1
        $first->values = $splitted->first();
337 1
        $last->values = $splitted->last();
338
339 1
        return Base::of(StreamInterface::class, $first, $last);
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345 1
    public function take(int $size): StreamInterface
346
    {
347 1
        $self = clone $this;
348 1
        $self->values = $self->values->take($size);
349
350 1
        return $self;
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356 1
    public function takeEnd(int $size): StreamInterface
357
    {
358 1
        $self = clone $this;
359 1
        $self->values = $self->values->takeEnd($size);
360
361 1
        return $self;
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 1
    public function append(StreamInterface $stream): StreamInterface
368
    {
369 1
        return $this->stream()->append($stream);
370
    }
371
372
    /**
373
     * {@inheritdoc}
374
     */
375 1
    public function intersect(StreamInterface $stream): StreamInterface
376
    {
377 1
        return $this->stream()->intersect($stream);
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     */
383 1
    public function join(string $separator): Str
384
    {
385 1
        return $this->stream()->join($separator);
386
    }
387
388
    /**
389
     * {@inheritdoc}
390
     */
391 1
    public function add($element): StreamInterface
392
    {
393 1
        $this->spec->validate($element);
394
395 1
        $self = clone $this;
396 1
        $self->values = $self->values->add($element);
397
398 1
        return $self;
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404 1
    public function sort(callable $function): StreamInterface
405
    {
406 1
        return $this->stream()->sort($function);
407
    }
408
409
    /**
410
     * {@inheritdoc}
411
     */
412 1
    public function reduce($carry, callable $reducer)
413
    {
414 1
        return $this->stream()->reduce($carry, $reducer);
415
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420 1
    public function clear(): StreamInterface
421
    {
422 1
        return new Base((string) $this->type);
423
    }
424
425
    /**
426
     * Return the same stream but in reverse order
427
     *
428
     * @return self<T>
429
     */
430 1
    public function reverse(): StreamInterface
431
    {
432 1
        $self = clone $this;
433 1
        $self->values = $self->values->reverse();
434
435 1
        return $self;
436
    }
437
438 35
    private function load($value)
439
    {
440 35
        if ($value instanceof Lazy) {
441 32
            return $value->load();
442
        }
443
444 5
        return $value;
445
    }
446
447 12
    private function stream(): Base
448
    {
449 12
        return Base::of((string) $this->type, ...$this->toPrimitive());
450
    }
451
}
452