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 ( f51db1...a35364 )
by Baptiste
08:25
created

Collection.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Innmind\Immutable;
4
5
use Innmind\Immutable\Exception\SortException;
6
use Innmind\Immutable\Exception\OutOfBoundException;
7
use Innmind\Immutable\Exception\RuntimeException;
8
use Innmind\Immutable\Exception\InvalidArgumentException;
9
use Innmind\Immutable\Exception\LogicException;
10
11
class Collection implements CollectionInterface
12
{
13
    private $values;
14
15 61
    public function __construct(array $values)
16
    {
17 61
        $this->values = $values;
18 61
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 49
    public function toPrimitive()
24
    {
25 49
        return $this->values;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function filter(callable $filter = null)
32
    {
33 1
        if ($filter === null) {
34 1
            $values = array_filter($this->values);
35 1
        } else {
36 1
            $values = array_filter(
37 1
                $this->values,
38 1
                $filter,
39
                ARRAY_FILTER_USE_BOTH
40 1
            );
41
        }
42
43 1
        return new self($values);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function intersect(CollectionInterface $collection)
50
    {
51 1
        return new self(array_intersect(
52 1
            $this->values,
53 1
            $collection->toPrimitive()
54 1
        ));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function chunk($size)
61
    {
62 1
        return new self(array_chunk($this->values, (int) $size));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function shift()
69
    {
70 1
        $values = $this->values;
71 1
        array_shift($values);
72
73 1
        return new self($values);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function reduce(callable $reducer, $initial = null)
80
    {
81 1
        return array_reduce($this->values, $reducer, $initial);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function search($needle, $strict = true)
88
    {
89 1
        return array_search($needle, $this->values, (bool) $strict);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function uintersect(CollectionInterface $collection, callable $intersecter)
96
    {
97 1
        return new self(array_uintersect(
98 1
            $this->values,
99 1
            $collection->toPrimitive(),
100
            $intersecter
101 1
        ));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function keyIntersect(CollectionInterface $collection)
108
    {
109 1
        return new self(array_intersect_key(
110 1
            $this->values,
111 1
            $collection->toPrimitive()
112 1
        ));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function map(callable $mapper)
119
    {
120 1
        return new self(array_map($mapper, $this->values));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function pad($size, $value)
127
    {
128 1
        return new self(array_pad($this->values, (int) $size, $value));
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function pop()
135
    {
136 1
        $values = $this->values;
137 1
        array_pop($values);
138
139 1
        return new self($values);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function sum()
146
    {
147 1
        return array_sum($this->values);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 1
    public function diff(CollectionInterface $collection)
154
    {
155 1
        return new self(array_diff($this->values, $collection->toPrimitive()));
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function flip()
162
    {
163 1
        return new self(array_flip($this->values));
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 1
    public function keys($search = null, $strict = true)
170
    {
171 1
        $args = func_get_args();
172
173 1
        if (count($args) > 0) {
174 1
            $keys = array_keys($this->values, $search, (bool) $strict);
175 1
        } else {
176 1
            $keys = array_keys($this->values);
177
        }
178
179 1
        return new self($keys);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 1
    public function push($value)
186
    {
187 1
        $values = $this->values;
188 1
        array_push($values, $value);
189
190 1
        return new self($values);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 2
    public function rand($num = 1)
197
    {
198 2
        if ((int) $num > $this->count()) {
199 1
            throw new OutOfBoundException(
200
                'Trying to return a wider collection than the current one'
201 1
            );
202
        }
203
204 1
        $keys = (array) array_rand($this->values, $num);
205 1
        $values = [];
206
207 1
        foreach ($keys as $key) {
208 1
            $values[$key] = $this->values[$key];
209 1
        }
210
211 1
        return new self($values);
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217 1
    public function merge(CollectionInterface $collection)
218
    {
219 1
        return new self(array_merge(
220 1
            $this->values,
221 1
            $collection->toPrimitive()
222 1
        ));
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 1
    public function slice($offset, $length = null, $preserveKeys = false)
229
    {
230 1
        return new self(array_slice(
231 1
            $this->values,
232 1
            (int) $offset,
233 1
            $length === null ? null : (int) $length,
234
            (bool) $preserveKeys
235 1
        ));
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 1
    public function udiff(CollectionInterface $collection, callable $differ)
242
    {
243 1
        return new self(array_udiff(
244 1
            $this->values,
245 1
            $collection->toPrimitive(),
246
            $differ
247 1
        ));
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253 1
    public function column($key, $indexKey = null)
254
    {
255 1
        return new self(array_column(
256 1
            $this->values,
257 1
            $key,
258
            $indexKey
259 1
        ));
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265 1
    public function splice($offset, $length = 0, $replacement = [])
266
    {
267 1
        $values = $this->values;
268 1
        array_splice($values, (int) $offset, (int) $length, $replacement);
269
270 1
        return new self($values);
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 1
    public function unique($flags = SORT_REGULAR)
277
    {
278 1
        return new self(array_unique($this->values, (int) $flags));
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284 1
    public function values()
285
    {
286 1
        return new self(array_values($this->values));
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 1
    public function product()
293
    {
294 1
        return array_product($this->values);
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 1
    public function replace(CollectionInterface $collection)
301
    {
302 1
        return new self(array_replace(
303 1
            $this->values,
304 1
            $collection->toPrimitive()
305 1
        ));
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311 1
    public function reverse($preserveKeys = false)
312
    {
313 1
        return new self(array_reverse($this->values, (bool) $preserveKeys));
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319 1
    public function unshift($value)
320
    {
321 1
        $values = $this->values;
322 1
        array_unshift($values, $value);
323
324 1
        return new self($values);
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330 1
    public function keyDiff(CollectionInterface $collection)
331
    {
332 1
        return new self(array_diff_key(
333 1
            $this->values,
334 1
            $collection->toPrimitive()
335 1
        ));
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341 1
    public function ukeyDiff(CollectionInterface $collection, callable $differ)
342
    {
343 1
        return new self(array_diff_ukey(
344 1
            $this->values,
345 1
            $collection->toPrimitive(),
346
            $differ
347 1
        ));
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353 1
    public function associativeDiff(CollectionInterface $collection)
354
    {
355 1
        return new self(array_diff_assoc(
356 1
            $this->values,
357 1
            $collection->toPrimitive()
358 1
        ));
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364 3
    public function hasKey($key, $strict = true)
365
    {
366 3
        if ((bool) $strict === true) {
367 3
            $bool = array_key_exists($key, $this->values);
368 3
        } else {
369 1
            $bool = isset($this->values[$key]);
370
        }
371
372 3
        return $bool;
373
    }
374
375
    /**
376
     * {@inheritdoc}
377
     */
378 1
    public function countValues()
379
    {
380 1
        return new self(array_count_values($this->values));
381
    }
382
383
    /**
384
     * {@inheritdoc}
385
     */
386 1
    public function ukeyIntersect(CollectionInterface $collection, callable $intersecter)
387
    {
388 1
        return new self(array_intersect_ukey(
389 1
            $this->values,
390 1
            $collection->toPrimitive(),
391
            $intersecter
392 1
        ));
393
    }
394
395
    /**
396
     * {@inheritdoc}
397
     */
398 1
    public function associativeIntersect(CollectionInterface $collection)
399
    {
400 1
        return new self(array_intersect_assoc(
401 1
            $this->values,
402 1
            $collection->toPrimitive()
403 1
        ));
404
    }
405
406
    /**
407
     * {@inheritdoc}
408
     */
409 1 View Code Duplication
    public function sort($flags = SORT_REGULAR)
0 ignored issues
show
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...
410
    {
411 1
        $values = $this->values;
412 1
        $bool = sort($values, (int) $flags);
413
414 1
        if ($bool === false) {
415
            throw new SortException('Sort failure');
416
        }
417
418 1
        return new self($values);
419
    }
420
421
    /**
422
     * {@inheritdoc}
423
     */
424 1 View Code Duplication
    public function associativeSort($flags = SORT_REGULAR)
0 ignored issues
show
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...
425
    {
426 1
        $values = $this->values;
427 1
        $bool = asort($values, (int) $flags);
428
429 1
        if ($bool === false) {
430
            throw new SortException('Sort failure');
431
        }
432
433 1
        return new self($values);
434
    }
435
436
    /**
437
     * {@inheritdoc}
438
     */
439 1 View Code Duplication
    public function keySort($flags = SORT_REGULAR)
0 ignored issues
show
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...
440
    {
441 1
        $values = $this->values;
442 1
        $bool = ksort($values, (int) $flags);
443
444 1
        if ($bool === false) {
445
            throw new SortException('Sort failure');
446
        }
447
448 1
        return new self($values);
449
    }
450
451
    /**
452
     * {@inheritdoc}
453
     */
454 1 View Code Duplication
    public function ukeySort(callable $sorter)
0 ignored issues
show
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...
455
    {
456 1
        $values = $this->values;
457 1
        $bool = uksort($values, $sorter);
458
459 1
        if ($bool === false) {
460
            throw new SortException('Sort failure');
461
        }
462
463 1
        return new self($values);
464
    }
465
466
    /**
467
     * {@inheritdoc}
468
     */
469 1 View Code Duplication
    public function reverseSort($flags = SORT_REGULAR)
0 ignored issues
show
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...
470
    {
471 1
        $values = $this->values;
472 1
        $bool = rsort($values, (int) $flags);
473
474 1
        if ($bool === false) {
475
            throw new SortException('Sort failure');
476
        }
477
478 1
        return new self($values);
479
    }
480
481
    /**
482
     * {@inheritdoc}
483
     */
484 1 View Code Duplication
    public function usort(callable $sorter)
0 ignored issues
show
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...
485
    {
486 1
        $values = $this->values;
487 1
        $bool = usort($values, $sorter);
488
489 1
        if ($bool === false) {
490
            throw new SortException('Sort failure');
491
        }
492
493 1
        return new self($values);
494
    }
495
496
    /**
497
     * {@inheritdoc}
498
     */
499 1 View Code Duplication
    public function associativeReverseSort($flags = SORT_REGULAR)
0 ignored issues
show
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...
500
    {
501 1
        $values = $this->values;
502 1
        $bool = arsort($values, (int) $flags);
503
504 1
        if ($bool === false) {
505
            throw new SortException('Sort failure');
506
        }
507
508 1
        return new self($values);
509
    }
510
511
    /**
512
     * {@inheritdoc}
513
     */
514 1 View Code Duplication
    public function keyReverseSort($flags = SORT_REGULAR)
0 ignored issues
show
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...
515
    {
516 1
        $values = $this->values;
517 1
        $bool = krsort($values, (int) $flags);
518
519 1
        if ($bool === false) {
520
            throw new SortException('Sort failure');
521
        }
522
523 1
        return new self($values);
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     */
529 1 View Code Duplication
    public function uassociativeSort(callable $sorter)
0 ignored issues
show
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...
530
    {
531 1
        $values = $this->values;
532 1
        $bool = uasort($values, $sorter);
533
534 1
        if ($bool === false) {
535
            throw new SortException('Sort failure');
536
        }
537
538 1
        return new self($values);
539
    }
540
541
    /**
542
     * {@inheritdoc}
543
     */
544 1
    public function naturalSort()
545
    {
546 1
        $values = $this->values;
547 1
        $bool = natsort($values);
548
549 1
        if ($bool === false) {
550
            throw new SortException('Sort failure');
551
        }
552
553 1
        return new self($values);
554
    }
555
556
    /**
557
     * {@inheritdoc}
558
     */
559 2 View Code Duplication
    public function first()
0 ignored issues
show
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...
560
    {
561 2
        if ($this->count() === 0) {
562 1
            throw new OutOfBoundException('There is no first item');
563
        }
564
565 1
        return array_values($this->values)[0];
566
    }
567
568
    /**
569
     * {@inheritdoc}
570
     */
571 2 View Code Duplication
    public function last()
0 ignored issues
show
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...
572
    {
573 2
        if ($this->count() === 0) {
574 1
            throw new OutOfBoundException('There is no last item');
575
        }
576
577 1
        $values = array_values($this->values);
578
579 1
        return end($values);
580
    }
581
582
    /**
583
     * {@inheritdoc}
584
     */
585 1
    public function each(callable $callback)
586
    {
587 1
        foreach ($this->values as $key => $value) {
588 1
            $callback($key, $value);
589 1
        }
590
591 1
        return $this;
592
    }
593
594
    /**
595
     * {@inheritdoc}
596
     */
597 1
    public function join($separator)
598
    {
599 1
        return implode((string) $separator, $this->values);
600
    }
601
602
    /**
603
     * {@inheritdoc}
604
     */
605 1
    public function shuffle()
606
    {
607 1
        $values = $this->values;
608 1
        $result = shuffle($values);
609
610 1
        if ($result === false) {
611
            throw new RuntimeException('Shuffle operation failed');
612
        }
613
614 1
        return new self($values);
615
    }
616
617
    /**
618
     * {@inheritdoc}
619
     */
620 1
    public function take($size, $preserveKeys = false)
621
    {
622 1
        $took = [];
623 1
        $keys = array_keys($this->values);
624 1
        $size = (int) $size;
625
626 1
        while (count($took) < $size) {
627
            do {
628 1
                $random = mt_rand(0, count($keys) - 1);
629 1
            } while (!isset($keys[$random]));
630 1
            $key = $keys[$random];
631 1
            $took[$key] = $this->values[$key];
632 1
            unset($keys[$random]);
633 1
        }
634
635 1
        if ($preserveKeys === false) {
636 1
            $took = array_values($took);
637 1
        }
638
639 1
        return new self($took);
640
    }
641
642
    /**
643
     * {@inheritdoc}
644
     */
645 9
    public function count()
646
    {
647 9
        return count($this->values);
648
    }
649
650
    /**
651
     * {@inheritdoc}
652
     */
653 1
    public function current()
654
    {
655 1
        return current($this->values);
656
    }
657
658
    /**
659
     * {@inheritdoc}
660
     */
661 1
    public function key()
662
    {
663 1
        return key($this->values);
664
    }
665
666
    /**
667
     * {@inheritdoc}
668
     */
669 1
    public function next()
670
    {
671 1
        next($this->values);
672 1
    }
673
674
    /**
675
     * {@inheritdoc}
676
     */
677 1
    public function rewind()
678
    {
679 1
        reset($this->values);
680 1
    }
681
682
    /**
683
     * {@inheritdoc}
684
     */
685 1
    public function valid()
686
    {
687 1
        return $this->key() !== null;
688
    }
689
690
    /**
691
     * {@inheritdoc}
692
     */
693 1
    public function offsetExists($offset)
694
    {
695 1
        return $this->hasKey($offset);
696
    }
697
698
    /**
699
     * {@inheritdoc}
700
     */
701 2
    public function offsetGet($offset)
702
    {
703 2
        if (!$this->hasKey($offset)) {
704 1
            throw new InvalidArgumentException(sprintf(
705 1
                'Unknown index %s',
706
                $offset
707 1
            ));
708
        }
709
710 1
        return $this->values[$offset];
711
    }
712
713
    /**
714
     * {@inheritdoc}
715
     */
716 1
    public function offsetSet($offset, $value)
717
    {
718 1
        throw new LogicException('You can\'t modify an immutable collection');
719
    }
720
721
    /**
722
     * {@inheritdoc}
723
     */
724 1
    public function offsetUnset($offset)
725
    {
726 1
        throw new LogicException('You can\'t modify an immutable collection');
727
    }
728
}
729