84631[BUGFIX] Allow integers as sort value in ArrayUtility::sortArraysByKey
Commit message
Review
-
Code review CR +2 Done
- Georg Ringer +2
- Garvin Hicking +1
- Benni Mack +1
-
Verified V +2 Done
- Georg Ringer +2
- Garvin Hicking +1
- Benni Mack +1
- core-ci +1 CI
Issues 1
Files 2
Review log 1518
-
Georg Ringer uploaded patch set 1 06/11/24, 20:50
-
Georg Ringer commented on patch set 1 06/11/24, 20:50
This change is ready for review. -
core-ci voted on patch set 1 V +1 06/11/24, 20:56 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/59252
-
Garvin Hicking commented on patch set 1 06/11/24, 21:11
Oh, thats a good one. I am a bit unsure of side effects when other types like arrays are passed which then get cast as a string; this would suddenly no longer throw an error then (just a waning of 'array to string conversion'. Generally I do agree that sorting by keys having integers would need fixing! But maybe guarded with an canBeInterpretedAsInt()? Or also allow floats as keys? objects (enums)? Paging @typo3-coding@oliverklee.de as our type guru for some feedback 😇 -
Georg Ringer commented on patch set 1 06/11/24, 21:12
Reply
we could check with is_scalar and be safe -
Oliver Klee commented on patch set 1 06/12/24, 07:50
Reply
Having only the cast would allow "all kinds of types that can be cast to string" as array values, including objects and booleans, which will make the behavior hard to understand and hard to predict and would violate the "try to avoid surprises concerning how the code behaves" principle. ;-) Also, to reduce both the cognitive load for understanding the function as well as the amount of tests needed to cover all relevant behavior, I'd strongly suggest keeping this to "string and int", and only add other types (e.g., floats) later when when really, really need this in a real-world usecase. I'd suggest to add very specific PHPDoc type annotations both to the `sortArraysByKey` method as well as the anonymous functions to allow PHPStan (and the human reader) to catch calls with array having values of non-string and non-integer types. And (if we want to be really sure) check in the anonymous function that the values are `is_string` or `is_int` and throw an exception with a helpful error message otherwise. This would allow track down incorrect calls by seeing the exception instead of by seeing "very strange, but only slightly incorrect" behavior of the calling code. -
Oliver Klee commented on patch set 1 06/12/24, 07:52
And just for the record: I think the method name tends to lead to misunderstandings as the method sorts by array values, not by keys, but by values associated with the given key. But that's out of scope for this change here. :-) -
Georg Ringer commented on patch set 1 06/14/24, 11:10
Reply
hey: what about is_scalar, I would also like to sort by booleans which would be casted to "1" and "0" which would be fine. -
Oliver Klee commented on patch set 1 06/14/24, 12:13
Reply Unresolved
I'd suggest to keep it to strings and ints for now (i.e., what the ticket is about). When we have a real-world use case where we need to compare booleans, we can discuss about how to do that. (At the moment, I'd like to keep the complexity of the method small, including the cognitive load required to understand how it works. At the moment, `(bool)true < "hello"` is true, which I don't find intuitive at all. (In general, I find comparing booleans to strings for sorting not intuitive.) -
Georg Ringer uploaded patch set 2
V +106/15/24, 19:39 -
core-ci voted on patch set 2 V -1 06/15/24, 19:45 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/59651
-
Georg Ringer uploaded patch set 3
V -106/15/24, 20:24 -
core-ci voted on patch set 3 V +1 06/15/24, 20:31 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/59653
-
Georg Ringer commented on patch set 3 06/17/24, 20:30
Reply
Done -
Benni Mack voted on patch set 3 CR +1 V +1 06/17/24, 22:35
-
Garvin Hicking voted on patch set 3 CR +1 V +1 06/18/24, 05:49
-
Georg Ringer voted on patch set 3 CR +2 V +2 06/18/24, 05:51
-
Georg Ringer merged patch set 4 06/18/24, 05:51