Skip to content

Commit 15b6f26

Browse files
feat: introduce FileStatus enum for type-safe status handling
Add FileStatus backed enum to provide compile-time type safety for file status values. This replaces the previous int-based approach with a proper enum that: - Eliminates the need for default fallback in match expressions - Provides IDE autocomplete for all valid status values - Throws ValueError for invalid status codes instead of silently returning unknown status - Accepts both int and FileStatus for backward compatibility Updated getTextOfStatus() to use FileStatus enum with automatic conversion from int via FileStatus::from(). Removed null return type as status column is NOT NULL in database, ensuring statusText is always a string. This improves code maintainability and prevents invalid status values from being used throughout the codebase. Signed-off-by: Vitor Mattos <[email protected]>
1 parent ed27dc3 commit 15b6f26

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

lib/Db/FileMapper.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace OCA\Libresign\Db;
1010

11+
use OCA\Libresign\Enum\FileStatus;
1112
use OCP\AppFramework\Db\DoesNotExistException;
1213
use OCP\AppFramework\Db\QBMapper;
1314
use OCP\Comments\ICommentsManager;
@@ -245,21 +246,23 @@ public function getFileType(int $id): string {
245246
return 'not_libresign_file';
246247
}
247248

248-
public function getTextOfStatus(int $status): ?string {
249+
public function getTextOfStatus(int|FileStatus $status): string {
250+
if (is_int($status)) {
251+
$status = FileStatus::from($status);
252+
}
249253
return match ($status) {
250254
// TRANSLATORS Name of the status when document is not a LibreSign file
251-
File::STATUS_NOT_LIBRESIGN_FILE => $this->l->t('not LibreSign file'),
255+
FileStatus::NOT_LIBRESIGN_FILE => $this->l->t('not LibreSign file'),
252256
// TRANSLATORS Name of the status that the document is still as a draft
253-
File::STATUS_DRAFT => $this->l->t('draft'),
257+
FileStatus::DRAFT => $this->l->t('draft'),
254258
// TRANSLATORS Name of the status that the document can be signed
255-
File::STATUS_ABLE_TO_SIGN => $this->l->t('available for signature'),
259+
FileStatus::ABLE_TO_SIGN => $this->l->t('available for signature'),
256260
// TRANSLATORS Name of the status when the document has already been partially signed
257-
File::STATUS_PARTIAL_SIGNED => $this->l->t('partially signed'),
261+
FileStatus::PARTIAL_SIGNED => $this->l->t('partially signed'),
258262
// TRANSLATORS Name of the status when the document has been completely signed
259-
File::STATUS_SIGNED => $this->l->t('signed'),
263+
FileStatus::SIGNED => $this->l->t('signed'),
260264
// TRANSLATORS Name of the status when the document was deleted
261-
File::STATUS_DELETED => $this->l->t('deleted'),
262-
default => '',
265+
FileStatus::DELETED => $this->l->t('deleted'),
263266
};
264267
}
265268

lib/Db/IdDocsMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private function getIdDocStatusText(int $status): string {
303303
return match ($status) {
304304
File::STATUS_ABLE_TO_SIGN => $this->l10n->t('waiting for approval'),
305305
File::STATUS_SIGNED => $this->l10n->t('approved'),
306-
default => $this->fileMapper->getTextOfStatus($status) ?? '',
306+
default => $this->fileMapper->getTextOfStatus($status),
307307
};
308308
}
309309
}

lib/Enum/FileStatus.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Libresign\Enum;
11+
12+
/**
13+
* File status enum
14+
*
15+
* Represents all possible states a LibreSign file can be in
16+
*/
17+
enum FileStatus: int {
18+
case NOT_LIBRESIGN_FILE = -1;
19+
case DRAFT = 0;
20+
case ABLE_TO_SIGN = 1;
21+
case PARTIAL_SIGNED = 2;
22+
case SIGNED = 3;
23+
case DELETED = 4;
24+
}

lib/ResponseDefinitions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* }
4343
* @psalm-type LibresignNextcloudFile = array{
4444
* message: string,
45-
* name: string,
45+
* name: non-falsy-string,
4646
* id: int,
4747
* status: int,
4848
* statusText: string,

0 commit comments

Comments
 (0)