Skip to content

Commit 58a0eb3

Browse files
authored
Merge pull request #12874 from nextcloud/fix/psalm-6-null-guards
refactor: add null guards in db mappers and fix JsonResponse return type for Psalm 6
2 parents 4afb37a + 79b3477 commit 58a0eb3

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

lib/Db/LocalMessageMapper.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,19 @@ public function getAllForUser(string $userId, int $type = LocalMessage::TYPE_OUT
7070

7171
$recipientMap = [];
7272
foreach ($recipients as $r) {
73-
$recipientMap[$r->getLocalMessageId()][] = $r;
73+
$localMessageId = $r->getLocalMessageId();
74+
if ($localMessageId === null) {
75+
continue;
76+
}
77+
$recipientMap[$localMessageId][] = $r;
7478
}
7579
$attachmentMap = [];
7680
foreach ($attachments as $a) {
77-
$attachmentMap[$a->getLocalMessageId()][] = $a;
81+
$localMessageId = $a->getLocalMessageId();
82+
if ($localMessageId === null) {
83+
continue;
84+
}
85+
$attachmentMap[$localMessageId][] = $a;
7886
}
7987

8088
return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) {
@@ -147,11 +155,19 @@ public function findDue(int $time, int $type = LocalMessage::TYPE_OUTGOING): arr
147155

148156
$recipientMap = [];
149157
foreach ($recipients as $r) {
150-
$recipientMap[$r->getLocalMessageId()][] = $r;
158+
$localMessageId = $r->getLocalMessageId();
159+
if ($localMessageId === null) {
160+
continue;
161+
}
162+
$recipientMap[$localMessageId][] = $r;
151163
}
152164
$attachmentMap = [];
153165
foreach ($attachments as $a) {
154-
$attachmentMap[$a->getLocalMessageId()][] = $a;
166+
$localMessageId = $a->getLocalMessageId();
167+
if ($localMessageId === null) {
168+
continue;
169+
}
170+
$attachmentMap[$localMessageId][] = $a;
155171
}
156172

157173
return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) {
@@ -196,11 +212,19 @@ public function findDueDrafts(int $time): array {
196212

197213
$recipientMap = [];
198214
foreach ($recipients as $r) {
199-
$recipientMap[$r->getLocalMessageId()][] = $r;
215+
$localMessageId = $r->getLocalMessageId();
216+
if ($localMessageId === null) {
217+
continue;
218+
}
219+
$recipientMap[$localMessageId][] = $r;
200220
}
201221
$attachmentMap = [];
202222
foreach ($attachments as $a) {
203-
$attachmentMap[$a->getLocalMessageId()][] = $a;
223+
$localMessageId = $a->getLocalMessageId();
224+
if ($localMessageId === null) {
225+
continue;
226+
}
227+
$attachmentMap[$localMessageId][] = $a;
204228
}
205229

206230
return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) {
@@ -245,7 +269,7 @@ public function updateWithRecipients(LocalMessage $message, array $to, array $cc
245269
try {
246270
$message = $this->update($message);
247271

248-
$this->recipientMapper->updateRecipients($message->getId(), $message->getRecipients(), $to, $cc, $bcc);
272+
$this->recipientMapper->updateRecipients($message->getId(), $message->getRecipients() ?? [], $to, $cc, $bcc);
249273
$recipients = $this->recipientMapper->findByLocalMessageId($message->getId());
250274
$message->setRecipients($recipients);
251275
$this->db->commit();

lib/Db/MessageMapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ public function updateBulk(Account $account, bool $permflagsEnabled, Message ...
545545
*/
546546
private function updateTags(Account $account, Message $message, array $tags, PerformanceLoggerTask $perf): void {
547547
$imapTags = $message->getTags();
548-
$dbTags = $tags[$message->getMessageId()] ?? [];
548+
$messageId = $message->getMessageId();
549+
$dbTags = $messageId !== null ? ($tags[$messageId] ?? []) : [];
549550

550551
if ($imapTags === [] && $dbTags === []) {
551552
// neither old nor new tags
@@ -1354,7 +1355,8 @@ public function findRelatedData(array $messages, string $userId): array {
13541355
$tags = $this->tagMapper->getAllTagsForMessages($messages, $userId);
13551356
/** @var Message $message */
13561357
$messages = array_map(static function ($message) use ($tags) {
1357-
$message->setTags($tags[$message->getMessageId()] ?? []);
1358+
$messageId = $message->getMessageId();
1359+
$message->setTags($messageId !== null ? ($tags[$messageId] ?? []) : []);
13581360
return $message;
13591361
}, $messages);
13601362
return $messages;

lib/Http/JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static function error(string $message,
107107
* @param Http::STATUS_* $status
108108
* @param array|JsonSerializable|bool|string $data
109109
*
110-
* @return static
110+
* @return self
111111
*/
112112
public static function errorFromThrowable(Throwable $error,
113113
int $status = Http::STATUS_INTERNAL_SERVER_ERROR,

0 commit comments

Comments
 (0)