From ae45a77cd14cc72aab13aed15e0c720f8e9344e1 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 27 Oct 2026 09:24:29 -0400 Subject: [PATCH 1/2] refactor(comments): rely on stable usort PHP 8 guarantees stable sorting, so remove the original-position tie-breaker used to preserve order for ties. Signed-off-by: Josh --- .../lib/Collaboration/CommentersSorter.php | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/apps/comments/lib/Collaboration/CommentersSorter.php b/apps/comments/lib/Collaboration/CommentersSorter.php index 3956423a50b0a..8d40ce30f43e7 100644 --- a/apps/comments/lib/Collaboration/CommentersSorter.php +++ b/apps/comments/lib/Collaboration/CommentersSorter.php @@ -22,11 +22,8 @@ public function getId(): string { } /** - * Sorts people who commented on the given item atop (descelating) of the - * others - * - * @param array &$sortArray - * @param array $context + * Sorts people who commented on the given item ahead of others, in + * descending order */ #[\Override] public function sort(array &$sortArray, array $context): void { @@ -35,7 +32,7 @@ public function sort(array &$sortArray, array $context): void { } $commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']); - if (count($commenters) === 0) { + if ($commenters === []) { return; } @@ -44,23 +41,9 @@ public function sort(array &$sortArray, array $context): void { continue; } - // at least on PHP 5.6 usort turned out to be not stable. So we add - // the current index to the value and compare it on a draw - $i = 0; - $workArray = array_map(function ($element) use (&$i) { - return [$i++, $element]; - }, $byType); - - usort($workArray, function ($a, $b) use ($commenters, $type) { - $r = $this->compare($a[1], $b[1], $commenters[$type]); - if ($r === 0) { - $r = $a[0] - $b[0]; - } - return $r; + usort($byType, function ($a, $b) use ($commenters, $type) { + return $this->compare($a, $b, $commenters[$type]); }); - - // and remove the index values again - $byType = array_column($workArray, 1); } } From e645ffe4d11161fd41cd36c60a16fbf287446763 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 27 Oct 2026 09:30:42 -0400 Subject: [PATCH 2/2] refactor(files_sharing): rely on stable usort Remove the original-position tie-breaker workaround. PHP 8+ preserves the input order of equal elements, so sorting can operate directly on recipient arrays. Also add return typing. Signed-off-by: Josh --- .../Collaboration/ShareRecipientSorter.php | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php index 9395b0600c69d..207b09a99968b 100644 --- a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php +++ b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php @@ -28,7 +28,7 @@ public function getId(): string { } #[\Override] - public function sort(array &$sortArray, array $context) { + public function sort(array &$sortArray, array $context): void { // let's be tolerant. Comments uses "files" by default, other usages are often singular if ($context['itemType'] !== 'files' && $context['itemType'] !== 'file') { return; @@ -50,33 +50,13 @@ public function sort(array &$sortArray, array $context) { continue; } - // at least on PHP 5.6 usort turned out to be not stable. So we add - // the current index to the value and compare it on a draw - $i = 0; - $workArray = array_map(function ($element) use (&$i) { - return [$i++, $element]; - }, $byType); - - usort($workArray, function ($a, $b) use ($al, $type) { - $result = $this->compare($a[1], $b[1], $al[$type]); - if ($result === 0) { - $result = $a[0] - $b[0]; - } - return $result; + usort($byType, function ($a, $b) use ($al, $type) { + return $this->compare($a, $b, $al[$type]); }); - - // and remove the index values again - $byType = array_column($workArray, 1); } } - /** - * @param array $a - * @param array $b - * @param array $al - * @return int - */ - protected function compare(array $a, array $b, array $al) { + protected function compare(array $a, array $b, array $al): int { $a = $a['value']['shareWith']; $b = $b['value']['shareWith'];