Skip to content

Commit 5518eee

Browse files
committed
refactor: optimize
1 parent dfcd74d commit 5518eee

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/Underscore.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Underscore implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS
1616
*/
1717
public function __construct($data = [])
1818
{
19-
$this->data = is_array($data) ? $data : Helper::asArray($data);
19+
$this->data = \is_array($data) ? $data : Helper::asArray($data);
2020
}
2121

2222
/**
@@ -62,7 +62,7 @@ public function collect(callable $fn)
6262

6363
public function reduce(callable $fn, $memo)
6464
{
65-
return array_reduce($this->data, $fn, $memo);
65+
return \array_reduce($this->data, $fn, $memo);
6666
}
6767

6868
public function foldl(callable $fn, $memo)
@@ -77,7 +77,7 @@ public function inject(callable $fn, $memo)
7777

7878
public function reduceRight(callable $fn, $memo)
7979
{
80-
return array_reduce(array_reverse($this->data), $fn, $memo);
80+
return \array_reduce(\array_reverse($this->data), $fn, $memo);
8181
}
8282

8383
public function foldr(callable $fn, $memo)
@@ -101,7 +101,7 @@ public function detect(callable $fn)
101101

102102
public function filter(callable $fn)
103103
{
104-
$data = array_filter($this->data, $fn, \ARRAY_FILTER_USE_BOTH);
104+
$data = \array_filter($this->data, $fn, \ARRAY_FILTER_USE_BOTH);
105105

106106
return new static($data);
107107
}
@@ -113,15 +113,15 @@ public function select(callable $fn)
113113

114114
public function reject(callable $fn)
115115
{
116-
$data = array_filter($this->data, $this->negate($fn), \ARRAY_FILTER_USE_BOTH);
116+
$data = \array_filter($this->data, $this->negate($fn), \ARRAY_FILTER_USE_BOTH);
117117

118118
return new static($data);
119119
}
120120

121121
protected function negate(callable $fn)
122122
{
123123
return function () use ($fn) {
124-
return !call_user_func_array($fn, func_get_args());
124+
return !\call_user_func_array($fn, \func_get_args());
125125
};
126126
}
127127

@@ -148,12 +148,8 @@ public function any(callable $fn)
148148
protected function match(callable $fn, $all = true)
149149
{
150150
foreach ($this->data as $index => $value) {
151-
if ($all && !$fn($value, $index)) {
152-
return false;
153-
}
154-
155-
if (!$all && $fn($value, $index)) {
156-
return true;
151+
if ($all ^ $fn($value, $index)) {
152+
return !$all;
157153
}
158154
}
159155

0 commit comments

Comments
 (0)