1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Collections\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Utility\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait FilterTrait |
9
|
|
|
* @package Nip\Collections\Traits |
10
|
|
|
*/ |
11
|
|
|
trait FilterTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Run a filter over each of the items. |
15
|
|
|
* |
16
|
|
|
* @param callable|null $callback |
17
|
|
|
* @return static |
18
|
|
|
*/ |
19
|
1 |
|
public function filter(callable $callback = null) |
20
|
|
|
{ |
21
|
1 |
|
if ($callback) { |
22
|
1 |
|
return new static(Arr::where($this->items, $callback)); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
return new static(array_filter($this->items)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the first item from the collection passing the given truth test. |
30
|
|
|
* |
31
|
|
|
* @param callable|null $callback |
32
|
|
|
* @param mixed $default |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
1 |
|
public function first(callable $callback = null, $default = null) |
36
|
|
|
{ |
37
|
1 |
|
return Arr::first($this->items, $callback, $default); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the items with the specified keys. |
42
|
|
|
* |
43
|
|
|
* @param mixed $keys |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
1 |
|
public function only($keys) |
47
|
|
|
{ |
48
|
1 |
|
if (is_null($keys)) { |
49
|
1 |
|
return new static($this->items); |
|
|
|
|
50
|
|
|
} |
51
|
1 |
|
$keys = is_array($keys) ? $keys : func_get_args(); |
52
|
1 |
|
return new static(Arr::only($this->items, $keys)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Splice a portion of the underlying collection array. |
57
|
|
|
* |
58
|
|
|
* @param int $offset |
59
|
|
|
* @param int|null $length |
60
|
|
|
* @param mixed $replacement |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
1 |
|
public function splice($offset, $length = null, $replacement = []) |
64
|
|
|
{ |
65
|
1 |
|
if (func_num_args() === 1) { |
66
|
|
|
return new static(array_splice($this->items, $offset)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return new static(array_splice($this->items, $offset, $length, $replacement)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.