Skip to content

Commit 9a6419c

Browse files
authored
Optimize Collection::containsStrict (#44970)
1 parent d085c0b commit 9a6419c

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ public function contains($key, $operator = null, $value = null)
183183
return $this->contains($this->operatorForWhere(...func_get_args()));
184184
}
185185

186+
/**
187+
* Determine if an item exists, using strict comparison.
188+
*
189+
* @param (callable(TValue): bool)|TValue|array-key $key
190+
* @param TValue|null $value
191+
* @return bool
192+
*/
193+
public function containsStrict($key, $value = null)
194+
{
195+
if (func_num_args() === 2) {
196+
return $this->contains(fn ($item) => data_get($item, $key) === $value);
197+
}
198+
199+
if ($this->useAsCallable($key)) {
200+
return ! is_null($this->first($key));
201+
}
202+
203+
return in_array($key, $this->items, true);
204+
}
205+
186206
/**
187207
* Determine if an item is not contained in the collection.
188208
*

src/Illuminate/Collections/LazyCollection.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ public function contains($key, $operator = null, $value = null)
236236
return $this->contains($this->operatorForWhere(...func_get_args()));
237237
}
238238

239+
/**
240+
* Determine if an item exists, using strict comparison.
241+
*
242+
* @param (callable(TValue): bool)|TValue|array-key $key
243+
* @param TValue|null $value
244+
* @return bool
245+
*/
246+
public function containsStrict($key, $value = null)
247+
{
248+
if (func_num_args() === 2) {
249+
return $this->contains(fn ($item) => data_get($item, $key) === $value);
250+
}
251+
252+
if ($this->useAsCallable($key)) {
253+
return ! is_null($this->first($key));
254+
}
255+
256+
foreach ($this as $item) {
257+
if ($item === $key) {
258+
return true;
259+
}
260+
}
261+
262+
return false;
263+
}
264+
239265
/**
240266
* Determine if an item is not contained in the enumerable.
241267
*

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,6 @@ public function some($key, $operator = null, $value = null)
195195
return $this->contains(...func_get_args());
196196
}
197197

198-
/**
199-
* Determine if an item exists, using strict comparison.
200-
*
201-
* @param (callable(TValue): bool)|TValue|array-key $key
202-
* @param TValue|null $value
203-
* @return bool
204-
*/
205-
public function containsStrict($key, $value = null)
206-
{
207-
if (func_num_args() === 2) {
208-
return $this->contains(fn ($item) => data_get($item, $key) === $value);
209-
}
210-
211-
if ($this->useAsCallable($key)) {
212-
return ! is_null($this->first($key));
213-
}
214-
215-
foreach ($this as $item) {
216-
if ($item === $key) {
217-
return true;
218-
}
219-
}
220-
221-
return false;
222-
}
223-
224198
/**
225199
* Dump the items and end the script.
226200
*

0 commit comments

Comments
 (0)