Collection   F
last analyzed

Complexity

Total Complexity 137

Size/Duplication

Total Lines 854
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 4 Features 0
Metric Value
eloc 168
dl 0
loc 854
ccs 306
cts 306
cp 1
rs 2
c 9
b 4
f 0
wmc 137

136 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 3 1
A getIterator() 0 3 1
A nth() 0 3 1
A normalize() 0 3 1
A intersperse() 0 3 1
A groupBy() 0 3 1
A intersectKeys() 0 3 1
A wrap() 0 3 1
A unpair() 0 3 1
A apply() 0 3 1
A all() 0 3 1
A __construct() 0 3 1
A append() 0 3 1
A cache() 0 3 1
A compare() 0 3 1
A averages() 0 3 1
A compact() 0 3 1
A chunk() 0 3 1
A combinate() 0 3 1
A coalesce() 0 3 1
A combine() 0 3 1
A count() 0 3 1
A collapse() 0 3 1
A column() 0 3 1
A asyncMap() 0 3 1
A asyncMapN() 0 3 1
A associate() 0 7 1
A cycle() 0 3 1
A diff() 0 3 1
A diffKeys() 0 3 1
A current() 0 3 1
A countIn() 0 9 1
A dispersion() 0 3 1
A distinct() 0 23 1
A dropWhile() 0 3 1
A drop() 0 3 1
A dump() 0 3 1
A intersect() 0 3 1
A isNotEmpty() 0 3 1
A span() 0 10 1
A equals() 0 3 1
A flatten() 0 3 1
A entropy() 0 3 1
A merge() 0 3 1
A map() 0 3 1
A takeWhile() 0 3 1
A scanLeft1() 0 3 1
A init() 0 3 1
A min() 0 3 1
A tail() 0 3 1
A strict() 0 3 1
A squash() 0 3 1
A scanRight() 0 3 1
A fromGenerator() 0 3 1
A fromIterable() 0 3 1
A falsy() 0 3 1
A partition() 0 10 1
A pack() 0 3 1
A matching() 0 3 1
A tails() 0 3 1
A keys() 0 3 1
A pipe() 0 3 1
A shuffle() 0 3 1
A lines() 0 3 1
A reverse() 0 3 1
A foldRight1() 0 3 1
A random() 0 3 1
A same() 0 17 2
A permutate() 0 3 1
A last() 0 3 1
A jsonSerialize() 0 3 1
A limit() 0 3 1
A reduce() 0 3 1
A fromResource() 0 3 1
A split() 0 3 1
A key() 0 3 1
A frequency() 0 3 1
A ifThenElse() 0 11 1
A find() 0 3 1
A foldLeft1() 0 3 1
A max() 0 3 1
A forget() 0 3 1
A implode() 0 3 1
A flatMap() 0 3 1
A scale() 0 8 1
A foldLeft() 0 3 1
A product() 0 3 1
A match() 0 3 1
A reject() 0 3 1
A prepend() 0 3 1
A has() 0 3 1
A every() 0 4 1
A head() 0 3 1
A explode() 0 3 1
A nullsy() 0 3 1
A get() 0 3 1
A slice() 0 3 1
A isEmpty() 0 3 1
A reduction() 0 3 1
A inits() 0 3 1
A scanLeft() 0 3 1
A mapN() 0 3 1
A flip() 0 3 1
A fromString() 0 3 1
A first() 0 3 1
A foldRight() 0 3 1
A duplicate() 0 23 1
A range() 0 3 1
A rsample() 0 3 1
A empty() 0 3 1
A since() 0 3 1
A filter() 0 3 1
A scanRight1() 0 3 1
A group() 0 3 1
A pair() 0 3 1
A pluck() 0 3 1
A sort() 0 3 1
A pad() 0 3 1
A fromCallable() 0 3 1
A fromFile() 0 4 1
A unpack() 0 3 1
A unwords() 0 3 1
A transpose() 0 3 1
A unwindow() 0 3 1
A when() 0 11 1
A times() 0 3 1
A truthy() 0 3 1
A window() 0 3 1
A words() 0 3 1
A unzip() 0 3 1
A unwrap() 0 3 1
A unlines() 0 3 1
A unfold() 0 3 1
A until() 0 3 1
A zip() 0 3 1
A tap() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection;
6
7
use Closure;
8
use Countable;
9
use Doctrine\Common\Collections\Criteria;
10
use Generator;
11
use JsonSerializable;
12
use loophp\collection\Contract\Collection as CollectionInterface;
13
use loophp\collection\Contract\Operation as OperationInterface;
14
use loophp\collection\Utils\CallbacksArrayReducer;
15
use loophp\iterators\ClosureIteratorAggregate;
16
use loophp\iterators\IterableIteratorAggregate;
17
use loophp\iterators\ResourceIteratorAggregate;
18
use loophp\iterators\StringIteratorAggregate;
19
use NoRewindIterator;
20
use Psr\Cache\CacheItemPoolInterface;
21
use Symfony\Component\Cache\Adapter\ArrayAdapter;
22
use Traversable;
23
24
use const INF;
25
use const PHP_INT_MAX;
26
27
/**
28
 * @immutable
29
 *
30
 * @template TKey
31
 * @template T
32
 *
33
 * @implements \loophp\collection\Contract\Collection<TKey, T>
34
 */
35
final class Collection implements CollectionInterface, JsonSerializable, Countable
36
{
37
    /**
38
     * @var ClosureIteratorAggregate<TKey, T>
39
     */
40
    private ClosureIteratorAggregate $innerIterator;
41
42
    /**
43
     * @param callable(mixed ...$parameters): iterable<TKey, T> $callable
44
     * @param iterable<int, mixed> $parameters
45
     */
46 724
    private function __construct(callable $callable, iterable $parameters = [])
47
    {
48 724
        $this->innerIterator = new ClosureIteratorAggregate($callable, $parameters);
49
    }
50
51 25
    public function all(bool $normalize = true): array
52
    {
53 25
        return iterator_to_array($this, !$normalize);
54
    }
55
56 2
    public function append(mixed ...$items): CollectionInterface
57
    {
58 2
        return new self((new Operation\Append())()($items), [$this]);
59
    }
60
61 5
    public function apply(callable ...$callbacks): CollectionInterface
62
    {
63 5
        return new self((new Operation\Apply())()(...$callbacks), [$this]);
64
    }
65
66 4
    public function associate(
67
        ?callable $callbackForKeys = null,
68
        ?callable $callbackForValues = null
69
    ): CollectionInterface {
70 4
        $defaultCallback = static fn (mixed $carry): mixed => $carry;
71
72 4
        return new self((new Operation\Associate())()($callbackForKeys ?? $defaultCallback)($callbackForValues ?? $defaultCallback), [$this]);
73
    }
74
75 2
    public function asyncMap(callable $callback): CollectionInterface
76
    {
77 2
        return new self((new Operation\AsyncMap())()($callback), [$this]);
78
    }
79
80 2
    public function asyncMapN(callable ...$callbacks): CollectionInterface
81
    {
82 2
        return new self((new Operation\AsyncMapN())()(...$callbacks), [$this]);
83
    }
84
85 4
    public function averages(): CollectionInterface
86
    {
87 4
        return new self((new Operation\Averages())(), [$this]);
88
    }
89
90 3
    public function cache(?CacheItemPoolInterface $cache = null): CollectionInterface
91
    {
92 3
        return new self((new Operation\Cache())()($cache ?? new ArrayAdapter()), [$this]);
93
    }
94
95 8
    public function chunk(int ...$sizes): CollectionInterface
96
    {
97 8
        return new self((new Operation\Chunk())()(...$sizes), [$this]);
98
    }
99
100 4
    public function coalesce(): CollectionInterface
101
    {
102 4
        return new self((new Operation\Coalesce())(), [$this]);
103
    }
104
105 4
    public function collapse(): CollectionInterface
106
    {
107 4
        return new self((new Operation\Collapse())(), [$this]);
108
    }
109
110 6
    public function column(mixed $column): CollectionInterface
111
    {
112 6
        return new self((new Operation\Column())()($column), [$this]);
113
    }
114
115 6
    public function combinate(?int $length = null): CollectionInterface
116
    {
117 6
        return new self((new Operation\Combinate())()($length), [$this]);
118
    }
119
120 6
    public function combine(mixed ...$keys): CollectionInterface
121
    {
122 6
        return new self((new Operation\Combine())()($keys), [$this]);
123
    }
124
125 4
    public function compact(mixed ...$values): CollectionInterface
126
    {
127 4
        return new self((new Operation\Compact())()($values), [$this]);
128
    }
129
130 10
    public function compare(callable $comparator, $default = null)
131
    {
132 10
        return (new self((new Operation\Compare())()($comparator), [$this]))->current(0, $default);
133
    }
134
135 10
    public function contains(mixed ...$values): bool
136
    {
137 10
        return (new Operation\Contains())()($values)($this)->current();
138
    }
139
140 8
    public function count(): int
141
    {
142 8
        return iterator_count($this);
143
    }
144
145 2
    public function countIn(int &$counter): CollectionInterface
146
    {
147 2
        return new self(
148 2
            (new Operation\Apply())()(
149 2
                static function () use (&$counter): void {
150 1
                    ++$counter;
151 2
                }
152 2
            ),
153 2
            [$this]
154 2
        );
155
    }
156
157 100
    public function current(int $index = 0, $default = null)
158
    {
159 100
        return (new Operation\Current())()($index)($default)($this)->current();
160
    }
161
162 4
    public function cycle(): CollectionInterface
163
    {
164 4
        return new self((new Operation\Cycle())(), [$this]);
165
    }
166
167 14
    public function diff(mixed ...$values): CollectionInterface
168
    {
169 14
        return new self((new Operation\Diff())()($values), [$this]);
170
    }
171
172 4
    public function diffKeys(mixed ...$keys): CollectionInterface
173
    {
174 4
        return new self((new Operation\DiffKeys())()($keys), [$this]);
175
    }
176
177 4
    public function dispersion(): CollectionInterface
178
    {
179 4
        return new self((new Operation\Dispersion())(), [$this]);
180
    }
181
182 12
    public function distinct(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface
183
    {
184 12
        $accessorCallback ??=
185
            /**
186
             * @param T $value
0 ignored issues
show
Bug introduced by
The type loophp\collection\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
187
             *
188
             * @return T
189
             */
190 12
            static fn (mixed $value): mixed => $value;
191
192 12
        $comparatorCallback ??=
193
            /**
194
             * @param T $left
195
             *
196
             * @return Closure(T): bool
197
             */
198 12
            static fn (mixed $left): Closure =>
199
                /**
200
                 * @param T $right
201
                 */
202 8
                static fn (mixed $right): bool => $left === $right;
203
204 12
        return new self((new Operation\Distinct())()($comparatorCallback)($accessorCallback), [$this]);
205
    }
206
207 4
    public function drop(int $count): CollectionInterface
208
    {
209 4
        return new self((new Operation\Drop())()($count), [$this]);
210
    }
211
212 4
    public function dropWhile(callable ...$callbacks): CollectionInterface
213
    {
214 4
        return new self((new Operation\DropWhile())()(...$callbacks), [$this]);
215
    }
216
217 4
    public function dump(string $name = '', int $size = 1, ?Closure $closure = null): CollectionInterface
218
    {
219 4
        return new self((new Operation\Dump())()($name)($size)($closure), [$this]);
220
    }
221
222 10
    public function duplicate(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface
223
    {
224 10
        $accessorCallback ??=
225
            /**
226
             * @param T $value
227
             *
228
             * @return T
229
             */
230 10
            static fn (mixed $value): mixed => $value;
231
232 10
        $comparatorCallback ??=
233
            /**
234
             * @param T $left
235
             *
236
             * @return Closure(T): bool
237
             */
238 10
            static fn (mixed $left): Closure =>
239
                /**
240
                 * @param T $right
241
                 */
242 6
                static fn (mixed $right): bool => $left === $right;
243
244 10
        return new self((new Operation\Duplicate())()($comparatorCallback)($accessorCallback), [$this]);
245
    }
246
247
    /**
248
     * @template UKey
249
     * @template U
250
     *
251
     * @return CollectionInterface<UKey, U>&self<UKey, U>
252
     */
253 4
    public static function empty(): CollectionInterface
254
    {
255 4
        return self::fromIterable([]);
256
    }
257
258 4
    public function entropy(): CollectionInterface
259
    {
260 4
        return new self((new Operation\Entropy())(), [$this]);
261
    }
262
263 36
    public function equals(iterable $other): bool
264
    {
265 36
        return (new Operation\Equals())()($other)($this)->current();
266
    }
267
268 16
    public function every(callable ...$callbacks): bool
269
    {
270 16
        return (new Operation\Every())()(static fn (int $index, mixed $value, mixed $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))($this)
271 16
            ->current();
272
    }
273
274 2
    public function explode(mixed ...$explodes): CollectionInterface
275
    {
276 2
        return new self((new Operation\Explode())()($explodes), [$this]);
277
    }
278
279 8
    public function falsy(): bool
280
    {
281 8
        return (new Operation\Falsy())()($this)->current();
282
    }
283
284 8
    public function filter(callable ...$callbacks): CollectionInterface
285
    {
286 8
        return new self((new Operation\Filter())()(...$callbacks), [$this]);
287
    }
288
289 10
    public function find(mixed $default = null, callable ...$callbacks)
290
    {
291 10
        return (new Operation\Find())()($default)(...$callbacks)($this)->current();
292
    }
293
294 10
    public function first(mixed $default = null)
295
    {
296 10
        return (new self((new Operation\First())(), [$this]))->current(0, $default);
297
    }
298
299 14
    public function flatMap(callable $callback): CollectionInterface
300
    {
301 14
        return new self((new Operation\FlatMap())()($callback), [$this]);
302
    }
303
304 6
    public function flatten(int $depth = PHP_INT_MAX): CollectionInterface
305
    {
306 6
        return new self((new Operation\Flatten())()($depth), [$this]);
307
    }
308
309 4
    public function flip(): CollectionInterface
310
    {
311 4
        return new self((new Operation\Flip())(), [$this]);
312
    }
313
314 8
    public function foldLeft(callable $callback, mixed $initial)
315
    {
316 8
        return (new self((new Operation\FoldLeft())()($callback)($initial), [$this]))->current();
317
    }
318
319 4
    public function foldLeft1(callable $callback): mixed
320
    {
321 4
        return (new self((new Operation\FoldLeft1())()($callback), [$this]))->current();
322
    }
323
324 2
    public function foldRight(callable $callback, mixed $initial): mixed
325
    {
326 2
        return (new self((new Operation\FoldRight())()($callback)($initial), [$this]))->current();
327
    }
328
329 4
    public function foldRight1(callable $callback): mixed
330
    {
331 4
        return (new self((new Operation\FoldRight1())()($callback), [$this]))->current();
332
    }
333
334 2
    public function forget(mixed ...$keys): CollectionInterface
335
    {
336 2
        return new self((new Operation\Forget())()($keys), [$this]);
337
    }
338
339 5
    public function frequency(): CollectionInterface
340
    {
341 5
        return new self((new Operation\Frequency())(), [$this]);
342
    }
343
344
    /**
345
     * @template NewTKey
346
     * @template NewT
347
     *
348
     * @param callable(mixed ...$parameters): iterable<NewTKey, NewT> $callable
349
     * @param iterable<int, mixed> $parameters
350
     *
351
     * @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT>
352
     */
353 3
    public static function fromCallable(callable $callable, iterable $parameters = []): CollectionInterface
354
    {
355 3
        return new self($callable, $parameters);
356
    }
357
358
    /**
359
     * @param Closure(resource): mixed $consumer
360
     *
361
     * @return CollectionInterface<int, string|mixed>&self<int, string|mixed>
362
     */
363 2
    public static function fromFile(string $filepath, ?Closure $consumer = null): CollectionInterface
364
    {
365 2
        return new self(
366 2
            static fn (): Generator => yield from new ResourceIteratorAggregate(fopen($filepath, 'rb'), true, $consumer),
367 2
        );
368
    }
369
370
    /**
371
     * @template NewTKey
372
     * @template NewT
373
     *
374
     * @param Generator<NewTKey, NewT> $generator
375
     *
376
     * @return CollectionInterface<NewTKey, NewT>&self<NewTKey, NewT>
377
     */
378 2
    public static function fromGenerator(Generator $generator): CollectionInterface
379
    {
380 2
        return new self(static fn (): Generator => yield from new NoRewindIterator($generator));
381
    }
382
383
    /**
384
     * @template UKey
385
     * @template U
386
     *
387
     * @param iterable<UKey, U> $iterable
388
     *
389
     * @return CollectionInterface<UKey, U>&self<UKey, U>
390
     */
391 707
    public static function fromIterable(iterable $iterable): CollectionInterface
392
    {
393 707
        return new self(static fn (): Generator => yield from new IterableIteratorAggregate($iterable));
394
    }
395
396
    /**
397
     * @param resource $resource
398
     *
399
     * @return CollectionInterface<int, string>&self<int, string>
400
     */
401 2
    public static function fromResource($resource): CollectionInterface
402
    {
403 2
        return new self(static fn (): Generator => yield from new ResourceIteratorAggregate($resource));
404
    }
405
406
    /**
407
     * @return CollectionInterface<int, string>&self<int, string>
408
     */
409 2
    public static function fromString(string $string, string $delimiter = ''): CollectionInterface
410
    {
411 2
        return new self(static fn (): Generator => yield from new StringIteratorAggregate($string, $delimiter));
412
    }
413
414 5
    public function get(mixed $key, mixed $default = null)
415
    {
416 5
        return (new self((new Operation\Get())()($key)($default), [$this]))->current(0, $default);
417
    }
418
419
    /**
420
     * @return Traversable<TKey, T>
421
     */
422 722
    public function getIterator(): Traversable
423
    {
424 722
        yield from $this->innerIterator->getIterator();
425
    }
426
427 6
    public function group(): CollectionInterface
428
    {
429 6
        return new self((new Operation\Group())(), [$this]);
430
    }
431
432 4
    public function groupBy(callable $callback): CollectionInterface
433
    {
434 4
        return new self((new Operation\GroupBy())()($callback), [$this]);
435
    }
436
437 16
    public function has(callable ...$callbacks): bool
438
    {
439 16
        return (new Operation\Has())()(...$callbacks)($this)->current();
440
    }
441
442 6
    public function head(mixed $default = null)
443
    {
444 6
        return (new self((new Operation\Head())(), [$this]))->current(0, $default);
445
    }
446
447 4
    public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface
448
    {
449 4
        $identity =
450
            /**
451
             * @param T $value
452
             *
453
             * @return T
454
             */
455 4
            static fn (mixed $value): mixed => $value;
456
457 4
        return new self((new Operation\IfThenElse())()($condition)($then)($else ?? $identity), [$this]);
458
    }
459
460 6
    public function implode(string $glue = ''): string
461
    {
462 6
        return (new self((new Operation\Implode())()($glue), [$this]))->current(0, '');
463
    }
464
465 4
    public function init(): CollectionInterface
466
    {
467 4
        return new self((new Operation\Init())(), [$this]);
468
    }
469
470 4
    public function inits(): CollectionInterface
471
    {
472 4
        return new self((new Operation\Inits())(), [$this]);
473
    }
474
475 4
    public function intersect(mixed ...$values): CollectionInterface
476
    {
477 4
        return new self((new Operation\Intersect())()($values), [$this]);
478
    }
479
480 6
    public function intersectKeys(mixed ...$keys): CollectionInterface
481
    {
482 6
        return new self((new Operation\IntersectKeys())()($keys), [$this]);
483
    }
484
485 10
    public function intersperse(mixed $element, int $every = 1, int $startAt = 0): CollectionInterface
486
    {
487 10
        return new self((new Operation\Intersperse())()($element)($every)($startAt), [$this]);
488
    }
489
490 14
    public function isEmpty(): bool
491
    {
492 14
        return (new Operation\IsEmpty())()($this)->current();
493
    }
494
495 14
    public function isNotEmpty(): bool
496
    {
497 14
        return (new Operation\IsNotEmpty())()($this)->current();
498
    }
499
500
    /**
501
     * @return array<mixed>
502
     */
503 1
    public function jsonSerialize(): array
504
    {
505 1
        return $this->all(false);
506
    }
507
508 6
    public function key(int $index = 0)
509
    {
510 6
        return (new Operation\Key())()($index)($this)->current();
511
    }
512
513 2
    public function keys(): CollectionInterface
514
    {
515 2
        return new self((new Operation\Keys())(), [$this]);
516
    }
517
518 14
    public function last(mixed $default = null)
519
    {
520 14
        return (new self((new Operation\Last())(), [$this]))->current(0, $default);
521
    }
522
523 15
    public function limit(int $count = -1, int $offset = 0): CollectionInterface
524
    {
525 15
        return new self((new Operation\Limit())()($count)($offset), [$this]);
526
    }
527
528 2
    public function lines(): CollectionInterface
529
    {
530 2
        return new self((new Operation\Lines())(), [$this]);
531
    }
532
533 16
    public function map(callable $callback): CollectionInterface
534
    {
535 16
        return new self((new Operation\Map())()($callback), [$this]);
536
    }
537
538 4
    public function mapN(callable ...$callbacks): CollectionInterface
539
    {
540 4
        return new self((new Operation\MapN())()(...$callbacks), [$this]);
541
    }
542
543 6
    public function match(callable $callback, ?callable $matcher = null): bool
544
    {
545 6
        return (new Operation\MatchOne())()($matcher ?? static fn (): bool => true)($callback)($this)->current();
546
    }
547
548 2
    public function matching(Criteria $criteria): CollectionInterface
549
    {
550 2
        return new self((new Operation\Matching())()($criteria), [$this]);
551
    }
552
553 8
    public function max(mixed $default = null)
554
    {
555 8
        return (new self((new Operation\Max())(), [$this]))->current(0, $default);
556
    }
557
558 2
    public function merge(iterable ...$sources): CollectionInterface
559
    {
560 2
        return new self((new Operation\Merge())()(...$sources), [$this]);
561
    }
562
563 8
    public function min(mixed $default = null)
564
    {
565 8
        return (new self((new Operation\Min())(), [$this]))->current(0, $default);
566
    }
567
568 9
    public function normalize(): CollectionInterface
569
    {
570 9
        return new self((new Operation\Normalize())(), [$this]);
571
    }
572
573 4
    public function nth(int $step, int $offset = 0): CollectionInterface
574
    {
575 4
        return new self((new Operation\Nth())()($step)($offset), [$this]);
576
    }
577
578 8
    public function nullsy(): bool
579
    {
580 8
        return (new Operation\Nullsy())()($this)->current();
581
    }
582
583 9
    public function pack(): CollectionInterface
584
    {
585 9
        return new self((new Operation\Pack())(), [$this]);
586
    }
587
588 2
    public function pad(int $size, mixed $value): CollectionInterface
589
    {
590 2
        return new self((new Operation\Pad())()($size)($value), [$this]);
591
    }
592
593 6
    public function pair(): CollectionInterface
594
    {
595 6
        return new self((new Operation\Pair())(), [$this]);
596
    }
597
598 2
    public function partition(callable ...$callbacks): CollectionInterface
599
    {
600 2
        return (new self((new Operation\Partition())()(...$callbacks), [$this]))
601 2
            ->map(
602
                /**
603
                 * @param iterable<TKey, T> $iterable
604
                 *
605
                 * @return Collection<TKey, T>
606
                 */
607 2
                static fn (iterable $iterable): Collection => Collection::fromIterable($iterable)
608 2
            );
609
    }
610
611 2
    public function permutate(): CollectionInterface
612
    {
613 2
        return new self((new Operation\Permutate())(), [$this]);
614
    }
615
616 2
    public function pipe(callable ...$callbacks): CollectionInterface
617
    {
618 2
        return new self((new Operation\Pipe())()(...$callbacks), [$this]);
619
    }
620
621 12
    public function pluck(mixed $pluck, mixed $default = null): CollectionInterface
622
    {
623 12
        return new self((new Operation\Pluck())()($pluck)($default), [$this]);
624
    }
625
626 2
    public function prepend(mixed ...$items): CollectionInterface
627
    {
628 2
        return new self((new Operation\Prepend())()($items), [$this]);
629
    }
630
631 4
    public function product(iterable ...$iterables): CollectionInterface
632
    {
633 4
        return new self((new Operation\Product())()(...$iterables), [$this]);
634
    }
635
636 2
    public function random(int $size = 1, ?int $seed = null): CollectionInterface
637
    {
638 2
        return new self((new Operation\Random())()($seed ?? random_int(0, 1000))($size), [$this]);
639
    }
640
641
    /**
642
     * @return self<int, float>
643
     */
644 2
    public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): CollectionInterface
645
    {
646 2
        return new self((new Operation\Range())()($start)($end)($step));
647
    }
648
649 9
    public function reduce(callable $callback, mixed $initial = null)
650
    {
651 9
        return (new self((new Operation\Reduce())()($callback)($initial), [$this]))->current();
652
    }
653
654 2
    public function reduction(callable $callback, mixed $initial = null): CollectionInterface
655
    {
656 2
        return new self((new Operation\Reduction())()($callback)($initial), [$this]);
657
    }
658
659 8
    public function reject(callable ...$callbacks): CollectionInterface
660
    {
661 8
        return new self((new Operation\Reject())()(...$callbacks), [$this]);
662
    }
663
664 4
    public function reverse(): CollectionInterface
665
    {
666 4
        return new self((new Operation\Reverse())(), [$this]);
667
    }
668
669 2
    public function rsample(float $probability): CollectionInterface
670
    {
671 2
        return new self((new Operation\RSample())()($probability), [$this]);
672
    }
673
674 46
    public function same(iterable $other, ?callable $comparatorCallback = null): bool
675
    {
676 46
        $comparatorCallback ??=
677
            /**
678
             * @param T $leftValue
679
             * @param TKey $leftKey
680
             *
681
             * @return Closure(T, TKey): bool
682
             */
683 46
            static fn (mixed $leftValue, mixed $leftKey): Closure =>
684
                /**
685
                 * @param T $rightValue
686
                 * @param TKey $rightKey
0 ignored issues
show
Bug introduced by
The type loophp\collection\TKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
687
                 */
688 32
                static fn (mixed $rightValue, mixed $rightKey): bool => $leftValue === $rightValue && $leftKey === $rightKey;
689
690 46
        return (new Operation\Same())()($other)($comparatorCallback)($this)->current();
691
    }
692
693 2
    public function scale(
694
        float $lowerBound,
695
        float $upperBound,
696
        float $wantedLowerBound = 0.0,
697
        float $wantedUpperBound = 1.0,
698
        float $base = 0.0
699
    ): CollectionInterface {
700 2
        return new self((new Operation\Scale())()($lowerBound)($upperBound)($wantedLowerBound)($wantedUpperBound)($base), [$this]);
701
    }
702
703 4
    public function scanLeft(callable $callback, mixed $initial): CollectionInterface
704
    {
705 4
        return new self((new Operation\ScanLeft())()($callback)($initial), [$this]);
706
    }
707
708 6
    public function scanLeft1(callable $callback): CollectionInterface
709
    {
710 6
        return new self((new Operation\ScanLeft1())()($callback), [$this]);
711
    }
712
713 4
    public function scanRight(callable $callback, mixed $initial): CollectionInterface
714
    {
715 4
        return new self((new Operation\ScanRight())()($callback)($initial), [$this]);
716
    }
717
718 4
    public function scanRight1(callable $callback): CollectionInterface
719
    {
720 4
        return new self((new Operation\ScanRight1())()($callback), [$this]);
721
    }
722
723 2
    public function shuffle(?int $seed = null): CollectionInterface
724
    {
725 2
        return new self((new Operation\Shuffle())()($seed ?? random_int(0, 1000)), [$this]);
726
    }
727
728 8
    public function since(callable ...$callbacks): CollectionInterface
729
    {
730 8
        return new self((new Operation\Since())()(...$callbacks), [$this]);
731
    }
732
733 6
    public function slice(int $offset, int $length = -1): CollectionInterface
734
    {
735 6
        return new self((new Operation\Slice())()($offset)($length), [$this]);
736
    }
737
738 14
    public function sort(int $type = OperationInterface\Sortable::BY_VALUES, null|callable|Closure $callback = null): CollectionInterface
739
    {
740 14
        return new self((new Operation\Sort())()($type)($callback), [$this]);
741
    }
742
743 2
    public function span(callable ...$callbacks): CollectionInterface
744
    {
745 2
        return (new self((new Operation\Span())()(...$callbacks), [$this]))
746 2
            ->map(
747
                /**
748
                 * @param iterable<TKey, T> $iterable
749
                 *
750
                 * @return Collection<TKey, T>
751
                 */
752 2
                static fn (iterable $iterable): Collection => Collection::fromIterable($iterable)
753 2
            );
754
    }
755
756 6
    public function split(int $type = OperationInterface\Splitable::BEFORE, callable ...$callbacks): CollectionInterface
757
    {
758 6
        return new self((new Operation\Split())()($type)(...$callbacks), [$this]);
759
    }
760
761 7
    public function squash(): CollectionInterface
762
    {
763 7
        return self::fromIterable($this->pack()->all(false))->unpack();
764
    }
765
766 4
    public function strict(?callable $callback = null): CollectionInterface
767
    {
768 4
        return new self((new Operation\Strict())()($callback), [$this]);
769
    }
770
771 2
    public function tail(): CollectionInterface
772
    {
773 2
        return new self((new Operation\Tail())(), [$this]);
774
    }
775
776 4
    public function tails(): CollectionInterface
777
    {
778 4
        return new self((new Operation\Tails())(), [$this]);
779
    }
780
781 6
    public function takeWhile(callable ...$callbacks): CollectionInterface
782
    {
783 6
        return new self((new Operation\TakeWhile())()(...$callbacks), [$this]);
784
    }
785
786 2
    public function tap(callable ...$callbacks): CollectionInterface
787
    {
788 2
        return new self((new Operation\Tap())()(...$callbacks), [$this]);
789
    }
790
791
    /**
792
     * @template U
793
     *
794
     * @param callable(int): U $callback
795
     *
796
     * @return self<int, U>
797
     */
798 2
    public static function times(int $number = 0, ?callable $callback = null): CollectionInterface
799
    {
800 2
        return new self((new Operation\Times())()($number)($callback));
801
    }
802
803 2
    public function transpose(): CollectionInterface
804
    {
805 2
        return new self((new Operation\Transpose())(), [$this]);
806
    }
807
808 8
    public function truthy(): bool
809
    {
810 8
        return (new Operation\Truthy())()($this)->current();
811
    }
812
813 2
    public static function unfold(callable $callback, iterable $parameters = []): CollectionInterface
814
    {
815 2
        return new self((new Operation\Unfold())()($parameters)($callback));
816
    }
817
818 2
    public function unlines(): string
819
    {
820 2
        return (new self((new Operation\Unlines())(), [$this]))->current(0, '');
821
    }
822
823 7
    public function unpack(): CollectionInterface
824
    {
825 7
        return new self((new Operation\Unpack())(), [$this]);
826
    }
827
828 2
    public function unpair(): CollectionInterface
829
    {
830 2
        return new self((new Operation\Unpair())(), [$this]);
831
    }
832
833 4
    public function until(callable ...$callbacks): CollectionInterface
834
    {
835 4
        return new self((new Operation\Until())()(...$callbacks), [$this]);
836
    }
837
838 2
    public function unwindow(): CollectionInterface
839
    {
840 2
        return new self((new Operation\Unwindow())(), [$this]);
841
    }
842
843 2
    public function unwords(): string
844
    {
845 2
        return (new self((new Operation\Unwords())(), [$this]))->current(0, '');
846
    }
847
848 6
    public function unwrap(): CollectionInterface
849
    {
850 6
        return new self((new Operation\Unwrap())(), [$this]);
851
    }
852
853 2
    public function unzip(): CollectionInterface
854
    {
855 2
        return new self((new Operation\Unzip())(), [$this]);
856
    }
857
858 4
    public function when(callable $predicate, callable $whenTrue, ?callable $whenFalse = null): CollectionInterface
859
    {
860 4
        $whenFalse ??=
861
            /**
862
             * @param iterable<TKey, T> $iterable
863
             *
864
             * @return iterable<TKey, T>
865
             */
866 4
            static fn (iterable $iterable): iterable => $iterable;
867
868 4
        return new self((new Operation\When())()($predicate)($whenTrue)($whenFalse), [$this]);
869
    }
870
871 8
    public function window(int $size): CollectionInterface
872
    {
873 8
        return new self((new Operation\Window())()($size), [$this]);
874
    }
875
876 2
    public function words(): CollectionInterface
877
    {
878 2
        return new self((new Operation\Words())(), [$this]);
879
    }
880
881 4
    public function wrap(): CollectionInterface
882
    {
883 4
        return new self((new Operation\Wrap())(), [$this]);
884
    }
885
886 4
    public function zip(iterable ...$iterables): CollectionInterface
887
    {
888 4
        return new self((new Operation\Zip())()(...$iterables), [$this]);
889
    }
890
}
891