Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Feb 10, 2025

Conversation

onairmarc
Copy link
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
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
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
44 checks passed
@vlakoff
Copy link
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
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