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 — develop ( 5fe34b...11f42d )
by Baptiste
03:53
created

Stream::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 375
    public function __construct(string $type)
24
    {
25 375
        $this->type = new Str($type);
26 375
        $this->spec = $this->getSpecificationFor($type);
27 375
        $this->values = new Sequence;
28 375
    }
29
30 3
    public static function of(string $type, ...$values): self
31
    {
32 3
        return array_reduce(
33 3
            $values,
34 3
            static function(self $self, $value): self {
35 3
                return $self->add($value);
36 3
            },
37 3
            new self($type)
38
        );
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 141
    public function type(): Str
45
    {
46 141
        return $this->type;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 48
    public function size(): int
53
    {
54 48
        return $this->values->size();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 18
    public function count(): int
61
    {
62 18
        return $this->values->size();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 135
    public function toPrimitive()
69
    {
70 135
        return $this->values->toPrimitive();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 102
    public function current()
77
    {
78 102
        return $this->values->current();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 72
    public function key()
85
    {
86 72
        return $this->values->key();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 96
    public function next()
93
    {
94 96
        $this->values->next();
95 96
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 102
    public function rewind()
101
    {
102 102
        $this->values->rewind();
103 102
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 102
    public function valid()
109
    {
110 102
        return $this->values->valid();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 3
    public function offsetExists($offset): bool
117
    {
118 3
        return $this->values->offsetExists($offset);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 27
    public function offsetGet($offset)
125
    {
126 27
        return $this->get($offset);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 3
    public function offsetSet($offset, $value)
133
    {
134 3
        throw new LogicException('You can\'t modify a stream');
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 3
    public function offsetUnset($offset)
141
    {
142 3
        throw new LogicException('You can\'t modify a stream');
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 78
    public function get(int $index)
149
    {
150 78
        return $this->values->get($index);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 6
    public function diff(StreamInterface $stream): StreamInterface
157
    {
158 6
        $this->validate($stream);
159
160 6
        $newStream = clone $this;
161 6
        $newStream->values = $this->values->diff(
162 6
            new Sequence(...$stream)
0 ignored issues
show
Documentation introduced by
new \Innmind\Immutable\Sequence(...$stream) is of type object<Innmind\Immutable\Sequence>, 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...
163
        );
164
165 6
        return $newStream;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 3
    public function distinct(): StreamInterface
172
    {
173 3
        $stream = clone $this;
174 3
        $stream->values = $this->values->distinct();
175
176 3
        return $stream;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 24
    public function drop(int $size): StreamInterface
183
    {
184 24
        $stream = clone $this;
185 24
        $stream->values = $this->values->drop($size);
186
187 24
        return $stream;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 3
    public function dropEnd(int $size): StreamInterface
194
    {
195 3
        $stream = clone $this;
196 3
        $stream->values = $this->values->dropEnd($size);
197
198 3
        return $stream;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 33
    public function equals(StreamInterface $stream): bool
205
    {
206 33
        $this->validate($stream);
207
208 30
        return $this->values->equals(
209 30
            new Sequence(...$stream)
0 ignored issues
show
Documentation introduced by
new \Innmind\Immutable\Sequence(...$stream) is of type object<Innmind\Immutable\Sequence>, 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...
210
        );
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 6
    public function filter(callable $predicate): StreamInterface
217
    {
218 6
        $stream = clone $this;
219 6
        $stream->values = $this->values->filter($predicate);
220
221 6
        return $stream;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 6
    public function foreach(callable $function): StreamInterface
228
    {
229 6
        $this->values->foreach($function);
230
231 6
        return $this;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237 9 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...
238
    {
239 9
        if ($this->size() === 0) {
240 3
            throw new GroupEmptySequenceException;
241
        }
242
243 6
        $map = null;
244
245 6
        foreach ($this->values as $value) {
246 6
            $key = $discriminator($value);
247
248 6
            if ($map === null) {
249 6
                $map = new Map(
250 6
                    $this->determineType($key),
251 6
                    StreamInterface::class
252
                );
253
            }
254
255 6
            if ($map->contains($key)) {
256 6
                $map = $map->put(
257 6
                    $key,
258 6
                    $map->get($key)->add($value)
259
                );
260
            } else {
261 6
                $map = $map->put(
262 6
                    $key,
263 6
                    (new self((string) $this->type))->add($value)
264
                );
265
            }
266
        }
267
268 6
        return $map;
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274 6
    public function first()
275
    {
276 6
        return $this->values->first();
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282 6
    public function last()
283
    {
284 6
        return $this->values->last();
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290 153
    public function contains($element): bool
291
    {
292 153
        return $this->values->contains($element);
293
    }
294
295
    /**
296
     * {@inheritdoc}
297
     */
298 57
    public function indexOf($element): int
299
    {
300 57
        return $this->values->indexOf($element);
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306 3
    public function indices(): StreamInterface
307
    {
308 3
        $stream = new self('int');
309 3
        $stream->values = $this->values->indices();
310
311 3
        return $stream;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317 9
    public function map(callable $function): StreamInterface
318
    {
319
        return $this
320 9
            ->values
321 9
            ->reduce(
322 9
                new self((string) $this->type),
323 9
                function(self $carry, $element) use($function): StreamInterface {
324 9
                    return $carry->add($function($element));
325 9
                }
326
            );
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332 6 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...
333
    {
334 6
        $this->spec->validate($element);
335
336 3
        $stream = clone $this;
337 3
        $stream->values = $this->values->pad($size, $element);
338
339 3
        return $stream;
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345 6
    public function partition(callable $predicate): MapInterface
346
    {
347 6
        $truthy = new self((string) $this->type);
348 6
        $falsy = new self((string) $this->type);
349
350 6
        foreach ($this->values as $value) {
351 6
            if ($predicate($value) === true) {
352 6
                $truthy = $truthy->add($value);
353
            } else {
354 6
                $falsy = $falsy->add($value);
355
            }
356
        }
357
358 6
        return (new Map('bool', StreamInterface::class))
359 6
            ->put(true, $truthy)
360 6
            ->put(false, $falsy);
361
    }
362
363
    /**
364
     * {@inheritdoc}
365
     */
366 9
    public function slice(int $from, int $until): StreamInterface
367
    {
368 9
        $stream = clone $this;
369 9
        $stream->values = $this->values->slice($from, $until);
370
371 9
        return $stream;
372
    }
373
374
    /**
375
     * {@inheritdoc}
376
     */
377 3
    public function splitAt(int $position): StreamInterface
378
    {
379 3
        $stream = new self(StreamInterface::class);
380 3
        $splitted = $this->values->splitAt($position);
381 3
        $first = new self((string) $this->type);
382 3
        $second = new self((string) $this->type);
383 3
        $first->values = $splitted->first();
384 3
        $second->values = $splitted->last();
385
386 3
        return $stream->add($first)->add($second);
387
    }
388
389
    /**
390
     * {@inheritdoc}
391
     */
392 24
    public function take(int $size): StreamInterface
393
    {
394 24
        $stream = clone $this;
395 24
        $stream->values = $this->values->take($size);
396
397 24
        return $stream;
398
    }
399
400
    /**
401
     * {@inheritdoc}
402
     */
403 3
    public function takeEnd(int $size): StreamInterface
404
    {
405 3
        $stream = clone $this;
406 3
        $stream->values = $this->values->takeEnd($size);
407
408 3
        return $stream;
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414 33 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...
415
    {
416 33
        $this->validate($stream);
417
418 30
        $self = clone $this;
419 30
        $self->values = $this->values->append(
420 30
            new Sequence(...$stream)
0 ignored issues
show
Documentation introduced by
new \Innmind\Immutable\Sequence(...$stream) is of type object<Innmind\Immutable\Sequence>, 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...
421
        );
422
423 30
        return $self;
424
    }
425
426
    /**
427
     * {@inheritdoc}
428
     */
429 9 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...
430
    {
431 9
        $this->validate($stream);
432
433 6
        $self = clone $this;
434 6
        $self->values = $this->values->intersect(
435 6
            new Sequence(...$stream)
0 ignored issues
show
Documentation introduced by
new \Innmind\Immutable\Sequence(...$stream) is of type object<Innmind\Immutable\Sequence>, 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...
436
        );
437
438 6
        return $self;
439
    }
440
441
    /**
442
     * {@inheritdoc}
443
     */
444 21
    public function join(string $separator): Str
445
    {
446 21
        return new Str((string) $this->values->join($separator));
447
    }
448
449
    /**
450
     * {@inheritdoc}
451
     */
452 297 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...
453
    {
454 297
        $this->spec->validate($element);
455
456 294
        $stream = clone $this;
457 294
        $stream->values = $this->values->add($element);
458
459 294
        return $stream;
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465 6
    public function sort(callable $function): StreamInterface
466
    {
467 6
        $stream = clone $this;
468 6
        $stream->values = $this->values->sort($function);
469
470 6
        return $stream;
471
    }
472
473
    /**
474
     * {@inheritdoc}
475
     */
476 60
    public function reduce($carry, callable $reducer)
477
    {
478 60
        return $this->values->reduce($carry, $reducer);
479
    }
480
481
    /**
482
     * {@inheritdoc}
483
     */
484 45
    public function clear(): StreamInterface
485
    {
486 45
        $self = clone $this;
487 45
        $self->values = new Sequence;
488
489 45
        return $self;
490
    }
491
492
    /**
493
     * {@inheritdoc}
494
     */
495 6
    public function reverse(): StreamInterface
496
    {
497 6
        $self = clone $this;
498 6
        $self->values = $this->values->reverse();
499
500 6
        return $self;
501
    }
502
503
    /**
504
     * Make sure the stream is compatible with the current one
505
     *
506
     * @param StreamInterface $stream
507
     *
508
     * @throws InvalidArgumentException
509
     *
510
     * @return void
511
     */
512 75
    private function validate(StreamInterface $stream)
513
    {
514 75
        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...
515 9
            throw new InvalidArgumentException(
516 9
                'The 2 streams does not reference the same type'
517
            );
518
        }
519 66
    }
520
}
521