Skip to content

Commit f8478f8

Browse files
authored
[12.x] Handle Null Check in Str::contains (#55991)
* [12.x] Handle Null Check in Str::contains This PR adds a null check to the $haystack parameter. Null values for this parameter have been deprecated. * [12.x] Micro-optimize Str::startsWith and Str::endsWith Return earlier if $haystack is null
1 parent 959fac8 commit f8478f8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/Illuminate/Support/Str.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ public static function chopEnd($subject, $needle)
297297
*/
298298
public static function contains($haystack, $needles, $ignoreCase = false)
299299
{
300+
if (is_null($haystack)) {
301+
return false;
302+
}
303+
300304
if ($ignoreCase) {
301305
$haystack = mb_strtolower($haystack);
302306
}
@@ -384,14 +388,14 @@ public static function deduplicate(string $string, string $character = ' ')
384388
*/
385389
public static function endsWith($haystack, $needles)
386390
{
387-
if (! is_iterable($needles)) {
388-
$needles = (array) $needles;
389-
}
390-
391391
if (is_null($haystack)) {
392392
return false;
393393
}
394394

395+
if (! is_iterable($needles)) {
396+
$needles = (array) $needles;
397+
}
398+
395399
foreach ($needles as $needle) {
396400
if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {
397401
return true;
@@ -1638,14 +1642,14 @@ public static function squish($value)
16381642
*/
16391643
public static function startsWith($haystack, $needles)
16401644
{
1641-
if (! is_iterable($needles)) {
1642-
$needles = [$needles];
1643-
}
1644-
16451645
if (is_null($haystack)) {
16461646
return false;
16471647
}
16481648

1649+
if (! is_iterable($needles)) {
1650+
$needles = [$needles];
1651+
}
1652+
16491653
foreach ($needles as $needle) {
16501654
if ((string) $needle !== '' && str_starts_with($haystack, $needle)) {
16511655
return true;

0 commit comments

Comments
 (0)