Skip to content

[11.x] Handle Null Check in Str::startsWith and Str::endsWith#54520

Merged
taylorotwell merged 4 commits into
laravel:11.xfrom
onairmarc:11.x
Feb 10, 2025
Merged

[11.x] Handle Null Check in Str::startsWith and Str::endsWith#54520
taylorotwell merged 4 commits into
laravel:11.xfrom
onairmarc:11.x

Conversation

@onairmarc
Copy link
Copy Markdown
Contributor

This PR adds a null check to the $haystack parameter. Null values for this parameter have been deprecated, causing error messages to be logged. If $haystack is null, then Str::startsWith and Str::endsWith will return false.

@shaedrich
Copy link
Copy Markdown
Contributor

Wouldn't it be better to either throw an exception or target master and enforce the argument types in the method signature instead of the PHPDoc?

@onairmarc
Copy link
Copy Markdown
Contributor Author

Wouldn't it be better to either throw an exception or target master and enforce the argument types in the method signature instead of the PHPDoc?

The PHPDoc didn't come from me. I forgot to turn off auto-formatting in my IDE, so those changes are me reverting what my IDE did automatically. I think early returning false is still appropriate, considering the haystack is null, so the haystack string implicitly does not start or end with the needle string.

@taylorotwell taylorotwell merged commit a92644b into laravel:11.x Feb 10, 2025
@vlakoff
Copy link
Copy Markdown
Contributor

vlakoff commented Feb 20, 2025

Related past PRs, for handling $needles being an array containing a null value: #30364 and #37087.

@vlakoff
Copy link
Copy Markdown
Contributor

vlakoff commented Feb 20, 2025

For consistency, this handling of null haystack should be made to contains() as well.

Note: when using the "ignoreCase" mode, make sure that mb_strtolower() doesn't receive the null haystack (return before).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants