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.
Passed
Push — master ( 9bd2f0...277002 )
by Dušan
03:03
created

CollectionTrait::pluck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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