GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 023ec3...45d736 )
by Baptiste
06:30
created

Stream::groupBy()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 20

Duplication

Lines 34
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 34
loc 34
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 6
nop 1
crap 6
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\{
7
    LogicException,
8
    GroupEmptySequenceException,
9
    InvalidArgumentException
10
};
11
12
/**
13
 * {@inheritdoc}
14
 */
15
class Stream implements StreamInterface
16
{
17
    use Type;
18
19
    private $type;
20
    private $spec;
21
    private $values;
22
23 100
    public function __construct(string $type)
24
    {
25 100
        $this->type = new Str($type);
26 100
        $this->spec = $this->getSpecFor($type);
27 100
        $this->values = new Sequence;
28 100
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 52
    public function type(): Str
34
    {
35 52
        return $this->type;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 6
    public function size(): int
42
    {
43 6
        return $this->values->size();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 4
    public function count(): int
50
    {
51 4
        return $this->values->size();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 38
    public function toPrimitive()
58
    {
59 38
        return $this->values->toPrimitive();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 14
    public function current()
66
    {
67 14
        return $this->values->current();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 10
    public function key()
74
    {
75 10
        return $this->values->key();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 14
    public function next()
82
    {
83 14
        $this->values->next();
84 14
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 14
    public function rewind()
90
    {
91 14
        $this->values->rewind();
92 14
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 14
    public function valid()
98
    {
99 14
        return $this->values->valid();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function offsetExists($offset): bool
106
    {
107 2
        return $this->values->offsetExists($offset);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 10
    public function offsetGet($offset)
114
    {
115 10
        return $this->get($offset);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    public function offsetSet($offset, $value)
122
    {
123 2
        throw new LogicException('You can\'t modify a stream');
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2
    public function offsetUnset($offset)
130
    {
131 2
        throw new LogicException('You can\'t modify a stream');
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 14
    public function get(int $index)
138
    {
139 14
        return $this->values->get($index);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2
    public function diff(StreamInterface $stream): StreamInterface
146
    {
147 2
        $this->validate($stream);
148
149 2
        $newStream = clone $this;
150 2
        $newStream->values = $this->values->diff(
151 2
            new Sequence(...$stream)
152
        );
153
154 2
        return $newStream;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 2
    public function distinct(): StreamInterface
161
    {
162 2
        $stream = clone $this;
163 2
        $stream->values = $this->values->distinct();
164
165 2
        return $stream;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 2
    public function drop(int $size): StreamInterface
172
    {
173 2
        $stream = clone $this;
174 2
        $stream->values = $this->values->drop($size);
175
176 2
        return $stream;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 2
    public function dropEnd(int $size): StreamInterface
183
    {
184 2
        $stream = clone $this;
185 2
        $stream->values = $this->values->dropEnd($size);
186
187 2
        return $stream;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 4
    public function equals(StreamInterface $stream): bool
194
    {
195 4
        $this->validate($stream);
196
197 2
        return $this->values->equals(
198 2
            new Sequence(...$stream)
199
        );
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 2
    public function filter(callable $predicate): StreamInterface
206
    {
207 2
        $stream = clone $this;
208 2
        $stream->values = $this->values->filter($predicate);
209
210 2
        return $stream;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function foreach(callable $function): StreamInterface
217
    {
218 2
        $this->values->foreach($function);
219
220 2
        return $this;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 4 View Code Duplication
    public function groupBy(callable $discriminator): MapInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228 4
        if ($this->size() === 0) {
229 2
            throw new GroupEmptySequenceException;
230
        }
231
232 2
        $map = null;
233
234 2
        foreach ($this->values as $value) {
235 2
            $key = $discriminator($value);
236
237 2
            if ($map === null) {
238 2
                $type = gettype($key);
239 2
                $map = new Map(
240 2
                    $type === 'object' ? get_class($key) : $type,
241 2
                    StreamInterface::class
242
                );
243
            }
244
245 2
            if ($map->contains($key)) {
246 2
                $map = $map->put(
247
                    $key,
248 2
                    $map->get($key)->add($value)
249
                );
250
            } else {
251 2
                $map = $map->put(
252
                    $key,
253 2
                    (new self((string) $this->type))->add($value)
254
                );
255
            }
256
        }
257
258 2
        return $map;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 4
    public function first()
265
    {
266 4
        return $this->values->first();
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 4
    public function last()
273
    {
274 4
        return $this->values->last();
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280 2
    public function contains($element): bool
281
    {
282 2
        return $this->values->contains($element);
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 2
    public function indexOf($element): int
289
    {
290 2
        return $this->values->indexOf($element);
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     */
296 2
    public function indices(): StreamInterface
297
    {
298 2
        $stream = new self('int');
299 2
        $stream->values = $this->values->indices();
300
301 2
        return $stream;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307 6
    public function map(callable $function): StreamInterface
308
    {
309
        return $this
310 6
            ->values
311 6
            ->reduce(
312 6
                new self((string) $this->type),
313 6
                function(self $carry, $element) use($function): StreamInterface {
314 6
                    return $carry->add($function($element));
315 6
                }
316
            );
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 4 View Code Duplication
    public function pad(int $size, $element): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
    {
324 4
        $this->spec->validate($element);
325
326 2
        $stream = clone $this;
327 2
        $stream->values = $this->values->pad($size, $element);
328
329 2
        return $stream;
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335 2
    public function partition(callable $predicate): MapInterface
336
    {
337 2
        $truthy = new self((string) $this->type);
338 2
        $falsy = new self((string) $this->type);
339
340 2
        foreach ($this->values as $value) {
341 2
            if ($predicate($value) === true) {
342 2
                $truthy = $truthy->add($value);
343
            } else {
344 2
                $falsy = $falsy->add($value);
345
            }
346
        }
347
348 2
        return (new Map('bool', StreamInterface::class))
349 2
            ->put(true, $truthy)
350 2
            ->put(false, $falsy);
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356 2
    public function slice(int $from, int $until): StreamInterface
357
    {
358 2
        $stream = clone $this;
359 2
        $stream->values = $this->values->slice($from, $until);
360
361 2
        return $stream;
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 2
    public function splitAt(int $position): StreamInterface
368
    {
369 2
        $stream = new self(StreamInterface::class);
370 2
        $splitted = $this->values->splitAt($position);
371 2
        $first = new self((string) $this->type);
372 2
        $second = new self((string) $this->type);
373 2
        $first->values = $splitted->first();
374 2
        $second->values = $splitted->last();
375
376 2
        return $stream->add($first)->add($second);
2 ignored issues
show
Documentation introduced by
$first is of type object<Innmind\Immutable\Stream>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$second is of type object<Innmind\Immutable\Stream>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
377
    }
378
379
    /**
380
     * {@inheritdoc}
381
     */
382 2
    public function take(int $size): StreamInterface
383
    {
384 2
        $stream = clone $this;
385 2
        $stream->values = $this->values->take($size);
386
387 2
        return $stream;
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393 2
    public function takeEnd(int $size): StreamInterface
394
    {
395 2
        $stream = clone $this;
396 2
        $stream->values = $this->values->takeEnd($size);
397
398 2
        return $stream;
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404 4 View Code Duplication
    public function append(StreamInterface $stream): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
    {
406 4
        $this->validate($stream);
407
408 2
        $self = clone $this;
409 2
        $self->values = $this->values->append(
410 2
            new Sequence(...$stream)
411
        );
412
413 2
        return $self;
414
    }
415
416
    /**
417
     * {@inheritdoc}
418
     */
419 4 View Code Duplication
    public function intersect(StreamInterface $stream): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420
    {
421 4
        $this->validate($stream);
422
423 2
        $self = clone $this;
424 2
        $self->values = $this->values->intersect(
425 2
            new Sequence(...$stream)
426
        );
427
428 2
        return $self;
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     */
434 4
    public function join(string $separator): Str
435
    {
436 4
        return new Str((string) $this->values->join($separator));
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     */
442 80 View Code Duplication
    public function add($element): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
443
    {
444 80
        $this->spec->validate($element);
445
446 78
        $stream = clone $this;
447 78
        $stream->values = $this->values->add($element);
448
449 78
        return $stream;
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455 2
    public function sort(callable $function): StreamInterface
456
    {
457 2
        $stream = clone $this;
458 2
        $stream->values = $this->values->sort($function);
459
460 2
        return $stream;
461
    }
462
463
    /**
464
     * {@inheritdoc}
465
     */
466 2
    public function reduce($carry, callable $reducer)
467
    {
468 2
        return $this->values->reduce($carry, $reducer);
469
    }
470
471
    /**
472
     * Make sure the stream is compatible with the current one
473
     *
474
     * @param StreamInterface $stream
475
     *
476
     * @throws InvalidArgumentException
477
     *
478
     * @return void
479
     */
480 14
    private function validate(StreamInterface $stream)
481
    {
482 14
        if (!$stream->type()->equals($this->type)) {
0 ignored issues
show
Documentation introduced by
$this->type is of type object<Innmind\Immutable\Str>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
483 6
            throw new InvalidArgumentException(
484 6
                'The 2 streams does not reference the same type'
485
            );
486
        }
487 8
    }
488
}
489