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] SORT_NATURAL on Collection no longer throws warning for nulls #52557

Merged

Conversation

Chaplinski
Copy link
Contributor

Currently, when using SORT_NATURAL on a Collection, any null values passed in throw a warning:

strnatcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /vendor/laravel/framework/src/Illuminate/Collections/Collection.php on line 1480

OR

strnatcmp(): Passing null to parameter #2 ($string2) of type string is deprecated in /vendor/laravel/framework/src/Illuminate/Collections/Collection.php on line 1480

It's entirely valid and common for null values to get compared when using sortBy() and passing in an array of properties to sort by. Passing in multiple properties in an array is what triggers the sortByMany() function.

Example:

$book
->authors()
->sortBy([
        ['LastName', 'desc'],
        ['FirstName', 'desc'],
        ['MiddleName', 'desc'],
    ], SORT_NATURAL);

Not all authors will have a value for middle name. Receiving a warning for every missing middle name every time you sort authors is too much noise.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@Chaplinski Chaplinski force-pushed the bug/sort-natural-nulls-cast-to-string branch from f332d52 to 48e55da Compare August 23, 2024 18:12
@Chaplinski Chaplinski changed the base branch from 10.x to 11.x August 23, 2024 18:12
@Chaplinski Chaplinski changed the title SORT_NATURAL on Collection no longer throws warning for nulls [11.x] SORT_NATURAL on Collection no longer throws warning for nulls Aug 23, 2024
@Chaplinski Chaplinski marked this pull request as ready for review August 23, 2024 18:19
@taylorotwell taylorotwell merged commit d16ce25 into laravel:11.x Aug 23, 2024
29 checks passed
@vlakoff
Copy link
Contributor

vlakoff commented Aug 25, 2024

strnatcasecmp() (line 1556), strcasecmp() (line 1558), strcmp() (line 1563) and strcoll() (line 1565) raise the same deprecation notice, so why not applying the same fix for them?

@Chaplinski
Copy link
Contributor Author

strnatcasecmp() (line 1556), strcasecmp() (line 1558), strcmp() (line 1563) and strcoll() (line 1565) raise the same deprecation notice, so why not applying the same fix for them?

Good question. I was initially trying to fix a warning that had been plaguing my team for a while, but I can look into these other warnings as well.

@vlakoff
Copy link
Contributor

vlakoff commented Aug 26, 2024

Refs this question on Stack Overflow, some of the answers are interesting: Migration to PHP 8.1 - how to fix Deprecated Passing null to parameter error - rename build in functions

In the case at hand, ?? '' might be considered instead. It would make understand that the point is not to pass exclusively strings, but to avoid passing null specifically.

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.

3 participants