Hydrate the parameters using a readonly struct
authorAlexander Ebert <ebert@woltlab.com>
Sun, 10 Mar 2024 14:19:02 +0000 (15:19 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 10 Mar 2024 14:19:39 +0000 (15:19 +0100)
wcfsetup/install/files/lib/action/EditorGetMentionSuggestionsAction.class.php
wcfsetup/install/files/lib/system/endpoint/controller/core/messages/MentionSuggestions.class.php

index ffdd20b6bc86a3c9163fd1ec808bb218f5c03ae2..5f04b4a6d65cf229626249d1d5b3680a373abfe8 100644 (file)
@@ -7,7 +7,6 @@ use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 use wcf\http\Helper;
 use wcf\system\endpoint\controller\core\messages\MentionSuggestions;
-use wcf\system\endpoint\Parameters;
 
 /**
  * Suggests users that may be mentioned.
@@ -29,13 +28,13 @@ final class EditorGetMentionSuggestionsAction implements RequestHandlerInterface
         \assert(\count($parameters) === 1);
         \assert($parameters[0]->getName() === 'parameters');
 
-        $attribute = current($parameters[0]->getAttributes(Parameters::class));
+        $type = $parameters[0]->getType();
 
-        \assert($attribute !== false);
+        \assert($type instanceof \ReflectionNamedType);
 
         $parameters = Helper::mapQueryParameters(
             $request->getQueryParams(),
-            $attribute->newInstance()->arrayShape,
+            $type->getName(),
         );
 
         return (new MentionSuggestions())->mentionSuggestions($parameters);
index af17c295fd59fde56adeb8d63feaffdcca8ae070..1ed792b5f992268535e77a11e8fb9190de7a94f9 100644 (file)
@@ -15,16 +15,9 @@ final class MentionSuggestions implements IController
 {
     #[GetRequest('/core/messages/mentionsuggestions')]
     public function mentionSuggestions(
-        #[Parameters(
-            <<<'EOT'
-                array {
-                    query: non-empty-string
-                }
-                EOT,
-        )]
-        array $parameters
+        #[Parameters] MentionSuggestionsParameters $parameters
     ): ResponseInterface {
-        $query = \mb_strtolower($parameters['query']);
+        $query = \mb_strtolower($parameters->query);
         $matches = [];
 
         foreach ($this->getGroups($query) as $userGroup) {
@@ -92,3 +85,13 @@ final class MentionSuggestions implements IController
         return $userGroups;
     }
 }
+
+/** @internal */
+final class MentionSuggestionsParameters
+{
+    public function __construct(
+        /** @var non-empty-string */
+        public readonly string $query,
+    ) {
+    }
+}