From 19af1aff010dc4d73463f5c52b6474bbf8583c8e Mon Sep 17 00:00:00 2001 From: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com> Date: Tue, 5 May 2026 13:40:20 +0200 Subject: [PATCH] refactor: add null guards in db mappers and fix JsonResponse return type for Psalm 6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LocalMessageMapper: guard getLocalMessageId() before using as array key in findAll, findDue, findDueDrafts; use ?? [] for getRecipients() - MessageMapper: extract getMessageId() to variable with null guard before using as array key in updateTags and findRelatedData - JsonResponse: align @return static → @return self on errorFromThrowable to match actual return type hint AI-assisted: Claude Code (claude-sonnet-4-6) Signed-off-by: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com> --- lib/Db/LocalMessageMapper.php | 38 ++++++++++++++++++++++++++++------- lib/Db/MessageMapper.php | 6 ++++-- lib/Http/JsonResponse.php | 2 +- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/Db/LocalMessageMapper.php b/lib/Db/LocalMessageMapper.php index 06ef03d023..0be085ce63 100644 --- a/lib/Db/LocalMessageMapper.php +++ b/lib/Db/LocalMessageMapper.php @@ -70,11 +70,19 @@ public function getAllForUser(string $userId, int $type = LocalMessage::TYPE_OUT $recipientMap = []; foreach ($recipients as $r) { - $recipientMap[$r->getLocalMessageId()][] = $r; + $localMessageId = $r->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $recipientMap[$localMessageId][] = $r; } $attachmentMap = []; foreach ($attachments as $a) { - $attachmentMap[$a->getLocalMessageId()][] = $a; + $localMessageId = $a->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $attachmentMap[$localMessageId][] = $a; } return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) { @@ -134,11 +142,19 @@ public function findDue(int $time, int $type = LocalMessage::TYPE_OUTGOING): arr $recipientMap = []; foreach ($recipients as $r) { - $recipientMap[$r->getLocalMessageId()][] = $r; + $localMessageId = $r->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $recipientMap[$localMessageId][] = $r; } $attachmentMap = []; foreach ($attachments as $a) { - $attachmentMap[$a->getLocalMessageId()][] = $a; + $localMessageId = $a->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $attachmentMap[$localMessageId][] = $a; } return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) { @@ -183,11 +199,19 @@ public function findDueDrafts(int $time): array { $recipientMap = []; foreach ($recipients as $r) { - $recipientMap[$r->getLocalMessageId()][] = $r; + $localMessageId = $r->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $recipientMap[$localMessageId][] = $r; } $attachmentMap = []; foreach ($attachments as $a) { - $attachmentMap[$a->getLocalMessageId()][] = $a; + $localMessageId = $a->getLocalMessageId(); + if ($localMessageId === null) { + continue; + } + $attachmentMap[$localMessageId][] = $a; } return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) { @@ -232,7 +256,7 @@ public function updateWithRecipients(LocalMessage $message, array $to, array $cc try { $message = $this->update($message); - $this->recipientMapper->updateRecipients($message->getId(), $message->getRecipients(), $to, $cc, $bcc); + $this->recipientMapper->updateRecipients($message->getId(), $message->getRecipients() ?? [], $to, $cc, $bcc); $recipients = $this->recipientMapper->findByLocalMessageId($message->getId()); $message->setRecipients($recipients); $this->db->commit(); diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index ce29c480ed..3a4e253207 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -531,7 +531,8 @@ public function updateBulk(Account $account, bool $permflagsEnabled, Message ... */ private function updateTags(Account $account, Message $message, array $tags, PerformanceLoggerTask $perf): void { $imapTags = $message->getTags(); - $dbTags = $tags[$message->getMessageId()] ?? []; + $messageId = $message->getMessageId(); + $dbTags = $messageId !== null ? ($tags[$messageId] ?? []) : []; if ($imapTags === [] && $dbTags === []) { // neither old nor new tags @@ -1340,7 +1341,8 @@ public function findRelatedData(array $messages, string $userId): array { $tags = $this->tagMapper->getAllTagsForMessages($messages, $userId); /** @var Message $message */ $messages = array_map(static function ($message) use ($tags) { - $message->setTags($tags[$message->getMessageId()] ?? []); + $messageId = $message->getMessageId(); + $message->setTags($messageId !== null ? ($tags[$messageId] ?? []) : []); return $message; }, $messages); return $messages; diff --git a/lib/Http/JsonResponse.php b/lib/Http/JsonResponse.php index e1a0300a6b..6d3b8143de 100644 --- a/lib/Http/JsonResponse.php +++ b/lib/Http/JsonResponse.php @@ -107,7 +107,7 @@ public static function error(string $message, * @param Http::STATUS_* $status * @param array|JsonSerializable|bool|string $data * - * @return static + * @return self */ public static function errorFromThrowable(Throwable $error, int $status = Http::STATUS_INTERNAL_SERVER_ERROR,