Passed
Push — master ( 59b6fa...06c5f6 )
by Pol
04:02
created

Collection::cycle()   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 1
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 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\Rebase;
37
use loophp\collection\Operation\Reduction;
38
use loophp\collection\Operation\Reverse;
39
use loophp\collection\Operation\RSample;
40
use loophp\collection\Operation\Scale;
41
use loophp\collection\Operation\Skip;
42
use loophp\collection\Operation\Slice;
43
use loophp\collection\Operation\Sort;
44
use loophp\collection\Operation\Split;
45
use loophp\collection\Operation\Tail;
46
use loophp\collection\Operation\Times;
47
use loophp\collection\Operation\Until;
48
use loophp\collection\Operation\Walk;
49
use loophp\collection\Operation\Zip;
50
use loophp\collection\Transformation\All;
51
use loophp\collection\Transformation\Contains;
52
use loophp\collection\Transformation\Count;
53
use loophp\collection\Transformation\First;
54
use loophp\collection\Transformation\Get;
55
use loophp\collection\Transformation\Implode;
56
use loophp\collection\Transformation\Last;
57
use loophp\collection\Transformation\Reduce;
58
59
use const INF;
60
use const PHP_INT_MAX;
61
62
/**
63
 * Class Collection.
64
 */
65
final class Collection extends Base implements CollectionInterface
66
{
67
    /**
68
     * {@inheritdoc}
69
     */
70 10
    public function all(): array
71
    {
72 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...
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @return \loophp\collection\Contract\Collection
79
     */
80 1
    public function append(...$items): BaseInterface
81
    {
82 1
        return $this->run(new Append($items));
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @return \loophp\collection\Contract\Collection
89
     */
90 1
    public function apply(callable ...$callables): BaseInterface
91
    {
92 1
        return $this->run(new Apply(...$callables));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @return \loophp\collection\Contract\Collection
99
     */
100 2
    public function chunk(int $size): BaseInterface
101
    {
102 2
        return $this->run(new Chunk($size));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @return \loophp\collection\Contract\Collection
109
     */
110 1
    public function collapse(): BaseInterface
111
    {
112 1
        return $this->run(new Collapse());
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @return \loophp\collection\Contract\Collection
119
     */
120 1
    public function combinate(?int $size = null): BaseInterface
121
    {
122 1
        return $this->run(new Combinate($size));
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     *
128
     * @return \loophp\collection\Contract\Collection
129
     */
130 1
    public function combine($keys): BaseInterface
131
    {
132 1
        return $this->run(new Combine($keys));
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function contains($key): bool
139
    {
140 1
        return $this->transform(new Contains($key));
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 2
    public function count(): int
147
    {
148 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...
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     *
154
     * @return \loophp\collection\Contract\Collection
155
     */
156 1
    public function cycle(int $length = 0): BaseInterface
157
    {
158 1
        return $this->run(new Cycle($length));
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     *
164
     * @return \loophp\collection\Contract\Collection
165
     */
166 1
    public function distinct(): BaseInterface
167
    {
168 1
        return $this->run(new Distinct());
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     *
174
     * @return \loophp\collection\Contract\Collection
175
     */
176 1
    public static function empty(): CollectionInterface
177
    {
178 1
        return new self();
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     *
184
     * @return \loophp\collection\Contract\Collection
185
     */
186 1
    public function explode(string ...$strings): BaseInterface
187
    {
188 1
        return $this->run(new Explode(...$strings));
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     *
194
     * @return \loophp\collection\Contract\Collection
195
     */
196 2
    public function filter(callable ...$callbacks): BaseInterface
197
    {
198 2
        return $this->run(new Filter(...$callbacks));
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 1
    public function first(?callable $callback = null, $default = null)
205
    {
206 1
        return $this->transform(new First($callback, $default));
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     *
212
     * @return \loophp\collection\Contract\Collection
213
     */
214 1
    public function flatten(int $depth = PHP_INT_MAX): BaseInterface
215
    {
216 1
        return $this->run(new Flatten($depth));
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     *
222
     * @return \loophp\collection\Contract\Collection
223
     */
224 2
    public function flip(): BaseInterface
225
    {
226 2
        return $this->run(new Flip());
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     *
232
     * @return \loophp\collection\Contract\Collection
233
     */
234 1
    public function forget(...$keys): BaseInterface
235
    {
236 1
        return $this->run(new Forget($keys));
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242 1
    public function get($key, $default = null)
243
    {
244 1
        return $this->transform(new Get($key, $default));
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 1
    public function implode(string $glue = ''): string
251
    {
252 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...
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     *
258
     * @return \loophp\collection\Contract\Collection
259
     */
260 1
    public function intersperse($element, int $every = 1, int $startAt = 0): BaseInterface
261
    {
262 1
        return $this->run(new Intersperse($element, $every, $startAt));
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     *
268
     * @return \loophp\collection\Contract\Collection
269
     */
270 2
    public static function iterate(callable $callback, ...$parameters): CollectionInterface
271
    {
272 2
        return (new self())->run(new Iterate($callback, $parameters));
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     *
278
     * @return \loophp\collection\Contract\Collection
279
     */
280 1
    public function keys(): BaseInterface
281
    {
282 1
        return $this->run(new Keys());
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 1
    public function last()
289
    {
290 1
        return $this->transform(new Last());
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     *
296
     * @return \loophp\collection\Contract\Collection
297
     */
298 4
    public function limit(int $limit): BaseInterface
299
    {
300 4
        return $this->run(new Limit($limit));
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     *
306
     * @return \loophp\collection\Contract\Collection
307
     */
308 2
    public function map(callable ...$callbacks): BaseInterface
309
    {
310 2
        return $this->run(new Walk(...$callbacks), new Normalize());
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     *
316
     * @return \loophp\collection\Contract\Collection
317
     */
318 1
    public function merge(...$sources): BaseInterface
319
    {
320 1
        return $this->run(new Merge($sources));
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     *
326
     * @return \loophp\collection\Contract\Collection
327
     */
328 1
    public function normalize(): BaseInterface
329
    {
330 1
        return $this->run(new Normalize());
331
    }
332
333
    /**
334
     * {@inheritdoc}
335
     *
336
     * @return \loophp\collection\Contract\Collection
337
     */
338 1
    public function nth(int $step, int $offset = 0): BaseInterface
339
    {
340 1
        return $this->run(new Nth($step, $offset));
341
    }
342
343
    /**
344
     * {@inheritdoc}
345
     *
346
     * @return \loophp\collection\Contract\Collection
347
     */
348 1
    public function only(...$keys): BaseInterface
349
    {
350 1
        return $this->run(new Only($keys));
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     *
356
     * @return \loophp\collection\Contract\Collection
357
     */
358 1
    public function pad(int $size, $value): BaseInterface
359
    {
360 1
        return $this->run(new Pad($size, $value));
361
    }
362
363
    /**
364
     * {@inheritdoc}
365
     *
366
     * @return \loophp\collection\Contract\Collection
367
     */
368 1
    public function permutate(): BaseInterface
369
    {
370 1
        return $this->run(new Permutate());
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     *
376
     * @return \loophp\collection\Contract\Collection
377
     */
378 1
    public function pluck($pluck, $default = null): BaseInterface
379
    {
380 1
        return $this->run(new Pluck($pluck, $default));
381
    }
382
383
    /**
384
     * {@inheritdoc}
385
     *
386
     * @return \loophp\collection\Contract\Collection
387
     */
388 1
    public function prepend(...$items): BaseInterface
389
    {
390 1
        return $this->run(new Prepend($items));
391
    }
392
393
    /**
394
     * {@inheritdoc}
395
     *
396
     * @return \loophp\collection\Contract\Collection
397
     */
398 1
    public function product(iterable ...$iterables): BaseInterface
399
    {
400 1
        return $this->run(new Product(...$iterables));
401
    }
402
403
    /**
404
     * {@inheritdoc}
405
     *
406
     * @return \loophp\collection\Contract\Collection
407
     */
408 2
    public static function range(int $start = 0, $end = INF, $step = 1): CollectionInterface
409
    {
410 2
        return (new self())->run(new Range($start, $end, $step));
411
    }
412
413
    /**
414
     * {@inheritdoc}
415
     *
416
     * @return \loophp\collection\Contract\Collection
417
     */
418 1
    public function rebase(): BaseInterface
419
    {
420 1
        return $this->run(new Rebase());
421
    }
422
423
    /**
424
     * {@inheritdoc}
425
     */
426 1
    public function reduce(callable $callback, $initial = null)
427
    {
428 1
        return $this->transform(new Reduce($callback, $initial));
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     *
434
     * @return \loophp\collection\Contract\Collection
435
     */
436 1
    public function reduction(callable $callback, $initial = null): BaseInterface
437
    {
438 1
        return $this->run(new Reduction($callback, $initial));
439
    }
440
441
    /**
442
     * {@inheritdoc}
443
     *
444
     * @return \loophp\collection\Contract\Collection
445
     */
446 1
    public function reverse(): BaseInterface
447
    {
448 1
        return $this->run(new Reverse());
449
    }
450
451
    /**
452
     * {@inheritdoc}
453
     *
454
     * @return \loophp\collection\Contract\Collection
455
     */
456 1
    public function rsample($probability): BaseInterface
457
    {
458 1
        return $this->run(new RSample($probability));
459
    }
460
461
    /**
462
     * {@inheritdoc}
463
     *
464
     * @return \loophp\collection\Contract\Collection
465
     */
466 1
    public function scale(
467
        float $lowerBound,
468
        float $upperBound,
469
        ?float $wantedLowerBound = null,
470
        ?float $wantedUpperBound = null,
471
        ?float $base = null
472
    ): BaseInterface {
473 1
        return $this->run(new Scale($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base));
474
    }
475
476
    /**
477
     * {@inheritdoc}
478
     *
479
     * @return \loophp\collection\Contract\Collection
480
     */
481 2
    public function skip(int ...$counts): BaseInterface
482
    {
483 2
        return $this->run(new Skip(...$counts));
484
    }
485
486
    /**
487
     * {@inheritdoc}
488
     *
489
     * @return \loophp\collection\Contract\Collection
490
     */
491 1
    public function slice(int $offset, ?int $length = null): BaseInterface
492
    {
493 1
        return $this->run(new Slice($offset, $length));
494
    }
495
496
    /**
497
     * {@inheritdoc}
498
     *
499
     * @return \loophp\collection\Contract\Collection
500
     */
501 1
    public function sort(?callable $callback = null): BaseInterface
502
    {
503 1
        return $this->run(new Sort($callback));
504
    }
505
506
    /**
507
     * {@inheritdoc}
508
     *
509
     * @return \loophp\collection\Contract\Collection
510
     */
511 1
    public function split(callable ...$callbacks): BaseInterface
512
    {
513 1
        return $this->run(new Split(...$callbacks));
514
    }
515
516
    /**
517
     * {@inheritdoc}
518
     *
519
     * @return \loophp\collection\Contract\Collection
520
     */
521 1
    public function tail(int $length = 1): BaseInterface
522
    {
523 1
        return $this->run(new Tail($length));
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     *
529
     * @return \loophp\collection\Contract\Collection
530
     */
531 2
    public static function times($number = INF, ?callable $callback = null): CollectionInterface
532
    {
533 2
        return (new self())->run(new Times($number, $callback));
534
    }
535
536
    /**
537
     * {@inheritdoc}
538
     *
539
     * @return \loophp\collection\Contract\Collection
540
     */
541 1
    public function until(callable $callback): BaseInterface
542
    {
543 1
        return $this->run(new Until($callback));
544
    }
545
546
    /**
547
     * {@inheritdoc}
548
     *
549
     * @return \loophp\collection\Contract\Collection
550
     */
551 3
    public function walk(callable ...$callbacks): BaseInterface
552
    {
553 3
        return $this->run(new Walk(...$callbacks));
554
    }
555
556
    /**
557
     * @param mixed $data
558
     * @param mixed ...$parameters
559
     *
560
     * @return \loophp\collection\Contract\Collection<mixed>
561
     */
562 52
    public static function with($data = [], ...$parameters): CollectionInterface
563
    {
564 52
        return new self($data, ...$parameters);
565
    }
566
567
    /**
568
     * {@inheritdoc}
569
     *
570
     * @return \loophp\collection\Contract\Collection
571
     */
572 1
    public function zip(...$items): BaseInterface
573
    {
574 1
        return $this->run(new Zip($items));
575
    }
576
}
577