From 54022dffabb70fba9b9ebe460e07ef4f740cc620 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Mon, 16 Sep 2024 14:57:00 +0200 Subject: [PATCH] refactor: Change some returns from false to null Signed-off-by: provokateurin --- lib/ACL/ACLCacheWrapper.php | 5 ++--- lib/Command/ACL.php | 2 +- lib/Command/Delete.php | 2 +- lib/Command/FolderCommand.php | 10 +++++----- lib/Command/Group.php | 2 +- lib/Command/Quota.php | 2 +- lib/Command/Rename.php | 2 +- lib/Command/Scan.php | 2 +- lib/Controller/FolderController.php | 4 ++-- lib/Folder/FolderManager.php | 13 ++++++++----- 10 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/ACL/ACLCacheWrapper.php b/lib/ACL/ACLCacheWrapper.php index 5eca89701..62c3da8bb 100644 --- a/lib/ACL/ACLCacheWrapper.php +++ b/lib/ACL/ACLCacheWrapper.php @@ -55,10 +55,9 @@ protected function formatCacheEntry($entry, array $rules = []) { public function getFolderContentsById($fileId) { $results = $this->getCache()->getFolderContentsById($fileId); $rules = $this->preloadEntries($results); - $entries = array_map(function ($entry) use ($rules) { + return array_filter(array_map(function ($entry) use ($rules) { return $this->formatCacheEntry($entry, $rules); - }, $results); - return array_filter(array_filter($entries)); + }, $results)); } public function search($pattern) { diff --git a/lib/Command/ACL.php b/lib/Command/ACL.php index f63859a8f..a36b23545 100644 --- a/lib/Command/ACL.php +++ b/lib/Command/ACL.php @@ -61,7 +61,7 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output) { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } if ($input->getOption('enable')) { diff --git a/lib/Command/Delete.php b/lib/Command/Delete.php index 6c2c2e1c9..2a9026bde 100644 --- a/lib/Command/Delete.php +++ b/lib/Command/Delete.php @@ -26,7 +26,7 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output) { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } $helper = $this->getHelper('question'); diff --git a/lib/Command/FolderCommand.php b/lib/Command/FolderCommand.php index c4ac76b60..c3158445b 100644 --- a/lib/Command/FolderCommand.php +++ b/lib/Command/FolderCommand.php @@ -31,19 +31,19 @@ public function __construct(FolderManager $folderManager, IRootFolder $rootFolde } /** - * @psalm-return array{id: mixed, mount_point: string, groups: array|mixed, quota: int, size: int|mixed, acl: bool}|false + * @psalm-return ?array{id: mixed, mount_point: string, groups: array|mixed, quota: int, size: int|mixed, acl: bool} */ - protected function getFolder(InputInterface $input, OutputInterface $output) { + protected function getFolder(InputInterface $input, OutputInterface $output): ?array { $folderId = (int)$input->getArgument('folder_id'); if ((string)$folderId !== $input->getArgument('folder_id')) { // Protect against removing folderId === 0 when typing a string (e.g. folder name instead of folder id) $output->writeln('Folder id argument is not an integer. Got ' . $input->getArgument('folder_id') . ''); - return false; + return null; } $folder = $this->folderManager->getFolder($folderId, $this->rootFolder->getMountPoint()->getNumericStorageId()); - if ($folder === false) { + if ($folder === null) { $output->writeln('Folder not found: ' . $folderId . ''); - return false; + return null; } return $folder; } diff --git a/lib/Command/Group.php b/lib/Command/Group.php index bd64ed64f..f8f5198ae 100644 --- a/lib/Command/Group.php +++ b/lib/Command/Group.php @@ -46,7 +46,7 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output) { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } diff --git a/lib/Command/Quota.php b/lib/Command/Quota.php index 5a5e46f57..9dcffa3a9 100644 --- a/lib/Command/Quota.php +++ b/lib/Command/Quota.php @@ -25,7 +25,7 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output) { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } $quotaString = strtolower($input->getArgument('quota')); diff --git a/lib/Command/Rename.php b/lib/Command/Rename.php index 906f34573..a2e10a5df 100644 --- a/lib/Command/Rename.php +++ b/lib/Command/Rename.php @@ -24,7 +24,7 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } $this->folderManager->renameFolder($folder['id'], $input->getArgument('name')); diff --git a/lib/Command/Scan.php b/lib/Command/Scan.php index 11f2ad0f7..9c17321bf 100644 --- a/lib/Command/Scan.php +++ b/lib/Command/Scan.php @@ -63,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $folders = $this->folderManager->getAllFolders(); } else { $folder = $this->getFolder($input, $output); - if ($folder === false) { + if ($folder === null) { return -1; } $folders = [$folder['id'] => $folder]; diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index c869f2f7d..669ceadc2 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -133,7 +133,7 @@ private function checkFolderExists(int $id): ?DataResponse { return new DataResponse([], Http::STATUS_NOT_FOUND); } $folder = $this->manager->getFolder($id, $storageId); - if ($folder === false) { + if ($folder === null) { return new DataResponse([], Http::STATUS_NOT_FOUND); } return null; @@ -152,7 +152,7 @@ private function getRootFolderStorageId(): ?int { public function addFolder(string $mountpoint): DataResponse { $id = $this->manager->createFolder(trim($mountpoint)); $folder = $this->manager->getFolder($id, $this->rootFolder->getMountPoint()->getNumericStorageId()); - if ($folder === false) { + if ($folder === null) { throw new OCSNotFoundException(); } return new DataResponse($folder); diff --git a/lib/Folder/FolderManager.php b/lib/Folder/FolderManager.php index 7c7a7ab9b..77e9f079f 100644 --- a/lib/Folder/FolderManager.php +++ b/lib/Folder/FolderManager.php @@ -255,10 +255,10 @@ private function getManageAcl(array $mappings): array { } /** - * @return array{id: mixed, mount_point: mixed, groups: array, quota: int, size: int, acl: bool}|false + * @return ?array{id: mixed, mount_point: mixed, groups: array, quota: int, size: int, acl: bool} * @throws Exception */ - public function getFolder(int $id, int $rootStorageId) { + public function getFolder(int $id, int $rootStorageId): ?array { $applicableMap = $this->getAllApplicable(); $query = $this->connection->getQueryBuilder(); @@ -271,17 +271,20 @@ public function getFolder(int $id, int $rootStorageId) { $result = $query->executeQuery(); $row = $result->fetch(); $result->closeCursor(); + if (!$row) { + return null; + } $folderMappings = $this->getFolderMappings($id); - return $row ? [ + return [ 'id' => $id, 'mount_point' => (string)$row['mount_point'], 'groups' => $applicableMap[$id] ?? [], 'quota' => (int)$row['quota'], - 'size' => $row['size'] ? $row['size'] : 0, + 'size' => $row['size'] ?: 0, 'acl' => (bool)$row['acl'], 'manage' => $this->getManageAcl($folderMappings) - ] : false; + ]; } /**