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.
x Sorry, these patches are not available anymore due to data migration. Please run a fresh inspection.
Completed
Push — master ( f1ee52...9a2238 )
by Baptiste
02:22
created

Collection.php (1 issue)

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