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::rand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
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
0 ignored issues
show
Deprecated Code introduced by
The interface Innmind\Immutable\CollectionInterface 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...
16
{
17
    private $values;
18
19 139
    public function __construct(array $values)
20
    {
21 139
        $this->values = $values;
22
23 139
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
24 139
            'Uset Set, Map or Stream instead',
25 139
            E_USER_DEPRECATED
26
        );
27 139
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 97
    public function toPrimitive()
33
    {
34 97
        return $this->values;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function filter(callable $filter = null): CollectionInterface
41
    {
42 2
        if ($filter === null) {
43 2
            $values = array_filter($this->values);
44
        } else {
45 2
            $values = array_filter(
46 2
                $this->values,
47
                $filter,
48 2
                ARRAY_FILTER_USE_BOTH
49
            );
50
        }
51
52 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...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2
    public function intersect(CollectionInterface $collection): CollectionInterface
59
    {
60 2
        return new self(array_intersect(
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...
61 2
            $this->values,
62 2
            $collection->toPrimitive()
63
        ));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 2 View Code Duplication
    public function chunk(int $size): CollectionInterface
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...
70
    {
71 2
        $chunks = array_chunk($this->values, $size);
72 2
        $subs = [];
73
74 2
        foreach ($chunks as $chunk) {
75 2
            $subs[] = new self($chunk);
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...
76
        }
77
78 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...
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function shift(): CollectionInterface
85
    {
86 2
        $values = $this->values;
87 2
        array_shift($values);
88
89 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...
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function reduce(callable $reducer, $initial = null)
96
    {
97 1
        return array_reduce($this->values, $reducer, $initial);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function search($needle, bool $strict = true)
104
    {
105 1
        return array_search($needle, $this->values, (bool) $strict);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function uintersect(CollectionInterface $collection, callable $intersecter): CollectionInterface
112
    {
113 2
        return new self(array_uintersect(
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...
114 2
            $this->values,
115 2
            $collection->toPrimitive(),
116
            $intersecter
117
        ));
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 2
    public function keyIntersect(CollectionInterface $collection): CollectionInterface
124
    {
125 2
        return new self(array_intersect_key(
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...
126 2
            $this->values,
127 2
            $collection->toPrimitive()
128
        ));
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    public function map(callable $mapper): CollectionInterface
135
    {
136 3
        return new self(array_map($mapper, $this->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...
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 2
    public function pad(int $size, $value): CollectionInterface
143
    {
144 2
        return new self(array_pad($this->values, $size, $value));
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...
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 2
    public function pop(): CollectionInterface
151
    {
152 2
        $values = $this->values;
153 2
        array_pop($values);
154
155 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...
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function sum()
162
    {
163 1
        return array_sum($this->values);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 2
    public function diff(CollectionInterface $collection): CollectionInterface
170
    {
171 2
        return new self(array_diff($this->values, $collection->toPrimitive()));
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...
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 1
    public function flip(): CollectionInterface
178
    {
179 1
        return new self(array_flip($this->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...
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 1
    public function keys($search = null, bool $strict = true): CollectionInterface
186
    {
187 1
        $args = func_get_args();
188
189 1
        if (count($args) > 0) {
190 1
            $keys = array_keys($this->values, $search, (bool) $strict);
191
        } else {
192 1
            $keys = array_keys($this->values);
193
        }
194
195 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...
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201 2
    public function push($value): CollectionInterface
202
    {
203 2
        $values = $this->values;
204 2
        array_push($values, $value);
205
206 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...
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 4
    public function rand(int $num = 1): CollectionInterface
213
    {
214 4
        if ($num > $this->count()) {
215 2
            throw new OutOfBoundException(
216 2
                'Trying to return a wider collection than the current one'
217
            );
218
        }
219
220 2
        $keys = (array) array_rand($this->values, $num);
221 2
        $values = [];
222
223 2
        foreach ($keys as $key) {
224 2
            $values[$key] = $this->values[$key];
225
        }
226
227 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...
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233 2
    public function merge(CollectionInterface $collection): CollectionInterface
234
    {
235 2
        return new self(array_merge(
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...
236 2
            $this->values,
237 2
            $collection->toPrimitive()
238
        ));
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 2
    public function slice(int $offset, int $length = null, bool $preserveKeys = false): CollectionInterface
245
    {
246 2
        return new self(array_slice(
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...
247 2
            $this->values,
248
            $offset,
249 2
            $length === null ? null : $length,
250
            $preserveKeys
251
        ));
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257 2
    public function udiff(CollectionInterface $collection, callable $differ): CollectionInterface
258
    {
259 2
        return new self(array_udiff(
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...
260 2
            $this->values,
261 2
            $collection->toPrimitive(),
262
            $differ
263
        ));
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269 1
    public function column($key, $indexKey = null): CollectionInterface
270
    {
271 1
        return new self(array_column(
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...
272 1
            $this->values,
273
            $key,
274
            $indexKey
275
        ));
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281 2
    public function splice(int $offset, int $length = 0, $replacement = []): CollectionInterface
282
    {
283 2
        $values = $this->values;
284 2
        array_splice($values, $offset, $length, $replacement);
285
286 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...
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 2
    public function unique(int $flags = self::SORT_REGULAR): CollectionInterface
293
    {
294 2
        return new self(array_unique($this->values, $flags));
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...
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 2
    public function values(): CollectionInterface
301
    {
302 2
        return new self(array_values($this->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...
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308 1
    public function product()
309
    {
310 1
        return array_product($this->values);
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316 2
    public function replace(CollectionInterface $collection): CollectionInterface
317
    {
318 2
        return new self(array_replace(
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...
319 2
            $this->values,
320 2
            $collection->toPrimitive()
321
        ));
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327 2
    public function reverse(bool $preserveKeys = false): CollectionInterface
328
    {
329 2
        return new self(array_reverse($this->values, $preserveKeys));
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...
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335 2
    public function unshift($value): CollectionInterface
336
    {
337 2
        $values = $this->values;
338 2
        array_unshift($values, $value);
339
340 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...
341
    }
342
343
    /**
344
     * {@inheritdoc}
345
     */
346 2
    public function keyDiff(CollectionInterface $collection): CollectionInterface
347
    {
348 2
        return new self(array_diff_key(
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...
349 2
            $this->values,
350 2
            $collection->toPrimitive()
351
        ));
352
    }
353
354
    /**
355
     * {@inheritdoc}
356
     */
357 2
    public function ukeyDiff(CollectionInterface $collection, callable $differ): CollectionInterface
358
    {
359 2
        return new self(array_diff_ukey(
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...
360 2
            $this->values,
361 2
            $collection->toPrimitive(),
362
            $differ
363
        ));
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369 2
    public function associativeDiff(CollectionInterface $collection): CollectionInterface
370
    {
371 2
        return new self(array_diff_assoc(
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...
372 2
            $this->values,
373 2
            $collection->toPrimitive()
374
        ));
375
    }
376
377
    /**
378
     * {@inheritdoc}
379
     */
380 47
    public function hasKey($key, bool $strict = true): bool
381
    {
382 47
        if ($strict === true) {
383 47
            $bool = array_key_exists($key, $this->values);
384
        } else {
385 1
            $bool = isset($this->values[$key]);
386
        }
387
388 47
        return $bool;
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     */
394 1
    public function countValues(): CollectionInterface
395
    {
396 1
        return new self(array_count_values($this->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...
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402 2
    public function ukeyIntersect(CollectionInterface $collection, callable $intersecter): CollectionInterface
403
    {
404 2
        return new self(array_intersect_ukey(
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...
405 2
            $this->values,
406 2
            $collection->toPrimitive(),
407
            $intersecter
408
        ));
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414 2
    public function associativeIntersect(CollectionInterface $collection): CollectionInterface
415
    {
416 2
        return new self(array_intersect_assoc(
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...
417 2
            $this->values,
418 2
            $collection->toPrimitive()
419
        ));
420
    }
421
422
    /**
423
     * {@inheritdoc}
424
     */
425 2 View Code Duplication
    public function sort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
426
    {
427 2
        $values = $this->values;
428 2
        $bool = sort($values, $flags);
429
430 2
        if ($bool === false) {
431
            throw new SortException('Sort failure');
432
        }
433
434 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...
435
    }
436
437
    /**
438
     * {@inheritdoc}
439
     */
440 2 View Code Duplication
    public function associativeSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
441
    {
442 2
        $values = $this->values;
443 2
        $bool = asort($values, $flags);
444
445 2
        if ($bool === false) {
446
            throw new SortException('Sort failure');
447
        }
448
449 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...
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455 2 View Code Duplication
    public function keySort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
456
    {
457 2
        $values = $this->values;
458 2
        $bool = ksort($values, $flags);
459
460 2
        if ($bool === false) {
461
            throw new SortException('Sort failure');
462
        }
463
464 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...
465
    }
466
467
    /**
468
     * {@inheritdoc}
469
     */
470 2 View Code Duplication
    public function ukeySort(callable $sorter): CollectionInterface
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...
471
    {
472 2
        $values = $this->values;
473 2
        $bool = uksort($values, $sorter);
474
475 2
        if ($bool === false) {
476
            throw new SortException('Sort failure');
477
        }
478
479 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...
480
    }
481
482
    /**
483
     * {@inheritdoc}
484
     */
485 2 View Code Duplication
    public function reverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
486
    {
487 2
        $values = $this->values;
488 2
        $bool = rsort($values, $flags);
489
490 2
        if ($bool === false) {
491
            throw new SortException('Sort failure');
492
        }
493
494 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...
495
    }
496
497
    /**
498
     * {@inheritdoc}
499
     */
500 2 View Code Duplication
    public function usort(callable $sorter): CollectionInterface
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...
501
    {
502 2
        $values = $this->values;
503 2
        $bool = usort($values, $sorter);
504
505 2
        if ($bool === false) {
506
            throw new SortException('Sort failure');
507
        }
508
509 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...
510
    }
511
512
    /**
513
     * {@inheritdoc}
514
     */
515 2 View Code Duplication
    public function associativeReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
516
    {
517 2
        $values = $this->values;
518 2
        $bool = arsort($values, $flags);
519
520 2
        if ($bool === false) {
521
            throw new SortException('Sort failure');
522
        }
523
524 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...
525
    }
526
527
    /**
528
     * {@inheritdoc}
529
     */
530 2 View Code Duplication
    public function keyReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface
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...
531
    {
532 2
        $values = $this->values;
533 2
        $bool = krsort($values, $flags);
534
535 2
        if ($bool === false) {
536
            throw new SortException('Sort failure');
537
        }
538
539 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...
540
    }
541
542
    /**
543
     * {@inheritdoc}
544
     */
545 2 View Code Duplication
    public function uassociativeSort(callable $sorter): CollectionInterface
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...
546
    {
547 2
        $values = $this->values;
548 2
        $bool = uasort($values, $sorter);
549
550 2
        if ($bool === false) {
551
            throw new SortException('Sort failure');
552
        }
553
554 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...
555
    }
556
557
    /**
558
     * {@inheritdoc}
559
     */
560 2 View Code Duplication
    public function naturalSort(): CollectionInterface
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...
561
    {
562 2
        $values = $this->values;
563 2
        $bool = natsort($values);
564
565 2
        if ($bool === false) {
566
            throw new SortException('Sort failure');
567
        }
568
569 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...
570
    }
571
572
    /**
573
     * {@inheritdoc}
574
     */
575 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...
576
    {
577 2
        if ($this->count() === 0) {
578 1
            throw new OutOfBoundException('There is no first item');
579
        }
580
581 1
        return array_values($this->values)[0];
582
    }
583
584
    /**
585
     * {@inheritdoc}
586
     */
587 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...
588
    {
589 2
        if ($this->count() === 0) {
590 1
            throw new OutOfBoundException('There is no last item');
591
        }
592
593 1
        $values = array_values($this->values);
594
595 1
        return end($values);
596
    }
597
598
    /**
599
     * {@inheritdoc}
600
     */
601 1
    public function each(callable $callback): CollectionInterface
602
    {
603 1
        foreach ($this->values as $key => $value) {
604 1
            $callback($key, $value);
605
        }
606
607 1
        return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\Immutable\Collection) is incompatible with the return type declared by the interface Innmind\Immutable\CollectionInterface::each of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
608
    }
609
610
    /**
611
     * {@inheritdoc}
612
     */
613 2
    public function join(string $separator): string
614
    {
615 2
        return implode($separator, $this->values);
616
    }
617
618
    /**
619
     * {@inheritdoc}
620
     */
621 2 View Code Duplication
    public function shuffle(): CollectionInterface
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...
622
    {
623 2
        $values = $this->values;
624 2
        $result = shuffle($values);
625
626 2
        if ($result === false) {
627
            throw new RuntimeException('Shuffle operation failed');
628
        }
629
630 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...
631
    }
632
633
    /**
634
     * {@inheritdoc}
635
     */
636 2
    public function take(int $size, bool $preserveKeys = false): CollectionInterface
637
    {
638 2
        $took = [];
639 2
        $keys = array_keys($this->values);
640
641 2
        while (count($took) < $size) {
642
            do {
643 2
                $random = mt_rand(0, count($keys) - 1);
644 2
            } while (!isset($keys[$random]));
645 2
            $key = $keys[$random];
646 2
            $took[$key] = $this->values[$key];
647 2
            unset($keys[$random]);
648
        }
649
650 2
        if ($preserveKeys === false) {
651 2
            $took = array_values($took);
652
        }
653
654 2
        return new self($took);
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...
655
    }
656
657
    /**
658
     * {@inheritdoc}
659
     */
660 2
    public function grep(string $pattern, bool $revert = false): CollectionInterface
661
    {
662 2
        return new self(preg_grep(
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...
663
            $pattern,
664 2
            $this->values,
665 2
            $revert === false ? 0 : PREG_GREP_INVERT
666
        ));
667
    }
668
669
    /**
670
     * {@inheritdoc}
671
     */
672 2
    public function set($key, $value): CollectionInterface
673
    {
674 2
        $values = $this->values;
675 2
        $values[$key] = $value;
676
677 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...
678
    }
679
680
    /**
681
     * {@inheritdoc}
682
     */
683 2
    public function contains($value): bool
684
    {
685 2
        return in_array($value, $this->values, true);
686
    }
687
688
    /**
689
     * {@inheritdoc}
690
     */
691 2
    public function get($key)
692
    {
693 2
        return $this->offsetGet($key);
694
    }
695
696
    /**
697
     * {@inheritdoc}
698
     */
699 2 View Code Duplication
    public function walk(callable $walker): CollectionInterface
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...
700
    {
701 2
        $values = $this->values;
702
703 2
        if (array_walk($values, $walker) === false) {
704
            throw new RuntimeException('Walk operation failed');
705
        }
706
707 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...
708
    }
709
710
    /**
711
     * {@inheritdoc}
712
     */
713 4
    public function unset($index): CollectionInterface
714
    {
715 4
        if (!$this->hasKey($index)) {
716 2
            throw new InvalidArgumentException(sprintf(
717 2
                'Unknown index %s',
718
                $index
719
            ));
720
        }
721
722 2
        $values = $this->values;
723 2
        unset($values[$index]);
724
725 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...
726
    }
727
728
    /**
729
     * {@inheritdoc}
730
     */
731 15
    public function count(): int
732
    {
733 15
        return count($this->values);
734
    }
735
736
    /**
737
     * {@inheritdoc}
738
     */
739 4
    public function current()
740
    {
741 4
        return current($this->values);
742
    }
743
744
    /**
745
     * {@inheritdoc}
746
     */
747 4
    public function key()
748
    {
749 4
        return key($this->values);
750
    }
751
752
    /**
753
     * {@inheritdoc}
754
     */
755 4
    public function next()
756
    {
757 4
        next($this->values);
758 4
    }
759
760
    /**
761
     * {@inheritdoc}
762
     */
763 4
    public function rewind()
764
    {
765 4
        reset($this->values);
766 4
    }
767
768
    /**
769
     * {@inheritdoc}
770
     */
771 4
    public function valid(): bool
772
    {
773 4
        return $this->key() !== null;
774
    }
775
776
    /**
777
     * {@inheritdoc}
778
     */
779 1
    public function offsetExists($offset): bool
780
    {
781 1
        return $this->hasKey($offset);
782
    }
783
784
    /**
785
     * {@inheritdoc}
786
     */
787 42
    public function offsetGet($offset)
788
    {
789 42
        if (!$this->hasKey($offset)) {
790 2
            throw new InvalidArgumentException(sprintf(
791 2
                'Unknown index %s',
792
                $offset
793
            ));
794
        }
795
796 40
        return $this->values[$offset];
797
    }
798
799
    /**
800
     * {@inheritdoc}
801
     */
802 1
    public function offsetSet($offset, $value)
803
    {
804 1
        throw new LogicException('You can\'t modify an immutable collection');
805
    }
806
807
    /**
808
     * {@inheritdoc}
809
     */
810 1
    public function offsetUnset($offset)
811
    {
812 1
        throw new LogicException('You can\'t modify an immutable collection');
813
    }
814
}
815