From 9adab4506d4e915e5d25585b87eb71179f693116 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Mon, 1 Sep 2025 13:58:34 -0300 Subject: [PATCH 1/2] prevent infinite loop on array_first, array_last, str_contains --- src/helpers.php | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 21e3342..7acd32d 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -83,7 +83,21 @@ function array_except($array, $keys) */ function array_first($array, ?callable $callback = null, $default = null) { - return Arr::first($array, $callback, $default); + if (is_null($callback)) { + if (empty($array)) { + return value($default); + } + + foreach ($array as $item) { + return $item; + } + + return value($default); + } + + $key = array_find_key($array, $callback); + + return $key !== null ? $array[$key] : value($default); } } @@ -155,7 +169,11 @@ function array_has($array, $keys) */ function array_last($array, ?callable $callback = null, $default = null) { - return Arr::last($array, $callback, $default); + if (is_null($callback)) { + return empty($array) ? value($default) : end($array); + } + + return Arr::first(array_reverse($array, true), $callback, $default); } } @@ -409,7 +427,21 @@ function str_before($subject, $search) */ function str_contains($haystack, $needles) { - return Str::contains($haystack, $needles); + if (is_null($haystack)) { + return false; + } + + if (! is_iterable($needles)) { + $needles = (array) $needles; + } + + foreach ($needles as $needle) { + if ($needle !== '' && strpos($haystack, $needle) !== false) { + return true; + } + } + + return false; } } From 2c5e53009d6b77d3385cfdd0c1a38a4b44b87c8f Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Mon, 1 Sep 2025 14:24:20 -0300 Subject: [PATCH 2/2] add deprecation notes --- src/helpers.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index 7acd32d..f2a8269 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -76,6 +76,9 @@ function array_except($array, $keys) /** * Return the first element in an array passing a given truth test. * + * @deprecated A native function with the same name was introduced in PHP 8.5. + * Use Illuminate\Support\Arr::first to keep functionality + * * @param array $array * @param callable|null $callback * @param mixed $default @@ -162,6 +165,9 @@ function array_has($array, $keys) /** * Return the last element in an array passing a given truth test. * + * @deprecated A native function with the same name was introduced in PHP 8.5. + * Use Illuminate\Support\Arr::last to keep functionality + * * @param array $array * @param callable|null $callback * @param mixed $default @@ -421,6 +427,9 @@ function str_before($subject, $search) /** * Determine if a given string contains a given substring. * + * @deprecated A native function with the same name was introduced in PHP 8.0. + * Use Illuminate\Support\Str::contains to keep functionality + * * @param string $haystack * @param string|array $needles * @return bool