Passed
Push — master ( 4e0120...c40ba3 )
by Pol
15:03
created

Collection::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection;
6
7
use loophp\collection\Contract\Base as BaseInterface;
8
use loophp\collection\Contract\Collection as CollectionInterface;
9
use loophp\collection\Operation\Append;
10
use loophp\collection\Operation\Apply;
11
use loophp\collection\Operation\Chunk;
12
use loophp\collection\Operation\Collapse;
13
use loophp\collection\Operation\Combinate;
14
use loophp\collection\Operation\Combine;
15
use loophp\collection\Operation\Cycle;
16
use loophp\collection\Operation\Distinct;
17
use loophp\collection\Operation\Explode;
18
use loophp\collection\Operation\Filter;
19
use loophp\collection\Operation\Flatten;
20
use loophp\collection\Operation\Flip;
21
use loophp\collection\Operation\Forget;
22
use loophp\collection\Operation\Intersperse;
23
use loophp\collection\Operation\Iterate;
24
use loophp\collection\Operation\Keys;
25
use loophp\collection\Operation\Limit;
26
use loophp\collection\Operation\Merge;
27
use loophp\collection\Operation\Normalize;
28
use loophp\collection\Operation\Nth;
29
use loophp\collection\Operation\Only;
30
use loophp\collection\Operation\Pad;
31
use loophp\collection\Operation\Permutate;
32
use loophp\collection\Operation\Pluck;
33
use loophp\collection\Operation\Prepend;
34
use loophp\collection\Operation\Product;
35
use loophp\collection\Operation\Range;
36
use loophp\collection\Operation\Reduction;
37
use loophp\collection\Operation\Reverse;
38
use loophp\collection\Operation\Scale;
39
use loophp\collection\Operation\Skip;
40
use loophp\collection\Operation\Slice;
41
use loophp\collection\Operation\Sort;
42
use loophp\collection\Operation\Split;
43
use loophp\collection\Operation\Tail;
44
use loophp\collection\Operation\Times;
45
use loophp\collection\Operation\Until;
46
use loophp\collection\Operation\Walk;
47
use loophp\collection\Operation\Zip;
48
use loophp\collection\Transformation\All;
49
use loophp\collection\Transformation\Contains;
50
use loophp\collection\Transformation\Count;
51
use loophp\collection\Transformation\First;
52
use loophp\collection\Transformation\Get;
53
use loophp\collection\Transformation\Implode;
54
use loophp\collection\Transformation\Last;
55
use loophp\collection\Transformation\Reduce;
56
57
use const INF;
58
use const PHP_INT_MAX;
59
60
/**
61
 * Class Collection.
62
 */
63
final class Collection extends Base implements CollectionInterface
64
{
65
    /**
66
     * {@inheritdoc}
67 10
     */
68
    public function all(): array
69 10
    {
70
        return $this->transform(new All());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transform(...n\Transformation\All()) could return the type boolean which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @return \loophp\collection\Contract\Collection
77 2
     */
78
    public function append(...$items): BaseInterface
79 2
    {
80
        return $this->run(new Append($items));
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @return \loophp\collection\Contract\Collection
87 1
     */
88
    public function apply(callable ...$callables): BaseInterface
89 1
    {
90
        return $this->run(new Apply(...$callables));
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     *
96
     * @return \loophp\collection\Contract\Collection
97 2
     */
98
    public function chunk(int $size): BaseInterface
99 2
    {
100
        return $this->run(new Chunk($size));
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * @return \loophp\collection\Contract\Collection
107 1
     */
108
    public function collapse(): BaseInterface
109 1
    {
110
        return $this->run(new Collapse());
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     *
116
     * @return \loophp\collection\Contract\Collection
117 1
     */
118
    public function combinate(?int $size = null): BaseInterface
119 1
    {
120
        return $this->run(new Combinate($size));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     *
126
     * @return \loophp\collection\Contract\Collection
127 1
     */
128
    public function combine($keys): BaseInterface
129 1
    {
130
        return $this->run(new Combine($keys));
131
    }
132
133
    /**
134
     * {@inheritdoc}
135 2
     */
136
    public function contains($key): bool
137 2
    {
138
        return $this->transform(new Contains($key));
139
    }
140
141
    /**
142
     * {@inheritdoc}
143 2
     */
144
    public function count(): int
145 2
    {
146
        return $this->transform(new Count());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transform(...Transformation\Count()) could return the type boolean which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     *
152
     * @return \loophp\collection\Contract\Collection
153 1
     */
154
    public function cycle(int $length = 0): BaseInterface
155 1
    {
156
        return $this->run(new Cycle($length));
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     *
162
     * @return \loophp\collection\Contract\Collection
163 1
     */
164
    public function distinct(): BaseInterface
165 1
    {
166
        return $this->run(new Distinct());
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     *
172
     * @return \loophp\collection\Contract\Collection
173 2
     */
174
    public static function empty(): CollectionInterface
175 2
    {
176
        return new Collection();
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     *
182
     * @return \loophp\collection\Contract\Collection
183 1
     */
184
    public function explode(string ...$strings): BaseInterface
185 1
    {
186
        return $this->run(new Explode(...$strings));
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     *
192
     * @return \loophp\collection\Contract\Collection
193 2
     */
194
    public function filter(callable ...$callbacks): BaseInterface
195 2
    {
196
        return $this->run(new Filter(...$callbacks));
197
    }
198
199
    /**
200
     * {@inheritdoc}
201 1
     */
202
    public function first(?callable $callback = null, $default = null)
203 1
    {
204
        return $this->transform(new First($callback, $default));
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     *
210
     * @return \loophp\collection\Contract\Collection
211 1
     */
212
    public function flatten(int $depth = PHP_INT_MAX): BaseInterface
213 1
    {
214
        return $this->run(new Flatten($depth));
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     *
220
     * @return \loophp\collection\Contract\Collection
221 2
     */
222
    public function flip(): BaseInterface
223 2
    {
224
        return $this->run(new Flip());
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     *
230
     * @return \loophp\collection\Contract\Collection
231 1
     */
232
    public function forget(...$keys): BaseInterface
233 1
    {
234
        return $this->run(new Forget($keys));
235
    }
236
237
    /**
238
     * {@inheritdoc}
239 1
     */
240
    public function get($key, $default = null)
241 1
    {
242
        return $this->transform(new Get($key, $default));
243
    }
244
245
    /**
246
     * {@inheritdoc}
247 1
     */
248
    public function implode(string $glue = ''): string
249 1
    {
250
        return $this->transform(new Implode($glue));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transform(...rmation\Implode($glue)) could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     *
256
     * @return \loophp\collection\Contract\Collection
257 1
     */
258
    public function intersperse($element, int $every = 1, int $startAt = 0): BaseInterface
259 1
    {
260
        return $this->run(new Intersperse($element, $every, $startAt));
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     *
266
     * @return \loophp\collection\Contract\Collection
267 2
     */
268
    public static function iterate(callable $callback, ...$parameters): CollectionInterface
269 2
    {
270
        return (new Collection())->run(new Iterate($callback, $parameters));
271 2
    }
272 2
273
    /**
274 2
     * {@inheritdoc}
275
     *
276 2
     * @return \loophp\collection\Contract\Collection
277
     */
278 2
    public function keys(): BaseInterface
279
    {
280
        return $this->run(new Keys());
281
    }
282
283
    /**
284
     * {@inheritdoc}
285
     */
286
    public function last()
287 1
    {
288
        return $this->transform(new Last());
289 1
    }
290
291
    /**
292
     * {@inheritdoc}
293
     *
294
     * @return \loophp\collection\Contract\Collection
295 1
     */
296
    public function limit(int $limit): BaseInterface
297 1
    {
298
        return $this->run(new Limit($limit));
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     *
304
     * @return \loophp\collection\Contract\Collection
305 4
     */
306
    public function map(callable ...$callbacks): BaseInterface
307 4
    {
308
        return $this->run(new Walk(...$callbacks), new Normalize());
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     *
314
     * @return \loophp\collection\Contract\Collection
315 2
     */
316
    public function merge(...$sources): BaseInterface
317 2
    {
318
        return $this->run(new Merge($sources));
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     *
324
     * @return \loophp\collection\Contract\Collection
325 1
     */
326
    public function normalize(): BaseInterface
327 1
    {
328
        return $this->run(new Normalize());
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     *
334
     * @return \loophp\collection\Contract\Collection
335 1
     */
336
    public function nth(int $step, int $offset = 0): BaseInterface
337 1
    {
338
        return $this->run(new Nth($step, $offset));
339
    }
340
341
    /**
342
     * {@inheritdoc}
343
     *
344
     * @return \loophp\collection\Contract\Collection
345 1
     */
346
    public function only(...$keys): BaseInterface
347 1
    {
348
        return $this->run(new Only($keys));
349
    }
350
351
    /**
352
     * {@inheritdoc}
353
     *
354
     * @return \loophp\collection\Contract\Collection
355 1
     */
356
    public function pad(int $size, $value): BaseInterface
357 1
    {
358
        return $this->run(new Pad($size, $value));
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     *
364
     * @return \loophp\collection\Contract\Collection
365 1
     */
366
    public function permutate(): BaseInterface
367 1
    {
368
        return $this->run(new Permutate());
369
    }
370
371
    /**
372
     * {@inheritdoc}
373
     *
374
     * @return \loophp\collection\Contract\Collection
375 1
     */
376
    public function pluck($pluck, $default = null): BaseInterface
377 1
    {
378
        return $this->run(new Pluck($pluck, $default));
379
    }
380
381
    /**
382
     * {@inheritdoc}
383
     *
384
     * @return \loophp\collection\Contract\Collection
385 1
     */
386
    public function prepend(...$items): BaseInterface
387 1
    {
388
        return $this->run(new Prepend($items));
389
    }
390
391
    /**
392
     * {@inheritdoc}
393
     *
394
     * @return \loophp\collection\Contract\Collection
395 1
     */
396
    public function product(iterable ...$iterables): BaseInterface
397 1
    {
398
        return $this->run(new Product(...$iterables));
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     *
404
     * @return \loophp\collection\Contract\Collection
405 1
     */
406
    public static function range(int $start = 0, $end = INF, $step = 1): CollectionInterface
407 1
    {
408
        return (new Collection())->run(new Range($start, $end, $step));
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     *
414
     * @return \loophp\collection\Contract\Collection
415 2
     */
416
    public function rebase(): BaseInterface
417 2
    {
418
        return new Collection($this->transform(new All()));
419
    }
420
421
    /**
422
     * {@inheritdoc}
423
     */
424
    public function reduce(callable $callback, $initial = null)
425 2
    {
426
        return $this->transform(new Reduce($callback, $initial));
427 2
    }
428
429
    /**
430
     * {@inheritdoc}
431
     *
432
     * @return \loophp\collection\Contract\Collection
433 1
     */
434
    public function reduction(callable $callback, $initial = null): BaseInterface
435 1
    {
436
        return $this->run(new Reduction($callback, $initial));
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     *
442
     * @return \loophp\collection\Contract\Collection
443 1
     */
444
    public function reverse(): BaseInterface
445 1
    {
446
        return $this->run(new Reverse());
447
    }
448
449
    /**
450
     * {@inheritdoc}
451
     *
452
     * @return \loophp\collection\Contract\Collection
453 1
     */
454
    public function rsample($probability): BaseInterface
455 1
    {
456
        $callback = static function ($item) use ($probability): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

456
        $callback = static function (/** @scrutinizer ignore-unused */ $item) use ($probability): bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
457
            return (mt_rand() / mt_getrandmax()) < $probability;
458
        };
459
460
        return $this->run(new Filter($callback));
461
    }
462
463 1
    /**
464
     * {@inheritdoc}
465
     */
466 1
    public function scale(
467 1
        float $lowerBound,
468
        float $upperBound,
469 1
        ?float $wantedLowerBound = null,
470
        ?float $wantedUpperBound = null,
471
        ?float $base = null
472
    ): BaseInterface {
473
        return $this->run(new Scale($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base));
474
    }
475 1
476
    /**
477
     * {@inheritdoc}
478
     *
479
     * @return \loophp\collection\Contract\Collection
480
     */
481
    public function skip(int ...$counts): BaseInterface
482 1
    {
483
        return $this->run(new Skip(...$counts));
484
    }
485
486
    /**
487
     * {@inheritdoc}
488
     *
489
     * @return \loophp\collection\Contract\Collection
490 2
     */
491
    public function slice(int $offset, ?int $length = null): BaseInterface
492 2
    {
493
        return $this->run(new Slice($offset, $length));
494
    }
495
496
    /**
497
     * {@inheritdoc}
498
     *
499
     * @return \loophp\collection\Contract\Collection
500 1
     */
501
    public function sort(callable $callback): BaseInterface
502 1
    {
503
        return $this->run(new Sort($callback));
504
    }
505
506
    /**
507
     * {@inheritdoc}
508
     *
509
     * @return \loophp\collection\Contract\Collection
510 1
     */
511
    public function split(callable ...$callbacks): BaseInterface
512 1
    {
513
        return $this->run(new Split(...$callbacks));
514
    }
515
516
    /**
517
     * {@inheritdoc}
518
     *
519
     * @return \loophp\collection\Contract\Collection
520 1
     */
521
    public function tail(int $length = 1): BaseInterface
522 1
    {
523
        return $this->run(new Tail($length));
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     *
529
     * @return \loophp\collection\Contract\Collection
530 1
     */
531
    public static function times(int $number, ?callable $callback = null): CollectionInterface
532 1
    {
533
        return (new Collection())->run(new Times($number, $callback));
534
    }
535
536
    /**
537
     * {@inheritdoc}
538
     *
539
     * @return \loophp\collection\Contract\Collection
540 2
     */
541
    public function until(callable $callback): BaseInterface
542 2
    {
543 1
        return $this->run(new Until($callback));
544
    }
545
546 2
    /**
547
     * {@inheritdoc}
548 2
     *
549 2
     * @return \loophp\collection\Contract\Collection
550
     */
551 2
    public function walk(callable ...$callbacks): BaseInterface
552
    {
553
        return $this->run(new Walk(...$callbacks));
554 2
    }
555
556
    /**
557
     * @param array<mixed> $data
558
     * @param mixed ...$parameters
559
     *
560
     * @return \loophp\collection\Contract\Base<mixed>
561
     */
562 1
    public static function with($data = [], ...$parameters): BaseInterface
563
    {
564 1
        return new Collection($data, ...$parameters);
565
    }
566
567
    /**
568
     * {@inheritdoc}
569
     *
570
     * @return \loophp\collection\Contract\Collection
571
     */
572 3
    public function zip(...$items): BaseInterface
573
    {
574 3
        return $this->run(new Zip($items));
575
    }
576
}
577