Improve the code logic
authorAlexander Ebert <ebert@woltlab.com>
Mon, 9 Oct 2023 16:05:53 +0000 (18:05 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 9 Oct 2023 16:05:53 +0000 (18:05 +0200)
wcfsetup/install/files/lib/data/article/category/ArticleCategory.class.php
wcfsetup/install/files/lib/system/label/LabelHandler.class.php
wcfsetup/install/files/lib/system/label/LabelPicker.class.php
wcfsetup/install/files/lib/system/label/LabelPickerGroup.class.php

index 40a4ead2262ad7dcd95489bb2e32518622da73f4..0a6361b84a7c56c4ab8af3676654d48fc856fd8d 100644 (file)
@@ -214,7 +214,7 @@ class ArticleCategory extends AbstractDecoratedCategory implements IAccessibleOb
             }
         }
 
-        return LabelPickerGroup::fromGroupIDs(\array_unique($groupIDs), false);
+        return LabelPickerGroup::fromGroupIDs($groupIDs, false);
     }
 
     /**
index af48316eb1863728a0c2bade5f1d456543bb8bd1..a7219a93676665cd043b55fef7f45b3b3d3469ae 100644 (file)
@@ -428,7 +428,6 @@ class LabelHandler extends SingletonFactory
 
     /**
      * @param int[] $groupIDs
-     * @param bool $invertible
      * @return LabelPicker[]
      * @since 6.0
      */
index 52b19aada085a5d10519918760a4537b8d87f507..fb37ea03d90ba9e80fb6deae3527136095973061 100644 (file)
@@ -8,7 +8,7 @@ use wcf\util\JSON;
 use wcf\util\StringUtil;
 
 /**
- * Provides helper methods to interact with the label group.
+ * Manages the state of a label picker and renders the component.
  *
  * @author Alexander Ebert
  * @copyright 2001-2023 WoltLab GmbH
@@ -117,7 +117,7 @@ final class LabelPicker
         if (!isset($this->elementID)) {
             $this->elementID = \sprintf(
                 '%s_labelGroup%d',
-                \substr(\md5($this->name), 0, 8),
+                \bin2hex(\random_bytes(16)),
                 $this->labelGroup->groupID,
             );
         }
index 273a6daf114c5deb5eb7703deb1ffef73260f3a2..15984d6447651976c93ea93671ee35ca70bb77a5 100644 (file)
@@ -97,26 +97,21 @@ final class LabelPickerGroup implements \Countable, \Iterator
 
     /**
      * Returns an unencoded query string for `labelIDs` for use in the LinkHandler.
-     *
-     * @return string
      */
     public function toUrlQueryString(): string
     {
         return \implode(
             '&',
-            \array_filter(
-                \array_map(static function (LabelPicker $labelPicker) {
-                    if (!$labelPicker->hasSelection()) {
-                        return '';
-                    }
-
-                    return \sprintf(
-                        'labelIDs[%d]=%d',
-                        $labelPicker->labelGroup->groupID,
-                        $labelPicker->getSelectedValue(),
-                    );
-                }, $this->labelPickers)
-            )
+            \array_map(static function (LabelPicker $labelPicker) {
+                return \sprintf(
+                    'labelIDs[%d]=%d',
+                    $labelPicker->labelGroup->groupID,
+                    $labelPicker->getSelectedValue(),
+                );
+            }, \array_filter(
+                $this->labelPickers,
+                static fn (LabelPicker $labelPicker) => $labelPicker->hasSelection()
+            ))
         );
     }
 
@@ -127,11 +122,12 @@ final class LabelPickerGroup implements \Countable, \Iterator
      */
     public function toLabelIDs(): array
     {
-        return \array_filter(
-            \array_map(
-                fn (LabelPicker $labelPicker) => $labelPicker->getSelectedValue(),
-                $this->labelPickers
-            ),
+        return \array_map(
+            static fn (LabelPicker $labelPicker) => $labelPicker->getSelectedValue(),
+            \array_filter(
+                $this->labelPickers,
+                static fn (LabelPicker $labelPicker) => $labelPicker->hasSelection()
+            )
         );
     }
 
@@ -250,7 +246,7 @@ final class LabelPickerGroup implements \Countable, \Iterator
     public static function fromViewableLabelGroups(array $viewableLabelGroups, bool $invertible): self
     {
         $labelPickers = \array_map(
-            fn (ViewableLabelGroup $viewableLabelGroup) => new LabelPicker($viewableLabelGroup, $invertible),
+            static fn (ViewableLabelGroup $viewableLabelGroup) => new LabelPicker($viewableLabelGroup, $invertible),
             $viewableLabelGroups
         );