[8.x] Sort collections by key when first element of sort operation is string (even if callable) #40212
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses #40203.
The documentation for
sortBy()
describes: "Each sort operation should be an array consisting of the attribute that you wish to sort by and the direction of the desired sort. [...] When sorting a collection by multiple attributes, you may also provide closures that define each sort operation."The following example summarizes the issue:
The developer expects the array to be sorted by the
"name"
and"sort"
keys. However, because "sort" is also the name of an existing function (in this case a built-in function), it is considered callable and the framework attempts to callsort()
instead, which can throw an error (thatsort()
was called with invalid parameters)The change in this PR makes it check if the first element in each sort operation is a string in addition to callable. If it's a string, it will continue to be treated as a key even if it is also the name of an existing function. This PR preserves the ability to pass in a string key or a closure (which would not be a string).
This PR may break functionality that relies on being able to pass in a function name as a string instead of a closure. I think that might be okay since using it that way is undocumented (as far as I could tell?)
I'm new to this so please kindly let me know if I should do anything differently here, or if I'm mistaken about anything. Thank you :)