1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Collections\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Collections\CollectionInterface; |
6
|
|
|
use Nip\Utility\Arr; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait FilterTrait |
10
|
|
|
* @package Nip\Collections\Traits |
11
|
|
|
*/ |
12
|
|
|
trait FilterTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Run a filter over each of the items. |
16
|
|
|
* |
17
|
|
|
* @param callable|null $callback |
18
|
|
|
* @return static |
19
|
1 |
|
*/ |
20
|
|
|
public function filter(callable $callback = null): CollectionInterface |
21
|
1 |
|
{ |
22
|
1 |
|
if ($callback) { |
23
|
|
|
return new static(Arr::where($this->items, $callback)); |
|
|
|
|
24
|
|
|
} |
25
|
1 |
|
|
26
|
|
|
return new static(array_filter($this->items)); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the first item from the collection passing the given truth test. |
31
|
|
|
* |
32
|
|
|
* @param callable|null $callback |
33
|
|
|
* @param mixed $default |
34
|
|
|
* @return mixed |
35
|
1 |
|
*/ |
36
|
|
|
public function first(callable $callback = null, $default = null) |
37
|
1 |
|
{ |
38
|
|
|
return Arr::first($this->items, $callback, $default); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the last item from the collection. |
43
|
|
|
* |
44
|
|
|
* @param callable|null $callback |
45
|
|
|
* @param mixed $default |
46
|
1 |
|
* @return mixed |
47
|
|
|
*/ |
48
|
1 |
|
public function last(callable $callback = null, $default = null) |
49
|
1 |
|
{ |
50
|
|
|
return Arr::last($this->items, $callback, $default); |
51
|
1 |
|
} |
52
|
1 |
|
|
53
|
|
|
/** |
54
|
|
|
* Get the items with the specified keys. |
55
|
|
|
* |
56
|
|
|
* @param mixed $keys |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
|
|
public function only($keys) |
60
|
|
|
{ |
61
|
|
|
if (is_null($keys)) { |
62
|
|
|
return new static($this->items); |
|
|
|
|
63
|
1 |
|
} |
64
|
|
|
$keys = is_array($keys) ? $keys : func_get_args(); |
65
|
1 |
|
return new static(Arr::only($this->items, $keys)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
1 |
|
* Splice a portion of the underlying collection array. |
70
|
|
|
* |
71
|
|
|
* @param int $offset |
72
|
|
|
* @param int|null $length |
73
|
|
|
* @param mixed $replacement |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
|
public function splice($offset, $length = null, $replacement = []) |
77
|
|
|
{ |
78
|
|
|
if (func_num_args() === 1) { |
79
|
|
|
return new static(array_splice($this->items, $offset)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new static(array_splice($this->items, $offset, $length, $replacement)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.