GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Failed Conditions
Push — master ( 1fefb9...e4e1fd )
by Dušan
02:42
created

Collection::drop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace DusanKasan\Knapsack;
4
5
use Closure;
6
use DusanKasan\Knapsack\Exceptions\InvalidArgument;
7
use Iterator;
8
use IteratorAggregate;
9
use RecursiveArrayIterator;
10
use Traversable;
11
12
class Collection implements Iterator
13
{
14
    /**
15
     * @var Iterator
16
     */
17
    protected $input;
18
19
    /**
20
     * @var callable
21
     */
22
    private $generatorFactory;
23
24
    /**
25
     * @param callable|Closure|array|Traversable $input If callable is passed, it must be a generator factory function
26
     */
27 78
    public function __construct($input)
28
    {
29 78
        if (is_array($input)) {
30 68
            $input = new RecursiveArrayIterator($input);
31 68
            $this->input = $input;
32 78
        } elseif ($input instanceof IteratorAggregate) {
33 1
            $input = $input->getIterator();
34 1
            $this->input = $input;
35 68
        } elseif (is_callable($input)) {
36 65
            $this->generatorFactory = $input;
37 65
            $this->input = $input();
38 67
        } elseif ($input instanceof Iterator) {
39 1
            $this->input = $input;
40 1
        } else {
41 1
            throw new InvalidArgument;
42
        }
43 77
    }
44
45
    /**
46
     * Static alias of normal constructor.
47
     *
48
     * @param array|Traversable $input
49
     * @return Collection
50
     */
51 4
    public static function from($input)
52
    {
53 4
        return new self($input);
54
    }
55
56
    /**
57
     * Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying
58
     * $function to the last value in the collection. By default this produces an infinite collection. However you can
59
     * end the collection by throwing a NoMoreItems exception.
60
     *
61
     * @param mixed $input
62
     * @param callable $function
63
     * @return Collection
64
     */
65 2
    public static function iterate($input, callable $function)
66
    {
67 2
        return iterate($input, $function);
68
    }
69
70
    /**
71
     * Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite.
72
     *
73
     * @param mixed $value
74
     * @param int $times
75
     * @return Collection
76
     */
77 3
    public static function repeat($value, $times = -1)
78
    {
79 3
        return repeat($value, $times);
80
    }
81
82
    /**
83
     * Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached.
84
     *
85
     * @param int $start
86
     * @param int|null $end
87
     * @param int $step
88
     * @return Collection
89
     */
90 2
    public static function range($start = 0, $end = null, $step = 1)
91
    {
92 2
        return \DusanKasan\Knapsack\range($start, $end, $step);
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 73
    public function current()
99
    {
100 73
        return $this->input->current();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 72
    public function next()
107
    {
108 72
        $this->input->next();
109 72
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 71
    public function key()
115
    {
116 71
        return $this->input->key();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 76
    public function valid()
123
    {
124 76
        return $this->input->valid();
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 76
    public function rewind()
131
    {
132 76
        if ($this->generatorFactory) {
133 65
            $this->input = call_user_func($this->generatorFactory);
134 65
        }
135
136 76
        $this->input->rewind();
137 76
    }
138
139
    /**
140
     * @return array
141
     */
142 57
    public function toArray()
143
    {
144 57
        return toArray($this);
145
    }
146
147
    /**
148
     * Returns a lazy collection of items for which $function returned true.
149
     *
150
     * @param callable $function ($value, $key)
151
     * @return Collection
152
     */
153 1
    public function filter(callable $function)
154
    {
155 1
        return filter($this, $function);
156
    }
157
158
    /**
159
     * Returns a lazy collection of distinct items. The comparison is the same as in in_array.
160
     *
161
     * @return Collection
162
     */
163 1
    public function distinct()
164
    {
165 1
        return distinct($this);
166
    }
167
168
    /**
169
     * Returns a lazy collection with items from all $collections passed as argument appended together
170
     *
171
     * @param Traversable|array ...$collections
172
     * @return Collection
173
     */
174 1
    public function concat(...$collections)
175
    {
176 1
        return concat($this, ...$collections);
177
    }
178
179
    /**
180
     * Returns collection where each item is changed to the output of executing $function on each key/item.
181
     *
182
     * @param callable $function
183
     * @return Collection
184
     */
185 1
    public function map(callable $function)
186
    {
187 1
        return map($this, $function);
188
    }
189
190
    /**
191
     * Reduces the collection to single value by iterating over the collection and calling $function while
192
     * passing $startValue and current key/item as parameters. The output of $function is used as $startValue in
193
     * next iteration. The output of $function on last element is the return value of this function.
194
     *
195
     * @param mixed $startValue
196
     * @param callable $function ($tmpValue, $value, $key)
197
     * @return mixed
198
     */
199 2
    public function reduce(callable $function, $startValue)
200
    {
201 2
        $result = reduce($this, $function, $startValue);
202
203 2
        return $result;
204
    }
205
206
    /**
207
     * Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value
208
     * is passed.
209
     *
210
     * @param int $depth How many levels should be flatten, default (-1) is infinite.
211
     * @return Collection
212
     */
213 1
    public function flatten($depth = -1)
214
    {
215 1
        return flatten($this, $depth);
216
    }
217
218
    /**
219
     * Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should
220
     * return true if first item is larger than the second and false otherwise.
221
     *
222
     * @param callable $function ($value1, $value2, $key1, $key2)
223
     * @return Collection
224
     */
225 1
    public function sort(callable $function)
226
    {
227 1
        return \DusanKasan\Knapsack\sort($this, $function);
228
    }
229
230
    /**
231
     * Returns lazy collection items of which are part of the original collection from item number $from to item
232
     * number $to. The items before $from are also iterated over, just not returned.
233
     *
234
     * @param int $from
235
     * @param int $to If omitted, will slice until end
236
     * @return Collection
237
     */
238 1
    public function slice($from, $to = -1)
239
    {
240 1
        return slice($this, $from, $to);
241
    }
242
243
    /**
244
     * Returns collection which items are separated into groups indexed by the return value of $function.
245
     *
246
     * @param callable $function ($value, $key)
247
     * @return Collection
248
     */
249 1
    public function groupBy(callable $function)
250
    {
251 1
        return groupBy($this, $function);
252
    }
253
254
    /**
255
     * Returns a lazy collection in which $function is executed for each item.
256
     *
257
     * @param callable $function ($value, $key)
258
     * @return Collection
259
     */
260 1
    public function each(callable $function)
261
    {
262 1
        return \DusanKasan\Knapsack\each($this, $function);
263
    }
264
265
    /**
266
     * Returns the number of items in this collection.
267
     *
268
     * @return int
269
     */
270 6
    public function size()
271
    {
272 6
        return size($this);
273
    }
274
275
    /**
276
     * Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw
277
     * ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable) an
278
     * instance of Collection will be returned.
279
     *
280
     * @param mixed $key
281
     * @param bool $convertToCollection
282
     * @return Collection|mixed
283
     * @throws ItemNotFound
284
     */
285 2
    public function get($key, $convertToCollection = false)
286
    {
287 2
        $result = get($this, $key);
288
289 2
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
290
    }
291
292
    /**
293
     * Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return
294
     * $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value
295
     * is a collection (array|Traversable) an instance of Collection will be returned.
296
     *
297
     * @param mixed $key
298
     * @param mixed $default
299
     * @param bool $convertToCollection
300
     * @return mixed
301
     * @throws ItemNotFound
302
     */
303 1
    public function getOrDefault($key, $default = null, $convertToCollection = false)
304
    {
305 1
        $result = getOrDefault($this, $key, $default);
306
307 1
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
308
    }
309
310
    /**
311
     * Returns nth item in the collection starting from 0. If the size of this collection is smaller than $position,
312
     * throw ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable) an
313
     * instance of Collection will be returned.
314
     *
315
     * @param int $position
316
     * @param bool $convertToCollection
317
     * @return Collection|mixed
318
     * @throws ItemNotFound
319
     */
320 6
    public function getNth($position, $convertToCollection = false)
321 2
    {
322 6
        $result = getNth($this, $position);
323
324 6
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
325
    }
326
327
    /**
328
     * Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true
329
     * and the return value is a collection (array|Traversable) an instance of Collection will be returned.
330
     *
331
     * @param callable $function
332
     * @param mixed|null $default
333
     * @param bool $convertToCollection
334
     * @return Collection|mixed
335
     */
336 1
    public function find(callable $function, $default = null, $convertToCollection = false)
337
    {
338 1
        $result = find($this, $function, $default);
339
340 1
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
341
    }
342
343
    /**
344
     * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of
345
     * items in this collection for which the $function returned this value.
346
     *
347
     * @param callable $function
348
     * @return Collection
349
     */
350 1
    public function countBy(callable $function)
351
    {
352 1
        return countBy($this, $function);
353
    }
354
355
    /**
356
     * Returns a lazy collection by changing keys of this collection for each item to the result of $function for
357
     * that item.
358
     *
359
     * @param callable $function
360
     * @return Collection
361
     */
362 1
    public function indexBy(callable $function)
363
    {
364 1
        return indexBy($this, $function);
365
    }
366
367
    /**
368
     * Returns true if $function returns true for every item in this collection, false otherwise.
369
     *
370
     * @param callable $function
371
     * @return bool
372
     */
373 1
    public function every(callable $function)
374
    {
375 1
        return every($this, $function);
376
    }
377
378
    /**
379
     * Returns true if $function returns true for at least one item in this collection, false otherwise.
380
     *
381
     * @param callable $function
382
     * @return bool
383
     */
384 1
    public function some(callable $function)
385
    {
386 1
        return some($this, $function);
387
    }
388
389
    /**
390
     * Returns true if $value is present in the collection.
391
     *
392
     * @param mixed $value
393
     * @return bool
394
     */
395 1
    public function contains($value)
396
    {
397 1
        return contains($this, $value);
398
    }
399
400
    /**
401
     * Returns collection of items in this collection in reverse order.
402
     *
403
     * @return Collection
404
     */
405 1
    public function reverse()
406
    {
407 1
        return reverse($this);
408
    }
409
410
    /**
411
     * Reduce the collection to single value. Walks from right to left.
412
     *
413
     * @param callable $function Must take 2 arguments, intermediate value and item from the iterator.
414
     * @param mixed $startValue
415
     * @return mixed
416
     */
417 1
    public function reduceRight(callable $function, $startValue)
418
    {
419 1
        return reduceRight($this, $function, $startValue);
420
    }
421
422
    /**
423
     * A form of slice that returns first $numberOfItems items.
424
     *
425
     * @param int $numberOfItems
426
     * @return Collection
427
     */
428 8
    public function take($numberOfItems)
429
    {
430 8
        return take($this, $numberOfItems);
431
    }
432
433
    /**
434
     * A form of slice that returns all but first $numberOfItems items.
435
     *
436
     * @param int $numberOfItems
437
     * @return Collection
438
     */
439 2
    public function drop($numberOfItems)
440
    {
441 2
        return drop($this, $numberOfItems);
442 2
    }
443
444
    /**
445
     * Returns collection of values from this collection but with keys being numerical from 0 upwards.
446
     *
447
     * @return Collection
448
     */
449 10
    public function values()
450
    {
451 10
        return values($this);
452
    }
453
454
    /**
455
     * Returns a lazy collection without elements matched by $function.
456
     *
457
     * @param callable $function
458
     * @return Collection
459
     */
460 1
    public function reject(callable $function)
461
    {
462 1
        return reject($this, $function);
463
    }
464
465
    /**
466
     * Returns a lazy collection of the keys of this collection.
467
     *
468
     * @return Collection
469
     */
470 1
    public function keys()
471
    {
472 1
        return keys($this);
473
    }
474
475
    /**
476
     * Returns a lazy collection of items of this collection separated by $separator
477
     *
478
     * @param mixed $separator
479
     * @return Collection
480
     */
481 1
    public function interpose($separator)
482
    {
483 1
        return interpose($this, $separator);
484
    }
485
486
    /**
487
     * Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped.
488
     *
489
     * @param int $numberOfItems
490
     * @return Collection
491
     */
492 1
    public function dropLast($numberOfItems = 1)
493
    {
494 1
        return dropLast($this, $numberOfItems);
495
    }
496
497
    /**
498
     * Returns a lazy collection of first item from first collection, first item from second, second from first and
499
     * so on. Accepts any number of collections.
500
     *
501
     * @param array|Traversable ...$collections
502
     * @return Collection
503
     */
504 1
    public function interleave(...$collections)
505
    {
506 1
        return interleave($this, ...$collections);
507
    }
508
509
    /**
510
     * Returns an infinite lazy collection of items in this collection repeated infinitely.
511
     *
512
     * @return Collection
513
     */
514 1
    public function cycle()
515
    {
516 1
        return cycle($this);
517
    }
518
519
    /**
520
     * Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided
521
     * it will be next integer index.
522
     *
523
     * @param mixed $value
524
     * @param mixed|null $key
525
     * @return Collection
526
     */
527 2
    public function prepend($value, $key = null)
528
    {
529 2
        return prepend($this, $value, $key);
530
    }
531
532
    /**
533
     * Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided
534
     * it will be next integer index.
535
     *
536
     * @param mixed $value
537
     * @param mixed $key
538
     * @return Collection
539
     */
540 7
    public function append($value, $key = null)
541
    {
542 7
        return append($this, $value, $key);
543
    }
544
545
    /**
546
     * Returns a lazy collection by removing items from this collection until first item for which $function returns
547
     * false.
548
     *
549
     * @param callable $function
550
     * @return Collection
551
     */
552 1
    public function dropWhile(callable $function)
553
    {
554 1
        return dropWhile($this, $function);
555
    }
556
557
    /**
558
     * Returns a lazy collection which is a result of calling map($function) and then flatten(1)
559
     *
560
     * @param callable $function
561
     * @return Collection
562
     */
563 1
    public function mapcat(callable $function)
564
    {
565 1
        return mapcat($this, $function);
566
    }
567
568
    /**
569
     * Returns a lazy collection of items from the start of the ollection until the first item for which $function
570
     * returns false.
571
     *
572
     * @param callable $function
573
     * @return Collection
574
     */
575 1
    public function takeWhile(callable $function)
576
    {
577 1
        return takeWhile($this, $function);
578
    }
579
580
    /**
581
     * Returns a collection of [take($position), drop($position)]
582
     *
583
     * @param int $position
584
     * @return Collection
585
     */
586 1
    public function splitAt($position)
587
    {
588 1
        return splitAt($this, $position);
589
    }
590
591
    /**
592
     * Returns a collection of [takeWhile($predicament), dropWhile($predicament]
593
     *
594
     * @param callable $function
595
     * @return Collection
596
     */
597 1
    public function splitWith(callable $function)
598
    {
599 1
        return splitWith($this, $function);
600
    }
601
602
    /**
603
     * Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap
604
     * are replaced by their values.
605
     *
606
     * @param Traversable|array $replacementMap
607
     * @return Collection
608
     */
609 1
    public function replace($replacementMap)
610
    {
611 1
        return replace($this, $replacementMap);
612
    }
613
614
    /**
615
     * Returns a lazy collection of reduction steps.
616
     *
617
     * @param callable $function
618
     * @param mixed $startValue
619
     * @return Collection
620
     */
621 1
    public function reductions(callable $function, $startValue)
622
    {
623 1
        return reductions($this, $function, $startValue);
624
    }
625
626
    /**
627
     * Returns a lazy collection of every nth item in this collection
628
     *
629
     * @param int $step
630
     * @return Collection
631
     */
632 1
    public function takeNth($step)
633
    {
634 1
        return takeNth($this, $step);
635
    }
636
637
    /**
638
     * Returns a non-collection of shuffled items from this collection
639
     *
640
     * @return Collection
641
     */
642 1
    public function shuffle()
643
    {
644 1
        return shuffle($this);
645
    }
646
647
    /**
648
     * Returns a lazy collection of collections of $numberOfItems items each, at $step step
649
     * apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions
650
     * do not overlap. If a $padding collection is supplied, use its elements as
651
     * necessary to complete last partition up to $numberOfItems items. In case there are
652
     * not enough padding elements, return a partition with less than $numberOfItems items.
653
     *
654
     * @param int $numberOfItems
655
     * @param int $step
656
     * @param array|Traversable $padding
657
     * @return Collection
658
     */
659 1
    public function partition($numberOfItems, $step = 0, $padding = [])
660
    {
661 1
        return partition($this, $numberOfItems, $step, $padding);
662
    }
663
664
    /**
665
     * Creates a lazy collection of collections created by partitioning this collection every time $function will
666
     * return different result.
667
     *
668
     * @param callable $function
669
     * @return Collection
670
     */
671 2
    public function partitionBy(callable $function)
672
    {
673 2
        return partitionBy($this, $function);
674
    }
675
676
    /**
677
     * Returns true if this collection is empty. False otherwise.
678
     *
679
     * @return bool
680
     */
681 2
    public function isEmpty()
682
    {
683 2
        return isEmpty($this);
684
    }
685
686
    /**
687
     * Opposite of isEmpty.
688
     *
689
     * @return bool
690
     */
691 2
    public function isNotEmpty()
692
    {
693 2
        return isNotEmpty($this);
694
    }
695
696
    /**
697
     * Returns a collection where keys are distinct items from this collection and their values are number of
698
     * occurrences of each value.
699
     *
700
     * @return Collection
701
     */
702 1
    public function frequencies()
703
    {
704 1
        return frequencies($this);
705
    }
706
707
    /**
708
     * Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection
709
     * is true and the return value is a collection (array|Traversable) an instance of Collection is returned.
710
     *
711
     * @param bool $convertToCollection
712
     * @return mixed|Collection
713
     * @throws ItemNotFound
714
     */
715 10
    public function first($convertToCollection = false)
716
    {
717 10
        $result = first($this);
718 9
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
719
    }
720
721
    /**
722
     * Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection
723
     * is true and the return value is a collection (array|Traversable) an
724
     *
725
     * @param bool $convertToCollection
726
     * @return mixed|Collection
727
     * @throws ItemNotFound
728
     */
729 2
    public function last($convertToCollection = false)
730
    {
731 2
        $result = last($this);
732 1
        return (isCollection($result) && $convertToCollection) ? new self($result) : $result;
733
    }
734
735
    /**
736
     * Returns a lazy collection by picking a $key key from each sub-collection of $this.
737
     *
738
     * @param mixed $key
739
     * @return Collection
740
     */
741 2
    public function pluck($key)
742
    {
743 2
        return pluck($this, $key);
744
    }
745
746
    /**
747
     * Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values.
748
     *
749
     * @return Collection
750
     */
751 1
    public function realize()
752
    {
753 1
        return realize($this);
754
    }
755
756
    /**
757
     * Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item.
758
     *
759
     * @return mixed
760
     */
761 2
    public function second()
762
    {
763 2
        return second($this);
764
    }
765
766
    /**
767
     * Combines the values of this collection as keys, with values of $collection as values.  The resulting collection
768
     * has length equal to the size of smaller collection. If $strict is true, the size of both collections must be
769
     * equal, otherwise ItemNotFound is thrown. When strict, the collection is realized immediately.
770
     *
771
     * @param array|Traversable $collection
772
     * @param bool $strict
773
     * @return Collection
774
     */
775 1
    public function combine($collection, $strict = false)
776
    {
777 1
        return combine($this, $collection, $strict);
778
    }
779
780
    /**
781
     * Returns a lazy collection without the items associated to any of the keys from $keys.
782
     *
783
     * @param array|Traversable $keys
784
     * @return Collection
785
     */
786 1
    public function except($keys)
787
    {
788 1
        return except($this, $keys);
789
    }
790
791
    /**
792
     * Returns a lazy collection of items associated to any of the keys from $keys.
793
     *
794
     * @param array|Traversable $keys
795
     * @return Collection
796
     */
797 1
    public function only($keys)
798
    {
799 1
        return only($this, $keys);
800
    }
801
802
    /**
803
     * Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the
804
     * ...$collections are iterated non-lazily.
805
     *
806
     * @param array|Traversable ...$collections
807
     * @return Collection
808
     */
809 1
    public function difference(...$collections)
810
    {
811 1
        return difference($this, ...$collections);
812
    }
813
814
815
    /**
816
     * Returns a lazy collection where keys and values are flipped.
817
     *
818
     * @return Collection
819
     */
820 1
    public function flip()
821
    {
822 1
        return flip($this);
823
    }
824
825
    /**
826
     * Checks for the existence of $key in this collection.
827
     *
828
     * @param mixed $key
829
     * @return bool
830
     */
831 1
    public function has($key)
832
    {
833 1
        return has($this, $key);
834
    }
835
}
836