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 ( cad621...d8b57f )
by Baptiste
04:31
created

Collection.php (29 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
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\SortException;
7
use Innmind\Immutable\Exception\OutOfBoundException;
8
use Innmind\Immutable\Exception\RuntimeException;
9
use Innmind\Immutable\Exception\InvalidArgumentException;
10
use Innmind\Immutable\Exception\LogicException;
11
12
/**
13
 * @deprecated To be removed in 2.0
14
 */
15
class Collection implements CollectionInterface
16
{
17
    private $values;
18
19 139
    public function __construct(array $values)
20
    {
21 139
        $this->values = $values;
22 139
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 97
    public function toPrimitive()
28
    {
29 97
        return $this->values;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function filter(callable $filter = null): CollectionInterface
36
    {
37 2
        if ($filter === null) {
38 2
            $values = array_filter($this->values);
39
        } else {
40 2
            $values = array_filter(
41 2
                $this->values,
42
                $filter,
43 2
                ARRAY_FILTER_USE_BOTH
44
            );
45
        }
46
47 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function intersect(CollectionInterface $collection): CollectionInterface
54
    {
55 2
        return new self(array_intersect(
56 2
            $this->values,
57 2
            $collection->toPrimitive()
58
        ));
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2 View Code Duplication
    public function chunk(int $size): CollectionInterface
65
    {
66 2
        $chunks = array_chunk($this->values, $size);
67 2
        $subs = [];
68
69 2
        foreach ($chunks as $chunk) {
70 2
            $subs[] = new self($chunk);
71
        }
72
73 2
        return new self($subs);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function shift(): CollectionInterface
80
    {
81 2
        $values = $this->values;
82 2
        array_shift($values);
83
84 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function reduce(callable $reducer, $initial = null)
91
    {
92 1
        return array_reduce($this->values, $reducer, $initial);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function search($needle, bool $strict = true)
99
    {
100 1
        return array_search($needle, $this->values, (bool) $strict);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function uintersect(CollectionInterface $collection, callable $intersecter): CollectionInterface
107
    {
108 2
        return new self(array_uintersect(
109 2
            $this->values,
110 2
            $collection->toPrimitive(),
111
            $intersecter
112
        ));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 2
    public function keyIntersect(CollectionInterface $collection): CollectionInterface
119
    {
120 2
        return new self(array_intersect_key(
121 2
            $this->values,
122 2
            $collection->toPrimitive()
123
        ));
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 3
    public function map(callable $mapper): CollectionInterface
130
    {
131 3
        return new self(array_map($mapper, $this->values));
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2
    public function pad(int $size, $value): CollectionInterface
138
    {
139 2
        return new self(array_pad($this->values, $size, $value));
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2
    public function pop(): CollectionInterface
146
    {
147 2
        $values = $this->values;
148 2
        array_pop($values);
149
150 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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): CollectionInterface
165
    {
166 2
        return new self(array_diff($this->values, $collection->toPrimitive()));
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 1
    public function flip(): CollectionInterface
173
    {
174 1
        return new self(array_flip($this->values));
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1
    public function keys($search = null, bool $strict = true): CollectionInterface
181
    {
182 1
        $args = func_get_args();
183
184 1
        if (count($args) > 0) {
185 1
            $keys = array_keys($this->values, $search, (bool) $strict);
186
        } else {
187 1
            $keys = array_keys($this->values);
188
        }
189
190 1
        return new self($keys);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 2
    public function push($value): CollectionInterface
197
    {
198 2
        $values = $this->values;
199 2
        array_push($values, $value);
200
201 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 4
    public function rand(int $num = 1): CollectionInterface
208
    {
209 4
        if ($num > $this->count()) {
210 2
            throw new OutOfBoundException(
211 2
                'Trying to return a wider collection than the current one'
212
            );
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
        }
221
222 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 2
    public function merge(CollectionInterface $collection): CollectionInterface
229
    {
230 2
        return new self(array_merge(
231 2
            $this->values,
232 2
            $collection->toPrimitive()
233
        ));
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239 2
    public function slice(int $offset, int $length = null, bool $preserveKeys = false): CollectionInterface
240
    {
241 2
        return new self(array_slice(
242 2
            $this->values,
243
            $offset,
244 2
            $length === null ? null : $length,
245
            $preserveKeys
246
        ));
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 2
    public function udiff(CollectionInterface $collection, callable $differ): CollectionInterface
253
    {
254 2
        return new self(array_udiff(
255 2
            $this->values,
256 2
            $collection->toPrimitive(),
257
            $differ
258
        ));
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 1
    public function column($key, $indexKey = null): CollectionInterface
265
    {
266 1
        return new self(array_column(
267 1
            $this->values,
268
            $key,
269
            $indexKey
270
        ));
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 2
    public function splice(int $offset, int $length = 0, $replacement = []): CollectionInterface
277
    {
278 2
        $values = $this->values;
279 2
        array_splice($values, $offset, $length, $replacement);
280
281 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 2
    public function unique(int $flags = self::SORT_REGULAR): CollectionInterface
288
    {
289 2
        return new self(array_unique($this->values, $flags));
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295 2
    public function values(): CollectionInterface
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): CollectionInterface
312
    {
313 2
        return new self(array_replace(
314 2
            $this->values,
315 2
            $collection->toPrimitive()
316
        ));
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 2
    public function reverse(bool $preserveKeys = false): CollectionInterface
323
    {
324 2
        return new self(array_reverse($this->values, $preserveKeys));
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330 2
    public function unshift($value): CollectionInterface
331
    {
332 2
        $values = $this->values;
333 2
        array_unshift($values, $value);
334
335 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341 2
    public function keyDiff(CollectionInterface $collection): CollectionInterface
342
    {
343 2
        return new self(array_diff_key(
344 2
            $this->values,
345 2
            $collection->toPrimitive()
346
        ));
347
    }
348
349
    /**
350
     * {@inheritdoc}
351
     */
352 2
    public function ukeyDiff(CollectionInterface $collection, callable $differ): CollectionInterface
353
    {
354 2
        return new self(array_diff_ukey(
355 2
            $this->values,
356 2
            $collection->toPrimitive(),
357
            $differ
358
        ));
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364 2
    public function associativeDiff(CollectionInterface $collection): CollectionInterface
365
    {
366 2
        return new self(array_diff_assoc(
367 2
            $this->values,
368 2
            $collection->toPrimitive()
369
        ));
370
    }
371
372
    /**
373
     * {@inheritdoc}
374
     */
375 47
    public function hasKey($key, bool $strict = true): bool
376
    {
377 47
        if ($strict === true) {
378 47
            $bool = array_key_exists($key, $this->values);
379
        } else {
380 1
            $bool = isset($this->values[$key]);
381
        }
382
383 47
        return $bool;
384
    }
385
386
    /**
387
     * {@inheritdoc}
388
     */
389 1
    public function countValues(): CollectionInterface
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): CollectionInterface
398
    {
399 2
        return new self(array_intersect_ukey(
400 2
            $this->values,
401 2
            $collection->toPrimitive(),
402
            $intersecter
403
        ));
404
    }
405
406
    /**
407
     * {@inheritdoc}
408
     */
409 2
    public function associativeIntersect(CollectionInterface $collection): CollectionInterface
410
    {
411 2
        return new self(array_intersect_assoc(
412 2
            $this->values,
413 2
            $collection->toPrimitive()
414
        ));
415
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420 2 View Code Duplication
    public function sort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
421
    {
422 2
        $values = $this->values;
423 2
        $bool = sort($values, $flags);
424
425 2
        if ($bool === false) {
426
            throw new SortException('Sort failure');
427
        }
428
429 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
430
    }
431
432
    /**
433
     * {@inheritdoc}
434
     */
435 2 View Code Duplication
    public function associativeSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
436
    {
437 2
        $values = $this->values;
438 2
        $bool = asort($values, $flags);
439
440 2
        if ($bool === false) {
441
            throw new SortException('Sort failure');
442
        }
443
444 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
445
    }
446
447
    /**
448
     * {@inheritdoc}
449
     */
450 2 View Code Duplication
    public function keySort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
451
    {
452 2
        $values = $this->values;
453 2
        $bool = ksort($values, $flags);
454
455 2
        if ($bool === false) {
456
            throw new SortException('Sort failure');
457
        }
458
459 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465 2 View Code Duplication
    public function ukeySort(callable $sorter): CollectionInterface
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);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
475
    }
476
477
    /**
478
     * {@inheritdoc}
479
     */
480 2 View Code Duplication
    public function reverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
481
    {
482 2
        $values = $this->values;
483 2
        $bool = rsort($values, $flags);
484
485 2
        if ($bool === false) {
486
            throw new SortException('Sort failure');
487
        }
488
489 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
490
    }
491
492
    /**
493
     * {@inheritdoc}
494
     */
495 2 View Code Duplication
    public function usort(callable $sorter): CollectionInterface
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);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
505
    }
506
507
    /**
508
     * {@inheritdoc}
509
     */
510 2 View Code Duplication
    public function associativeReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
511
    {
512 2
        $values = $this->values;
513 2
        $bool = arsort($values, $flags);
514
515 2
        if ($bool === false) {
516
            throw new SortException('Sort failure');
517
        }
518
519 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
520
    }
521
522
    /**
523
     * {@inheritdoc}
524
     */
525 2 View Code Duplication
    public function keyReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
526
    {
527 2
        $values = $this->values;
528 2
        $bool = krsort($values, $flags);
529
530 2
        if ($bool === false) {
531
            throw new SortException('Sort failure');
532
        }
533
534 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
535
    }
536
537
    /**
538
     * {@inheritdoc}
539
     */
540 2 View Code Duplication
    public function uassociativeSort(callable $sorter): CollectionInterface
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);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
550
    }
551
552
    /**
553
     * {@inheritdoc}
554
     */
555 2 View Code Duplication
    public function naturalSort(): CollectionInterface
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);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
565
    }
566
567
    /**
568
     * {@inheritdoc}
569
     */
570 2 View Code Duplication
    public function first()
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()
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): CollectionInterface
597
    {
598 1
        foreach ($this->values as $key => $value) {
599 1
            $callback($key, $value);
600
        }
601
602 1
        return $this;
603
    }
604
605
    /**
606
     * {@inheritdoc}
607
     */
608 2
    public function join(string $separator): string
609
    {
610 2
        return implode($separator, $this->values);
611
    }
612
613
    /**
614
     * {@inheritdoc}
615
     */
616 2 View Code Duplication
    public function shuffle(): CollectionInterface
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);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
626
    }
627
628
    /**
629
     * {@inheritdoc}
630
     */
631 2
    public function take(int $size, bool $preserveKeys = false): CollectionInterface
632
    {
633 2
        $took = [];
634 2
        $keys = array_keys($this->values);
635
636 2
        while (count($took) < $size) {
637
            do {
638 2
                $random = mt_rand(0, count($keys) - 1);
639 2
            } while (!isset($keys[$random]));
640 2
            $key = $keys[$random];
641 2
            $took[$key] = $this->values[$key];
642 2
            unset($keys[$random]);
643
        }
644
645 2
        if ($preserveKeys === false) {
646 2
            $took = array_values($took);
647
        }
648
649 2
        return new self($took);
650
    }
651
652
    /**
653
     * {@inheritdoc}
654
     */
655 2
    public function grep(string $pattern, bool $revert = false): CollectionInterface
656
    {
657 2
        return new self(preg_grep(
658
            $pattern,
659 2
            $this->values,
660 2
            $revert === false ? 0 : PREG_GREP_INVERT
661
        ));
662
    }
663
664
    /**
665
     * {@inheritdoc}
666
     */
667 2
    public function set($key, $value): CollectionInterface
668
    {
669 2
        $values = $this->values;
670 2
        $values[$key] = $value;
671
672 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
673
    }
674
675
    /**
676
     * {@inheritdoc}
677
     */
678 2
    public function contains($value): bool
679
    {
680 2
        return in_array($value, $this->values, true);
681
    }
682
683
    /**
684
     * {@inheritdoc}
685
     */
686 2
    public function get($key)
687
    {
688 2
        return $this->offsetGet($key);
689
    }
690
691
    /**
692
     * {@inheritdoc}
693
     */
694 2 View Code Duplication
    public function walk(callable $walker): CollectionInterface
695
    {
696 2
        $values = $this->values;
697
698 2
        if (array_walk($values, $walker) === false) {
699
            throw new RuntimeException('Walk operation failed');
700
        }
701
702 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
703
    }
704
705
    /**
706
     * {@inheritdoc}
707
     */
708 4
    public function unset($index): CollectionInterface
709
    {
710 4
        if (!$this->hasKey($index)) {
711 2
            throw new InvalidArgumentException(sprintf(
712 2
                'Unknown index %s',
713
                $index
714
            ));
715
        }
716
717 2
        $values = $this->values;
718 2
        unset($values[$index]);
719
720 2
        return new self($values);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\Collection has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
721
    }
722
723
    /**
724
     * {@inheritdoc}
725
     */
726 15
    public function count(): int
727
    {
728 15
        return count($this->values);
729
    }
730
731
    /**
732
     * {@inheritdoc}
733
     */
734 4
    public function current()
735
    {
736 4
        return current($this->values);
737
    }
738
739
    /**
740
     * {@inheritdoc}
741
     */
742 4
    public function key()
743
    {
744 4
        return key($this->values);
745
    }
746
747
    /**
748
     * {@inheritdoc}
749
     */
750 4
    public function next()
751
    {
752 4
        next($this->values);
753 4
    }
754
755
    /**
756
     * {@inheritdoc}
757
     */
758 4
    public function rewind()
759
    {
760 4
        reset($this->values);
761 4
    }
762
763
    /**
764
     * {@inheritdoc}
765
     */
766 4
    public function valid(): bool
767
    {
768 4
        return $this->key() !== null;
769
    }
770
771
    /**
772
     * {@inheritdoc}
773
     */
774 1
    public function offsetExists($offset): bool
775
    {
776 1
        return $this->hasKey($offset);
777
    }
778
779
    /**
780
     * {@inheritdoc}
781
     */
782 42
    public function offsetGet($offset)
783
    {
784 42
        if (!$this->hasKey($offset)) {
785 2
            throw new InvalidArgumentException(sprintf(
786 2
                'Unknown index %s',
787
                $offset
788
            ));
789
        }
790
791 40
        return $this->values[$offset];
792
    }
793
794
    /**
795
     * {@inheritdoc}
796
     */
797 1
    public function offsetSet($offset, $value)
798
    {
799 1
        throw new LogicException('You can\'t modify an immutable collection');
800
    }
801
802
    /**
803
     * {@inheritdoc}
804
     */
805 1
    public function offsetUnset($offset)
806
    {
807 1
        throw new LogicException('You can\'t modify an immutable collection');
808
    }
809
}
810