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
Branch flags (e657ae)
by Baptiste
02:13
created

Collection::grep()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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