From 8af9f3790c3c12833aa211a4030e622a932efdb7 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Thu, 24 Sep 2026 17:17:20 +0200 Subject: [PATCH] refactor(Sharing): Simplify sorting Signed-off-by: provokateurin --- lib/unstable/Sharing/Permission/SharePermission.php | 8 ++++++-- lib/unstable/Sharing/Property/ShareProperty.php | 7 ++++++- lib/unstable/Sharing/Recipient/ShareRecipient.php | 10 ++++++++-- lib/unstable/Sharing/Share.php | 9 +++++++-- lib/unstable/Sharing/Source/ShareSource.php | 8 ++++++-- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/unstable/Sharing/Permission/SharePermission.php b/lib/unstable/Sharing/Permission/SharePermission.php index f6cac7e6dea9b..bd760221667b5 100644 --- a/lib/unstable/Sharing/Permission/SharePermission.php +++ b/lib/unstable/Sharing/Permission/SharePermission.php @@ -59,8 +59,12 @@ public function format(ISharingRegistry $registry, IFactory $l10nFactory): array public static function formatMultiple(ISharingRegistry $registry, IFactory $l10nFactory, array $permissions): array { $permissions = array_map(static fn (SharePermission $permission): array => $permission->format($registry, $l10nFactory), $permissions); - // First sort by priority and then sort by class name to get a stable order regardless of the DB order - usort($permissions, static fn (array $a, array $b): int => 2 * ($b['priority'] <=> $a['priority']) + ($a['class'] <=> $b['class'])); + usort( + $permissions, + static fn (array $a, array $b): int + => ($b['priority'] <=> $a['priority']) + ?: ($a['class'] <=> $b['class']), + ); return $permissions; } diff --git a/lib/unstable/Sharing/Property/ShareProperty.php b/lib/unstable/Sharing/Property/ShareProperty.php index 388bb0758fe18..2894424b97f02 100644 --- a/lib/unstable/Sharing/Property/ShareProperty.php +++ b/lib/unstable/Sharing/Property/ShareProperty.php @@ -64,7 +64,12 @@ public static function formatMultiple(ISharingRegistry $registry, IFactory $l10n $properties = array_map(fn (ShareProperty $property): array => $property->format($registry, $l10nFactory, $share), $properties); // First sort by priority and then sort by class name to get a stable order regardless of the DB order - usort($properties, static fn (array $a, array $b): int => 2 * ($b['priority'] <=> $a['priority']) + ($a['class'] <=> $b['class'])); + usort( + $properties, + static fn (array $a, array $b): int + => ($b['priority'] <=> $a['priority']) + ?: ($a['class'] <=> $b['class']), + ); return $properties; } diff --git a/lib/unstable/Sharing/Recipient/ShareRecipient.php b/lib/unstable/Sharing/Recipient/ShareRecipient.php index 7f3aa46c1b69c..0c3541226045e 100644 --- a/lib/unstable/Sharing/Recipient/ShareRecipient.php +++ b/lib/unstable/Sharing/Recipient/ShareRecipient.php @@ -126,8 +126,14 @@ public static function formatMultiple(ISharingRegistry $registry, IFactory $l10n ++$recipientDisplayNames[$displayName]; } - // First sort by least amount of disabled permissions, then by instance, then by class and finally by value to get a stable order regardless of the DB order - usort($recipients, static fn (ShareRecipient $a, ShareRecipient $b): int => 8 * (count($a->getDisabledPermissions()) <=> count($b->getDisabledPermissions())) + 4 * ($a->instance === null ? -1 : ($a->instance <=> $b->instance)) + 2 * ($a->class <=> $b->class) + ($a->value <=> $b->value)); + usort( + $recipients, + static fn (ShareRecipient $a, ShareRecipient $b): int + => (count($a->getDisabledPermissions()) <=> count($b->getDisabledPermissions())) + ?: ($a->instance === null ? -1 : ($a->instance <=> $b->instance)) + ?: ($a->class <=> $b->class) + ?: ($a->value <=> $b->value), + ); return array_map(static fn (ShareRecipient $recipient): array => $recipient->format($registry, $l10nFactory, $urlGenerator, $userManager, $recipientDisplayNames[$recipientTypes[$recipient->class]?->getRecipientDisplayName($recipient->value) ?? $recipient->value] === 1), $recipients); } diff --git a/lib/unstable/Sharing/Share.php b/lib/unstable/Sharing/Share.php index bf446e321a741..55a11f6e15710 100644 --- a/lib/unstable/Sharing/Share.php +++ b/lib/unstable/Sharing/Share.php @@ -321,8 +321,13 @@ public function format(ISharingRegistry $registry, IFactory $l10nFactory, IURLGe * @experimental 35.0.0 */ public static function formatMultiple(ISharingRegistry $registry, IFactory $l10nFactory, IURLGenerator $urlGenerator, IUserManager $userManager, ShareAccessContext $accessContext, array $shares): array { - // First sort by number of enabled permissions and then sort by share id to get a stable order regardless of the DB order - usort($shares, static fn (Share $a, Share $b): int => 2 * (count($b->getEffectiveEnabledPermissions($accessContext)) <=> count($a->getEffectiveEnabledPermissions($accessContext))) + ($a->id <=> $b->id)); + usort( + $shares, + static fn (Share $a, Share $b): int + => (count($b->getEffectiveEnabledPermissions($accessContext)) <=> count($a->getEffectiveEnabledPermissions($accessContext))) + ?: ($a->id <=> $b->id), + ); + return array_map(static fn (Share $share): array => $share->format($registry, $l10nFactory, $urlGenerator, $userManager, $accessContext), $shares); } } diff --git a/lib/unstable/Sharing/Source/ShareSource.php b/lib/unstable/Sharing/Source/ShareSource.php index 174dbfae51ccc..59c0a6bb97890 100644 --- a/lib/unstable/Sharing/Source/ShareSource.php +++ b/lib/unstable/Sharing/Source/ShareSource.php @@ -81,8 +81,12 @@ public static function formatMultiple(ISharingRegistry $registry, IFactory $l10n ++$sourceDisplayNames[$displayName]; } - // First sort by class and then sort by value to get a stable order regardless of the DB order - usort($sources, static fn (ShareSource $a, ShareSource $b): int => 2 * ($a->class <=> $b->class) + ($a->value <=> $b->value)); + usort( + $sources, + static fn (ShareSource $a, ShareSource $b): int + => ($a->class <=> $b->class) + ?: ($a->value <=> $b->value), + ); return array_map(static fn (ShareSource $source): array => $source->format($registry, $l10nFactory, $sourceDisplayNames[$source->getMetadata($registry)->getDisplayName()] === 1), $sources); }