Skip to content

Commit 9adab45

Browse files
committed
prevent infinite loop on array_first, array_last, str_contains
1 parent bc28464 commit 9adab45

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/helpers.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,21 @@ function array_except($array, $keys)
8383
*/
8484
function array_first($array, ?callable $callback = null, $default = null)
8585
{
86-
return Arr::first($array, $callback, $default);
86+
if (is_null($callback)) {
87+
if (empty($array)) {
88+
return value($default);
89+
}
90+
91+
foreach ($array as $item) {
92+
return $item;
93+
}
94+
95+
return value($default);
96+
}
97+
98+
$key = array_find_key($array, $callback);
99+
100+
return $key !== null ? $array[$key] : value($default);
87101
}
88102
}
89103

@@ -155,7 +169,11 @@ function array_has($array, $keys)
155169
*/
156170
function array_last($array, ?callable $callback = null, $default = null)
157171
{
158-
return Arr::last($array, $callback, $default);
172+
if (is_null($callback)) {
173+
return empty($array) ? value($default) : end($array);
174+
}
175+
176+
return Arr::first(array_reverse($array, true), $callback, $default);
159177
}
160178
}
161179

@@ -409,7 +427,21 @@ function str_before($subject, $search)
409427
*/
410428
function str_contains($haystack, $needles)
411429
{
412-
return Str::contains($haystack, $needles);
430+
if (is_null($haystack)) {
431+
return false;
432+
}
433+
434+
if (! is_iterable($needles)) {
435+
$needles = (array) $needles;
436+
}
437+
438+
foreach ($needles as $needle) {
439+
if ($needle !== '' && strpos($haystack, $needle) !== false) {
440+
return true;
441+
}
442+
}
443+
444+
return false;
413445
}
414446
}
415447

0 commit comments

Comments
 (0)