Passed
Push — master ( 1940b1...c55a2e )
by Pol
03:56
created

Collection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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