Issues (99)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/support/Collection.php (23 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Childish\support;
3
4
use stdClass;
5
use Countable;
6
use Exception;
7
use ArrayAccess;
8
use Traversable;
9
use ArrayIterator;
10
use CachingIterator;
11
use JsonSerializable;
12
use IteratorAggregate;
13
14
15
/**
16
 * Collection
17
 *
18
 * @author    Pu ShaoWei <[email protected]>
19
 * @date      2017/12/7
20
 * @package   Childish\support
21
 * @version   1.0
22
 */
23
class Collection implements ArrayAccess, Countable, IteratorAggregate,  JsonSerializable
24
{
25
26
    /**
27
     * The items contained in the collection.
28
     *
29
     * @var array
30
     */
31
    protected $items = [];
32
33
    /**
34
     * The methods that can be proxied.
35
     *
36
     * @var array
37
     */
38
    protected static $proxies = [
39
        'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap',
40
        'keyBy', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum',
41
    ];
42
43
    /**
44
     * Create a new collection.
45
     *
46
     * @param  mixed $items
47
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
     */
49
    public function __construct($items = [])
50
    {
51
        $this->items = $this->getArrayableItems($items);
52
    }
53
54
    /**
55
     * Create a new collection instance if the value isn't one already.
56
     *
57
     * @param  mixed $items
58
     * @return static
59
     */
60
    public static function make($items = [])
61
    {
62
        return new static($items);
63
    }
64
65
    /**
66
     * Wrap the given value in a collection if applicable.
67
     *
68
     * @param  mixed $value
69
     * @return static
70
     */
71
    public static function wrap($value)
72
    {
73
        return $value instanceof self
74
            ? new static($value)
75
            : new static(Tools::wrap($value));
76
    }
77
78
    /**
79
     * Get the underlying items from the given collection if applicable.
80
     *
81
     * @param  array|static $value
82
     * @return array
83
     */
84
    public static function unwrap($value)
85
    {
86
        return $value instanceof self ? $value->all() : $value;
87
    }
88
89
    /**
90
     * Create a new collection by invoking the callback a given amount of times.
91
     *
92
     * @param  int      $number
93
     * @param  callable $callback
94
     * @return static
95
     */
96
    public static function times($number, callable $callback = null)
97
    {
98
        if ($number < 1) {
99
            return new static;
100
        }
101
102
        if (is_null($callback)) {
103
            return new static(range(1, $number));
104
        }
105
106
        return (new static(range(1, $number)))->map($callback);
107
    }
108
109
    /**
110
     * Get all of the items in the collection.
111
     *
112
     * @return array
113
     */
114
    public function all()
115
    {
116
        return $this->items;
117
    }
118
119
    /**
120
     * Get the average value of a given key.
121
     *
122
     * @param  callable|string|null $callback
123
     * @return mixed
124
     */
125
    public function avg($callback = null)
126
    {
127
        if ($count = $this->count()) {
128
            return $this->sum($callback) / $count;
129
        }
130
    }
131
132
    /**
133
     * Alias for the "avg" method.
134
     *
135
     * @param  callable|string|null $callback
136
     * @return mixed
137
     */
138
    public function average($callback = null)
139
    {
140
        return $this->avg($callback);
141
    }
142
143
    /**
144
     * Get the median of a given key.
145
     *
146
     * @param  null $key
147
     * @return mixed
148
     */
149
    public function median($key = null)
150
    {
151
        $count = $this->count();
152
153
        if ($count == 0) {
154
            return;
155
        }
156
157
        $values = (isset($key) ? $this->pluck($key) : $this)
158
            ->sort()->values();
159
160
        $middle = (int)($count / 2);
161
162
        if ($count % 2) {
163
            return $values->get($middle);
164
        }
165
166
        return (new static([
167
            $values->get($middle - 1), $values->get($middle),
168
        ]))->average();
169
    }
170
171
    /**
172
     * Get the mode of a given key.
173
     *
174
     * @param  mixed $key
175
     * @return array|null
176
     */
177
    public function mode($key = null)
178
    {
179
        $count = $this->count();
180
181
        if ($count == 0) {
182
            return;
183
        }
184
185
        $collection = isset($key) ? $this->pluck($key) : $this;
186
187
        $counts = new self;
188
189
        $collection->each(function ($value) use ($counts) {
190
            $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1;
191
        });
192
193
        $sorted = $counts->sort();
194
195
        $highestValue = $sorted->last();
196
197
        return $sorted->filter(function ($value) use ($highestValue) {
198
            return $value == $highestValue;
199
        })->sort()->keys()->all();
200
    }
201
202
    /**
203
     * Collapse the collection of items into a single array.
204
     *
205
     * @return static
206
     */
207
    public function collapse()
208
    {
209
        return new static(Tools::collapse($this->items));
0 ignored issues
show
The method collapse() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
210
    }
211
212
    /**
213
     * Determine if an item exists in the collection.
214
     *
215
     * @param  mixed $key
216
     * @param  mixed $operator
217
     * @param  mixed $value
218
     * @return bool
219
     */
220
    public function contains($key, $operator = null, $value = null)
221
    {
222
        if (func_num_args() == 1) {
223
            if ($this->useAsCallable($key)) {
224
                $placeholder = new stdClass;
225
226
                return $this->first($key, $placeholder) !== $placeholder;
227
            }
228
229
            return in_array($key, $this->items);
230
        }
231
232
        if (func_num_args() == 2) {
233
            $value = $operator;
234
235
            $operator = '=';
236
        }
237
238
        return $this->contains($this->operatorForWhere($key, $operator, $value));
239
    }
240
241
    /**
242
     * Determine if an item exists in the collection using strict comparison.
243
     *
244
     * @param  mixed $key
245
     * @param  mixed $value
246
     * @return bool
247
     */
248
    public function containsStrict($key, $value = null)
249
    {
250
        if (func_num_args() == 2) {
251
            return $this->contains(function ($item) use ($key, $value) {
252
                return data_get($item, $key) === $value;
253
            });
254
        }
255
256
        if ($this->useAsCallable($key)) {
257
            return !is_null($this->first($key));
258
        }
259
260
        return in_array($key, $this->items, true);
261
    }
262
263
    /**
264
     * Cross join with the given lists, returning all possible permutations.
265
     *
266
     * @param  mixed ...$lists
267
     * @return static
268
     */
269
    public function crossJoin(...$lists)
270
    {
271
        return new static(Tools::crossJoin(
0 ignored issues
show
The method crossJoin() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
272
            $this->items, ...array_map([$this, 'getArrayableItems'], $lists)
273
        ));
274
    }
275
276
    /**
277
     * Get the items in the collection that are not present in the given items.
278
     *
279
     * @param  mixed $items
280
     * @return static
281
     */
282
    public function diff($items)
283
    {
284
        return new static(array_diff($this->items, $this->getArrayableItems($items)));
285
    }
286
287
    /**
288
     * Get the items in the collection whose keys and values are not present in the given items.
289
     *
290
     * @param  mixed $items
291
     * @return static
292
     */
293
    public function diffAssoc($items)
294
    {
295
        return new static(array_diff_assoc($this->items, $this->getArrayableItems($items)));
296
    }
297
298
    /**
299
     * Get the items in the collection whose keys are not present in the given items.
300
     *
301
     * @param  mixed $items
302
     * @return static
303
     */
304
    public function diffKeys($items)
305
    {
306
        return new static(array_diff_key($this->items, $this->getArrayableItems($items)));
307
    }
308
309
    /**
310
     * Execute a callback over each item.
311
     *
312
     * @param  callable $callback
313
     * @return $this
314
     */
315
    public function each(callable $callback)
316
    {
317
        foreach ($this->items as $key => $item) {
318
            if ($callback($item, $key) === false) {
319
                break;
320
            }
321
        }
322
323
        return $this;
324
    }
325
326
    /**
327
     * Execute a callback over each nested chunk of items.
328
     *
329
     * @param  callable $callback
330
     * @return static
331
     */
332
    public function eachSpread(callable $callback)
333
    {
334
        return $this->each(function ($chunk, $key) use ($callback) {
335
            $chunk[] = $key;
336
337
            return $callback(...$chunk);
338
        });
339
    }
340
341
    /**
342
     * Determine if all items in the collection pass the given test.
343
     *
344
     * @param  string|callable $key
345
     * @param  mixed           $operator
346
     * @param  mixed           $value
347
     * @return bool
348
     */
349
    public function every($key, $operator = null, $value = null)
350
    {
351
        if (func_num_args() == 1) {
352
            $callback = $this->valueRetriever($key);
0 ignored issues
show
$key is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
353
354
            foreach ($this->items as $k => $v) {
355
                if (!$callback($v, $k)) {
356
                    return false;
357
                }
358
            }
359
360
            return true;
361
        }
362
363
        if (func_num_args() == 2) {
364
            $value = $operator;
365
366
            $operator = '=';
367
        }
368
369
        return $this->every($this->operatorForWhere($key, $operator, $value));
0 ignored issues
show
$key is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
370
    }
371
372
    /**
373
     * Get all items except for those with the specified keys.
374
     *
375
     * @param  mixed $keys
376
     * @return static
377
     */
378
    public function except($keys)
379
    {
380
        $keys = is_array($keys) ? $keys : func_get_args();
381
382
        return new static(Tools::except($this->items, $keys));
383
    }
384
385
    /**
386
     * Run a filter over each of the items.
387
     *
388
     * @param  callable|null $callback
389
     * @return static
390
     */
391
    public function filter(callable $callback = null)
392
    {
393
        if ($callback) {
394
            return new static(Tools::where($this->items, $callback));
395
        }
396
397
        return new static(array_filter($this->items));
398
    }
399
400
    /**
401
     * Apply the callback if the value is truthy.
402
     *
403
     * @param  bool     $value
404
     * @param  callable $callback
405
     * @param  callable $default
406
     * @return mixed
407
     */
408
    public function when($value, callable $callback, callable $default = null)
409
    {
410
        if ($value) {
411
            return $callback($this);
412
        } else if ($default) {
413
            return $default($this);
414
        }
415
416
        return $this;
417
    }
418
419
    /**
420
     * Apply the callback if the value is falsy.
421
     *
422
     * @param  bool     $value
423
     * @param  callable $callback
424
     * @param  callable $default
425
     * @return mixed
426
     */
427
    public function unless($value, callable $callback, callable $default = null)
428
    {
429
        return $this->when(!$value, $callback, $default);
430
    }
431
432
    /**
433
     * Filter items by the given key value pair.
434
     *
435
     * @param  string $key
436
     * @param  mixed  $operator
437
     * @param  mixed  $value
438
     * @return static
439
     */
440
    public function where($key, $operator, $value = null)
441
    {
442
        if (func_num_args() == 2) {
443
            $value = $operator;
444
445
            $operator = '=';
446
        }
447
448
        return $this->filter($this->operatorForWhere($key, $operator, $value));
449
    }
450
451
    /**
452
     * Get an operator checker callback.
453
     *
454
     * @param  string $key
455
     * @param  string $operator
456
     * @param  mixed  $value
457
     * @return \Closure
458
     */
459
    protected function operatorForWhere($key, $operator, $value)
460
    {
461
        return function ($item) use ($key, $operator, $value) {
462
            $retrieved = data_get($item, $key);
463
464
            try {
465
                switch ($operator) {
466
                    default:
467
                    case '=':
468
                    case '==':
469
                        return $retrieved == $value;
470
                    case '!=':
471
                    case '<>':
472
                        return $retrieved != $value;
473
                    case '<':
474
                        return $retrieved < $value;
475
                    case '>':
476
                        return $retrieved > $value;
477
                    case '<=':
478
                        return $retrieved <= $value;
479
                    case '>=':
480
                        return $retrieved >= $value;
481
                    case '===':
482
                        return $retrieved === $value;
483
                    case '!==':
484
                        return $retrieved !== $value;
485
                }
486
            } catch (Exception $e) {
0 ignored issues
show
catch (\Exception $e) { return false; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
487
                return false;
488
            }
489
        };
490
    }
491
492
    /**
493
     * Filter items by the given key value pair using strict comparison.
494
     *
495
     * @param  string $key
496
     * @param  mixed  $value
497
     * @return static
498
     */
499
    public function whereStrict($key, $value)
500
    {
501
        return $this->where($key, '===', $value);
502
    }
503
504
    /**
505
     * Filter items by the given key value pair.
506
     *
507
     * @param  string $key
508
     * @param  mixed  $values
509
     * @param  bool   $strict
510
     * @return static
511
     */
512
    public function whereIn($key, $values, $strict = false)
513
    {
514
        $values = $this->getArrayableItems($values);
515
516
        return $this->filter(function ($item) use ($key, $values, $strict) {
517
            return in_array(data_get($item, $key), $values, $strict);
518
        });
519
    }
520
521
    /**
522
     * Filter items by the given key value pair using strict comparison.
523
     *
524
     * @param  string $key
525
     * @param  mixed  $values
526
     * @return static
527
     */
528
    public function whereInStrict($key, $values)
529
    {
530
        return $this->whereIn($key, $values, true);
531
    }
532
533
    /**
534
     * Filter items by the given key value pair.
535
     *
536
     * @param  string $key
537
     * @param  mixed  $values
538
     * @param  bool   $strict
539
     * @return static
540
     */
541
    public function whereNotIn($key, $values, $strict = false)
542
    {
543
        $values = $this->getArrayableItems($values);
544
545
        return $this->reject(function ($item) use ($key, $values, $strict) {
546
            return in_array(data_get($item, $key), $values, $strict);
547
        });
548
    }
549
550
    /**
551
     * Filter items by the given key value pair using strict comparison.
552
     *
553
     * @param  string $key
554
     * @param  mixed  $values
555
     * @return static
556
     */
557
    public function whereNotInStrict($key, $values)
558
    {
559
        return $this->whereNotIn($key, $values, true);
560
    }
561
562
    /**
563
     * Get the first item from the collection.
564
     *
565
     * @param  callable|null $callback
566
     * @param  mixed         $default
567
     * @return mixed
568
     */
569
    public function first(callable $callback = null, $default = null)
570
    {
571
        return Tools::first($this->items, $callback, $default);
572
    }
573
574
    /**
575
     * Get a flattened array of the items in the collection.
576
     *
577
     * @param  int $depth
578
     * @return static
579
     */
580
    public function flatten($depth = INF)
581
    {
582
        return new static(Tools::flatten($this->items, $depth));
583
    }
584
585
    /**
586
     * Flip the items in the collection.
587
     *
588
     * @return static
589
     */
590
    public function flip()
591
    {
592
        return new static(array_flip($this->items));
593
    }
594
595
    /**
596
     * Remove an item from the collection by key.
597
     *
598
     * @param  string|array $keys
599
     * @return $this
600
     */
601
    public function forget($keys)
602
    {
603
        foreach ((array)$keys as $key) {
604
            $this->offsetUnset($key);
605
        }
606
607
        return $this;
608
    }
609
610
    /**
611
     * Get an item from the collection by key.
612
     *
613
     * @param  mixed $key
614
     * @param  mixed $default
615
     * @return mixed
616
     */
617
    public function get($key, $default = null)
618
    {
619
        if ($this->offsetExists($key)) {
620
            return $this->items[$key];
621
        }
622
623
        return value($default);
624
    }
625
626
    /**
627
     * Group an associative array by a field or using a callback.
628
     *
629
     * @param  callable|string $groupBy
630
     * @param  bool            $preserveKeys
631
     * @return static
632
     */
633
    public function groupBy($groupBy, $preserveKeys = false)
634
    {
635
        $groupBy = $this->valueRetriever($groupBy);
0 ignored issues
show
$groupBy is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
636
637
        $results = [];
638
639
        foreach ($this->items as $key => $value) {
640
            $groupKeys = $groupBy($value, $key);
641
642
            if (!is_array($groupKeys)) {
643
                $groupKeys = [$groupKeys];
644
            }
645
646
            foreach ($groupKeys as $groupKey) {
647
                $groupKey = is_bool($groupKey) ? (int)$groupKey : $groupKey;
648
649
                if (!array_key_exists($groupKey, $results)) {
650
                    $results[$groupKey] = new static;
651
                }
652
653
                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
654
            }
655
        }
656
657
        return new static($results);
658
    }
659
660
    /**
661
     * Key an associative array by a field or using a callback.
662
     *
663
     * @param  callable|string $keyBy
664
     * @return static
665
     */
666
    public function keyBy($keyBy)
667
    {
668
        $keyBy = $this->valueRetriever($keyBy);
0 ignored issues
show
$keyBy is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
669
670
        $results = [];
671
672
        foreach ($this->items as $key => $item) {
673
            $resolvedKey = $keyBy($item, $key);
674
675
            if (is_object($resolvedKey)) {
676
                $resolvedKey = (string)$resolvedKey;
677
            }
678
679
            $results[$resolvedKey] = $item;
680
        }
681
682
        return new static($results);
683
    }
684
685
    /**
686
     * Determine if an item exists in the collection by key.
687
     *
688
     * @param  mixed $key
689
     * @return bool
690
     */
691
    public function has($key)
692
    {
693
        $keys = is_array($key) ? $key : func_get_args();
694
695
        foreach ($keys as $value) {
696
            if (!$this->offsetExists($value)) {
697
                return false;
698
            }
699
        }
700
701
        return true;
702
    }
703
704
    /**
705
     * Concatenate values of a given key as a string.
706
     *
707
     * @param  string $value
708
     * @param  string $glue
709
     * @return string
710
     */
711
    public function implode($value, $glue = null)
712
    {
713
        $first = $this->first();
714
715
        if (is_array($first) || is_object($first)) {
716
            return implode($glue, $this->pluck($value)->all());
717
        }
718
719
        return implode($value, $this->items);
720
    }
721
722
    /**
723
     * Intersect the collection with the given items.
724
     *
725
     * @param  mixed $items
726
     * @return static
727
     */
728
    public function intersect($items)
729
    {
730
        return new static(array_intersect($this->items, $this->getArrayableItems($items)));
731
    }
732
733
    /**
734
     * Intersect the collection with the given items by key.
735
     *
736
     * @param  mixed $items
737
     * @return static
738
     */
739
    public function intersectByKeys($items)
740
    {
741
        return new static(array_intersect_key(
742
            $this->items, $this->getArrayableItems($items)
743
        ));
744
    }
745
746
    /**
747
     * Determine if the collection is empty or not.
748
     *
749
     * @return bool
750
     */
751
    public function isEmpty()
752
    {
753
        return empty($this->items);
754
    }
755
756
    /**
757
     * Determine if the collection is not empty.
758
     *
759
     * @return bool
760
     */
761
    public function isNotEmpty()
762
    {
763
        return !$this->isEmpty();
764
    }
765
766
    /**
767
     * Determine if the given value is callable, but not a string.
768
     *
769
     * @param  mixed $value
770
     * @return bool
771
     */
772
    protected function useAsCallable($value)
773
    {
774
        return !is_string($value) && is_callable($value);
775
    }
776
777
    /**
778
     * Get the keys of the collection items.
779
     *
780
     * @return static
781
     */
782
    public function keys()
783
    {
784
        return new static(array_keys($this->items));
785
    }
786
787
    /**
788
     * Get the last item from the collection.
789
     *
790
     * @param  callable|null $callback
791
     * @param  mixed         $default
792
     * @return mixed
793
     */
794
    public function last(callable $callback = null, $default = null)
795
    {
796
        return Tools::last($this->items, $callback, $default);
0 ignored issues
show
The method last() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
797
    }
798
799
    /**
800
     * Get the values of a given key.
801
     *
802
     * @param  string|array $value
803
     * @param  string|null  $key
804
     * @return static
805
     */
806
    public function pluck($value, $key = null)
807
    {
808
        return new static(Tools::pluck($this->items, $value, $key));
0 ignored issues
show
The method pluck() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
809
    }
810
811
    /**
812
     * Run a map over each of the items.
813
     *
814
     * @param  callable $callback
815
     * @return static
816
     */
817
    public function map(callable $callback)
818
    {
819
        $keys = array_keys($this->items);
820
821
        $items = array_map($callback, $this->items, $keys);
822
823
        return new static(array_combine($keys, $items));
824
    }
825
826
    /**
827
     * Run a map over each nested chunk of items.
828
     *
829
     * @param  callable $callback
830
     * @return static
831
     */
832
    public function mapSpread(callable $callback)
833
    {
834
        return $this->map(function ($chunk, $key) use ($callback) {
835
            $chunk[] = $key;
836
837
            return $callback(...$chunk);
838
        });
839
    }
840
841
    /**
842
     * Run a dictionary map over the items.
843
     * The callback should return an associative array with a single key/value pair.
844
     *
845
     * @param  callable $callback
846
     * @return static
847
     */
848
    public function mapToDictionary(callable $callback)
849
    {
850
        $dictionary = $this->map($callback)->reduce(function ($groups, $pair) {
851
            $groups[key($pair)][] = reset($pair);
852
853
            return $groups;
854
        }, []);
855
856
        return new static($dictionary);
857
    }
858
859
    /**
860
     * Run a grouping map over the items.
861
     * The callback should return an associative array with a single key/value pair.
862
     *
863
     * @param  callable $callback
864
     * @return static
865
     */
866
    public function mapToGroups(callable $callback)
867
    {
868
        $groups = $this->mapToDictionary($callback);
869
870
        return $groups->map([$this, 'make']);
871
    }
872
873
    /**
874
     * Run an associative map over each of the items.
875
     * The callback should return an associative array with a single key/value pair.
876
     *
877
     * @param  callable $callback
878
     * @return static
879
     */
880
    public function mapWithKeys(callable $callback)
881
    {
882
        $result = [];
883
884
        foreach ($this->items as $key => $value) {
885
            $assoc = $callback($value, $key);
886
887
            foreach ($assoc as $mapKey => $mapValue) {
888
                $result[$mapKey] = $mapValue;
889
            }
890
        }
891
892
        return new static($result);
893
    }
894
895
    /**
896
     * Map a collection and flatten the result by a single level.
897
     *
898
     * @param  callable $callback
899
     * @return static
900
     */
901
    public function flatMap(callable $callback)
902
    {
903
        return $this->map($callback)->collapse();
904
    }
905
906
    /**
907
     * Map the values into a new class.
908
     *
909
     * @param  string $class
910
     * @return static
911
     */
912
    public function mapInto($class)
913
    {
914
        return $this->map(function ($value, $key) use ($class) {
915
            return new $class($value, $key);
916
        });
917
    }
918
919
    /**
920
     * Get the max value of a given key.
921
     *
922
     * @param  callable|string|null $callback
923
     * @return mixed
924
     */
925
    public function max($callback = null)
926
    {
927
        $callback = $this->valueRetriever($callback);
0 ignored issues
show
$callback is of type callable|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
928
929
        return $this->filter(function ($value) {
930
            return !is_null($value);
931
        })->reduce(function ($result, $item) use ($callback) {
932
            $value = $callback($item);
933
934
            return is_null($result) || $value > $result ? $value : $result;
935
        });
936
    }
937
938
    /**
939
     * Merge the collection with the given items.
940
     *
941
     * @param  mixed $items
942
     * @return static
943
     */
944
    public function merge($items)
945
    {
946
        return new static(array_merge($this->items, $this->getArrayableItems($items)));
947
    }
948
949
    /**
950
     * Create a collection by using this collection for keys and another for its values.
951
     *
952
     * @param  mixed $values
953
     * @return static
954
     */
955
    public function combine($values)
956
    {
957
        return new static(array_combine($this->all(), $this->getArrayableItems($values)));
958
    }
959
960
    /**
961
     * Union the collection with the given items.
962
     *
963
     * @param  mixed $items
964
     * @return static
965
     */
966
    public function union($items)
967
    {
968
        return new static($this->items + $this->getArrayableItems($items));
969
    }
970
971
    /**
972
     * Get the min value of a given key.
973
     *
974
     * @param  callable|string|null $callback
975
     * @return mixed
976
     */
977
    public function min($callback = null)
978
    {
979
        $callback = $this->valueRetriever($callback);
0 ignored issues
show
$callback is of type callable|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
980
981
        return $this->filter(function ($value) {
982
            return !is_null($value);
983
        })->reduce(function ($result, $item) use ($callback) {
984
            $value = $callback($item);
985
986
            return is_null($result) || $value < $result ? $value : $result;
987
        });
988
    }
989
990
    /**
991
     * Create a new collection consisting of every n-th element.
992
     *
993
     * @param  int $step
994
     * @param  int $offset
995
     * @return static
996
     */
997
    public function nth($step, $offset = 0)
998
    {
999
        $new = [];
1000
1001
        $position = 0;
1002
1003
        foreach ($this->items as $item) {
1004
            if ($position % $step === $offset) {
1005
                $new[] = $item;
1006
            }
1007
1008
            $position++;
1009
        }
1010
1011
        return new static($new);
1012
    }
1013
1014
    /**
1015
     * Get the items with the specified keys.
1016
     *
1017
     * @param  mixed $keys
1018
     * @return static
1019
     */
1020
    public function only($keys)
1021
    {
1022
        if (is_null($keys)) {
1023
            return new static($this->items);
1024
        }
1025
1026
        $keys = is_array($keys) ? $keys : func_get_args();
1027
1028
        return new static(Tools::only($this->items, $keys));
0 ignored issues
show
The method only() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1029
    }
1030
1031
    /**
1032
     * "Paginate" the collection by slicing it into a smaller collection.
1033
     *
1034
     * @param  int $page
1035
     * @param  int $perPage
1036
     * @return static
1037
     */
1038
    public function forPage($page, $perPage)
1039
    {
1040
        $offset = max(0, ($page - 1) * $perPage);
1041
1042
        return $this->slice($offset, $perPage);
1043
    }
1044
1045
    /**
1046
     * Partition the collection into two arrays using the given callback or key.
1047
     *
1048
     * @param  callable|string $callback
1049
     * @return static
1050
     */
1051
    public function partition($callback)
1052
    {
1053
        $partitions = [new static, new static];
1054
1055
        $callback = $this->valueRetriever($callback);
0 ignored issues
show
$callback is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1056
1057
        foreach ($this->items as $key => $item) {
1058
            $partitions[(int)!$callback($item, $key)][$key] = $item;
1059
        }
1060
1061
        return new static($partitions);
1062
    }
1063
1064
    /**
1065
     * Pass the collection to the given callback and return the result.
1066
     *
1067
     * @param  callable $callback
1068
     * @return mixed
1069
     */
1070
    public function pipe(callable $callback)
1071
    {
1072
        return $callback($this);
1073
    }
1074
1075
    /**
1076
     * Get and remove the last item from the collection.
1077
     *
1078
     * @return mixed
1079
     */
1080
    public function pop()
1081
    {
1082
        return array_pop($this->items);
1083
    }
1084
1085
    /**
1086
     * Push an item onto the beginning of the collection.
1087
     *
1088
     * @param  mixed $value
1089
     * @param  mixed $key
1090
     * @return $this
1091
     */
1092
    public function prepend($value, $key = null)
1093
    {
1094
        $this->items = Tools::prepend($this->items, $value, $key);
0 ignored issues
show
The method prepend() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1095
1096
        return $this;
1097
    }
1098
1099
    /**
1100
     * Push an item onto the end of the collection.
1101
     *
1102
     * @param  mixed $value
1103
     * @return $this
1104
     */
1105
    public function push($value)
1106
    {
1107
        $this->offsetSet(null, $value);
1108
1109
        return $this;
1110
    }
1111
1112
    /**
1113
     * Push all of the given items onto the collection.
1114
     *
1115
     * @param  \Traversable $source
1116
     * @return $this
1117
     */
1118
    public function concat($source)
1119
    {
1120
        $result = new static($this);
1121
1122
        foreach ($source as $item) {
1123
            $result->push($item);
1124
        }
1125
1126
        return $result;
1127
    }
1128
1129
    /**
1130
     * Get and remove an item from the collection.
1131
     *
1132
     * @param  mixed $key
1133
     * @param  mixed $default
1134
     * @return mixed
1135
     */
1136
    public function pull($key, $default = null)
1137
    {
1138
        return Tools::pull($this->items, $key, $default);
0 ignored issues
show
The method pull() does not seem to exist on object<Childish\support\Tools>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1139
    }
1140
1141
    /**
1142
     * Put an item in the collection by key.
1143
     *
1144
     * @param  mixed $key
1145
     * @param  mixed $value
1146
     * @return $this
1147
     */
1148
    public function put($key, $value)
1149
    {
1150
        $this->offsetSet($key, $value);
1151
1152
        return $this;
1153
    }
1154
1155
    /**
1156
     * Get one or a specified number of items randomly from the collection.
1157
     *
1158
     * @param  int|null $number
1159
     * @return mixed
1160
     * @throws \InvalidArgumentException
1161
     */
1162
    public function random($number = null)
1163
    {
1164
        if (is_null($number)) {
1165
            return Tools::random($this->items);
1166
        }
1167
1168
        return new static(Tools::random($this->items, $number));
1169
    }
1170
1171
    /**
1172
     * Reduce the collection to a single value.
1173
     *
1174
     * @param  callable $callback
1175
     * @param  mixed    $initial
1176
     * @return mixed
1177
     */
1178
    public function reduce(callable $callback, $initial = null)
1179
    {
1180
        return array_reduce($this->items, $callback, $initial);
1181
    }
1182
1183
    /**
1184
     * Create a collection of all elements that do not pass a given truth test.
1185
     *
1186
     * @param  callable|mixed $callback
1187
     * @return static
1188
     */
1189
    public function reject($callback)
1190
    {
1191
        if ($this->useAsCallable($callback)) {
1192
            return $this->filter(function ($value, $key) use ($callback) {
1193
                return !$callback($value, $key);
1194
            });
1195
        }
1196
1197
        return $this->filter(function ($item) use ($callback) {
1198
            return $item != $callback;
1199
        });
1200
    }
1201
1202
    /**
1203
     * Reverse items order.
1204
     *
1205
     * @return static
1206
     */
1207
    public function reverse()
1208
    {
1209
        return new static(array_reverse($this->items, true));
1210
    }
1211
1212
    /**
1213
     * Search the collection for a given value and return the corresponding key if successful.
1214
     *
1215
     * @param  mixed $value
1216
     * @param  bool  $strict
1217
     * @return mixed
1218
     */
1219
    public function search($value, $strict = false)
1220
    {
1221
        if (!$this->useAsCallable($value)) {
1222
            return array_search($value, $this->items, $strict);
1223
        }
1224
1225
        foreach ($this->items as $key => $item) {
1226
            if (call_user_func($value, $item, $key)) {
1227
                return $key;
1228
            }
1229
        }
1230
1231
        return false;
1232
    }
1233
1234
    /**
1235
     * Get and remove the first item from the collection.
1236
     *
1237
     * @return mixed
1238
     */
1239
    public function shift()
1240
    {
1241
        return array_shift($this->items);
1242
    }
1243
1244
    /**
1245
     * Shuffle the items in the collection.
1246
     *
1247
     * @param  int $seed
1248
     * @return static
1249
     */
1250
    public function shuffle($seed = null)
1251
    {
1252
        $items = $this->items;
1253
1254
        if (is_null($seed)) {
1255
            shuffle($items);
1256
        } else {
1257
            srand($seed);
1258
1259
            usort($items, function () {
1260
                return rand(-1, 1);
1261
            });
1262
        }
1263
1264
        return new static($items);
1265
    }
1266
1267
    /**
1268
     * Slice the underlying collection array.
1269
     *
1270
     * @param  int $offset
1271
     * @param  int $length
1272
     * @return static
1273
     */
1274
    public function slice($offset, $length = null)
1275
    {
1276
        return new static(array_slice($this->items, $offset, $length, true));
1277
    }
1278
1279
    /**
1280
     * Split a collection into a certain number of groups.
1281
     *
1282
     * @param  int $numberOfGroups
1283
     * @return static
1284
     */
1285
    public function split($numberOfGroups)
1286
    {
1287
        if ($this->isEmpty()) {
1288
            return new static;
1289
        }
1290
1291
        $groupSize = ceil($this->count() / $numberOfGroups);
1292
1293
        return $this->chunk($groupSize);
1294
    }
1295
1296
    /**
1297
     * Chunk the underlying collection array.
1298
     *
1299
     * @param  int $size
1300
     * @return static
1301
     */
1302
    public function chunk($size)
1303
    {
1304
        if ($size <= 0) {
1305
            return new static;
1306
        }
1307
1308
        $chunks = [];
1309
1310
        foreach (array_chunk($this->items, $size, true) as $chunk) {
1311
            $chunks[] = new static($chunk);
1312
        }
1313
1314
        return new static($chunks);
1315
    }
1316
1317
    /**
1318
     * Sort through each item with a callback.
1319
     *
1320
     * @param  callable|null $callback
1321
     * @return static
1322
     */
1323
    public function sort(callable $callback = null)
1324
    {
1325
        $items = $this->items;
1326
1327
        $callback
1328
            ? uasort($items, $callback)
1329
            : asort($items);
1330
1331
        return new static($items);
1332
    }
1333
1334
    /**
1335
     * Sort the collection using the given callback.
1336
     *
1337
     * @param  callable|string $callback
1338
     * @param  int             $options
1339
     * @param  bool            $descending
1340
     * @return static
1341
     */
1342
    public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
1343
    {
1344
        $results = [];
1345
1346
        $callback = $this->valueRetriever($callback);
0 ignored issues
show
$callback is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1347
1348
        // First we will loop through the items and get the comparator from a callback
1349
        // function which we were given. Then, we will sort the returned values and
1350
        // and grab the corresponding values for the sorted keys from this array.
1351
        foreach ($this->items as $key => $value) {
1352
            $results[$key] = $callback($value, $key);
1353
        }
1354
1355
        $descending ? arsort($results, $options)
1356
            : asort($results, $options);
1357
1358
        // Once we have sorted all of the keys in the array, we will loop through them
1359
        // and grab the corresponding model so we can set the underlying items list
1360
        // to the sorted version. Then we'll just return the collection instance.
1361
        foreach (array_keys($results) as $key) {
1362
            $results[$key] = $this->items[$key];
1363
        }
1364
1365
        return new static($results);
1366
    }
1367
1368
    /**
1369
     * Sort the collection in descending order using the given callback.
1370
     *
1371
     * @param  callable|string $callback
1372
     * @param  int             $options
1373
     * @return static
1374
     */
1375
    public function sortByDesc($callback, $options = SORT_REGULAR)
1376
    {
1377
        return $this->sortBy($callback, $options, true);
1378
    }
1379
1380
    /**
1381
     * Splice a portion of the underlying collection array.
1382
     *
1383
     * @param  int      $offset
1384
     * @param  int|null $length
1385
     * @param  mixed    $replacement
1386
     * @return static
1387
     */
1388
    public function splice($offset, $length = null, $replacement = [])
1389
    {
1390
        if (func_num_args() == 1) {
1391
            return new static(array_splice($this->items, $offset));
1392
        }
1393
1394
        return new static(array_splice($this->items, $offset, $length, $replacement));
1395
    }
1396
1397
    /**
1398
     * Get the sum of the given values.
1399
     *
1400
     * @param  callable|string|null $callback
1401
     * @return mixed
1402
     */
1403
    public function sum($callback = null)
1404
    {
1405
        if (is_null($callback)) {
1406
            return array_sum($this->items);
1407
        }
1408
1409
        $callback = $this->valueRetriever($callback);
0 ignored issues
show
$callback is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1410
1411
        return $this->reduce(function ($result, $item) use ($callback) {
1412
            return $result + $callback($item);
1413
        }, 0);
1414
    }
1415
1416
    /**
1417
     * Take the first or last {$limit} items.
1418
     *
1419
     * @param  int $limit
1420
     * @return static
1421
     */
1422
    public function take($limit)
1423
    {
1424
        if ($limit < 0) {
1425
            return $this->slice($limit, abs($limit));
1426
        }
1427
1428
        return $this->slice(0, $limit);
1429
    }
1430
1431
    /**
1432
     * Pass the collection to the given callback and then return it.
1433
     *
1434
     * @param  callable $callback
1435
     * @return $this
1436
     */
1437
    public function tap(callable $callback)
1438
    {
1439
        $callback(new static($this->items));
1440
1441
        return $this;
1442
    }
1443
1444
    /**
1445
     * Transform each item in the collection using a callback.
1446
     *
1447
     * @param  callable $callback
1448
     * @return $this
1449
     */
1450
    public function transform(callable $callback)
1451
    {
1452
        $this->items = $this->map($callback)->all();
1453
1454
        return $this;
1455
    }
1456
1457
    /**
1458
     * Return only unique items from the collection array.
1459
     *
1460
     * @param  string|callable|null $key
1461
     * @param  bool                 $strict
1462
     * @return static
1463
     */
1464
    public function unique($key = null, $strict = false)
1465
    {
1466
        if (is_null($key)) {
1467
            return new static(array_unique($this->items, SORT_REGULAR));
1468
        }
1469
1470
        $callback = $this->valueRetriever($key);
0 ignored issues
show
$key is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1471
1472
        $exists = [];
1473
1474
        return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) {
1475
            if (in_array($id = $callback($item, $key), $exists, $strict)) {
1476
                return true;
1477
            }
1478
1479
            $exists[] = $id;
1480
        });
1481
    }
1482
1483
    /**
1484
     * Return only unique items from the collection array using strict comparison.
1485
     *
1486
     * @param  string|callable|null $key
1487
     * @return static
1488
     */
1489
    public function uniqueStrict($key = null)
1490
    {
1491
        return $this->unique($key, true);
1492
    }
1493
1494
    /**
1495
     * Reset the keys on the underlying array.
1496
     *
1497
     * @return static
1498
     */
1499
    public function values()
1500
    {
1501
        return new static(array_values($this->items));
1502
    }
1503
1504
    /**
1505
     * Get a value retrieving callback.
1506
     *
1507
     * @param  string $value
1508
     * @return callable
1509
     */
1510
    protected function valueRetriever($value)
1511
    {
1512
        if ($this->useAsCallable($value)) {
1513
            return $value;
1514
        }
1515
1516
        return function ($item) use ($value) {
1517
            return data_get($item, $value);
1518
        };
1519
    }
1520
1521
    /**
1522
     * Zip the collection together with one or more arrays.
1523
     * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
1524
     *      => [[1, 4], [2, 5], [3, 6]]
1525
     *
1526
     * @param  mixed ...$items
1527
     * @return static
1528
     */
1529
    public function zip($items)
0 ignored issues
show
The parameter $items is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1530
    {
1531
        $arrayableItems = array_map(function ($items) {
1532
            return $this->getArrayableItems($items);
1533
        }, func_get_args());
1534
1535
        $params = array_merge([
1536
            function () {
1537
                return new static(func_get_args());
1538
            }, $this->items
1539
        ], $arrayableItems);
1540
1541
        return new static(call_user_func_array('array_map', $params));
1542
    }
1543
1544
    /**
1545
     * Pad collection to the specified length with a value.
1546
     *
1547
     * @param  int   $size
1548
     * @param  mixed $value
1549
     * @return static
1550
     */
1551
    public function pad($size, $value)
1552
    {
1553
        return new static(array_pad($this->items, $size, $value));
1554
    }
1555
1556
    /**
1557
     * Get the collection of items as a plain array.
1558
     *
1559
     * @return array
1560
     */
1561
    public function toArray()
1562
    {
1563
        return array_map(function ($value) {
1564
            return $value instanceof Arrayable ? $value->toArray() : $value;
0 ignored issues
show
The class Childish\support\Arrayable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1565
        }, $this->items);
1566
    }
1567
1568
    /**
1569
     * Convert the object into something JSON serializable.
1570
     *
1571
     * @return array
1572
     */
1573
    public function jsonSerialize()
1574
    {
1575
        return array_map(function ($value) {
1576
            if ($value instanceof JsonSerializable) {
1577
                return $value->jsonSerialize();
1578
            }
1579
            return $value;
1580
        }, $this->items);
1581
    }
1582
1583
    /**
1584
     * Get the collection of items as JSON.
1585
     *
1586
     * @param  int $options
1587
     * @return string
1588
     */
1589
    public function toJson($options = 0)
1590
    {
1591
        return json_encode($this->jsonSerialize(), $options);
1592
    }
1593
1594
    /**
1595
     * Get an iterator for the items.
1596
     *
1597
     * @return \ArrayIterator
1598
     */
1599
    public function getIterator()
1600
    {
1601
        return new ArrayIterator($this->items);
1602
    }
1603
1604
    /**
1605
     * Get a CachingIterator instance.
1606
     *
1607
     * @param  int $flags
1608
     * @return \CachingIterator
1609
     */
1610
    public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
1611
    {
1612
        return new CachingIterator($this->getIterator(), $flags);
1613
    }
1614
1615
    /**
1616
     * Count the number of items in the collection.
1617
     *
1618
     * @return int
1619
     */
1620
    public function count()
1621
    {
1622
        return count($this->items);
1623
    }
1624
1625
    /**
1626
     * Get a base Support collection instance from this collection.
1627
     *
1628
     * @return \Illuminate\Support\Collection
1629
     */
1630
    public function toBase()
1631
    {
1632
        return new self($this);
1633
    }
1634
1635
    /**
1636
     * Determine if an item exists at an offset.
1637
     *
1638
     * @param  mixed $key
1639
     * @return bool
1640
     */
1641
    public function offsetExists($key)
1642
    {
1643
        return array_key_exists($key, $this->items);
1644
    }
1645
1646
    /**
1647
     * Get an item at a given offset.
1648
     *
1649
     * @param  mixed $key
1650
     * @return mixed
1651
     */
1652
    public function offsetGet($key)
1653
    {
1654
        return $this->items[$key];
1655
    }
1656
1657
    /**
1658
     * Set the item at a given offset.
1659
     *
1660
     * @param  mixed $key
1661
     * @param  mixed $value
1662
     * @return void
1663
     */
1664
    public function offsetSet($key, $value)
1665
    {
1666
        if (is_null($key)) {
1667
            $this->items[] = $value;
1668
        } else {
1669
            $this->items[$key] = $value;
1670
        }
1671
    }
1672
1673
    /**
1674
     * Unset the item at a given offset.
1675
     *
1676
     * @param  string $key
1677
     * @return void
1678
     */
1679
    public function offsetUnset($key)
1680
    {
1681
        unset($this->items[$key]);
1682
    }
1683
1684
    /**
1685
     * Convert the collection to its string representation.
1686
     *
1687
     * @return string
1688
     */
1689
    public function __toString()
1690
    {
1691
        return $this->toJson();
1692
    }
1693
1694
    /**
1695
     * Results array of items from Collection or Arrayable.
1696
     *
1697
     * @param  mixed $items
1698
     * @return array
1699
     */
1700
    protected function getArrayableItems($items)
1701
    {
1702
        if (is_array($items)) {
1703
            return $items;
1704
        } else if ($items instanceof self) {
1705
            return $items->all();
1706
        } else if ($items instanceof Arrayable) {
0 ignored issues
show
The class Childish\support\Arrayable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1707
            return $items->toArray();
1708
        } else if ($items instanceof Jsonable) {
0 ignored issues
show
The class Childish\support\Jsonable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1709
            return json_decode($items->toJson(), true);
1710
        } else if ($items instanceof JsonSerializable) {
1711
            return $items->jsonSerialize();
1712
        } else if ($items instanceof Traversable) {
1713
            return iterator_to_array($items);
1714
        }
1715
1716
        return (array)$items;
1717
    }
1718
1719
    /**
1720
     * Add a method to the list of proxied methods.
1721
     *
1722
     * @param  string $method
1723
     * @return void
1724
     */
1725
    public static function proxy($method)
1726
    {
1727
        static::$proxies[] = $method;
1728
    }
1729
}