Passed
Push — master ( f3dd8c...8b1d93 )
by Pol
11:27
created

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

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