From 0d67aa09f526f08f98ac499af73877b7885ce693 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 4 Apr 2024 07:00:10 +0000 Subject: [PATCH 1/2] chore(deps-dev): bump psalm/phar from 5.19.0 to 5.23.1 Bumps [psalm/phar](https://github.com/psalm/phar) from 5.19.0 to 5.23.1. - [Release notes](https://github.com/psalm/phar/releases) - [Commits](https://github.com/psalm/phar/compare/5.19.0...5.23.1) --- updated-dependencies: - dependency-name: psalm/phar dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 9c0145ef12c..911477d06ae 100644 --- a/composer.lock +++ b/composer.lock @@ -874,16 +874,16 @@ }, { "name": "psalm/phar", - "version": "5.19.0", + "version": "5.23.1", "source": { "type": "git", "url": "https://github.com/psalm/phar.git", - "reference": "ec7ecda263af008af9ab74432e309278f31f1e02" + "reference": "07bb50acefdaf7b663087186f86d47542a9b1622" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/psalm/phar/zipball/ec7ecda263af008af9ab74432e309278f31f1e02", - "reference": "ec7ecda263af008af9ab74432e309278f31f1e02", + "url": "https://api.github.com/repos/psalm/phar/zipball/07bb50acefdaf7b663087186f86d47542a9b1622", + "reference": "07bb50acefdaf7b663087186f86d47542a9b1622", "shasum": "" }, "require": { @@ -903,9 +903,9 @@ "description": "Composer-based Psalm Phar", "support": { "issues": "https://github.com/psalm/phar/issues", - "source": "https://github.com/psalm/phar/tree/5.19.0" + "source": "https://github.com/psalm/phar/tree/5.23.1" }, - "time": "2024-01-09T21:19:15+00:00" + "time": "2024-03-11T20:43:33+00:00" }, { "name": "psr/clock", From d0f59529ce4d41e08b05c66800e6514cffb0b43b Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 23 Jan 2024 09:09:30 +0100 Subject: [PATCH 2/2] fix(lint): avoid risky truthy falsy comparison Signed-off-by: Max --- lib/Controller/AttachmentController.php | 12 ++++++------ lib/Controller/SessionController.php | 2 +- lib/Controller/UserApiController.php | 11 ++++++----- lib/Db/StepMapper.php | 3 +++ lib/Service/ApiService.php | 6 +++--- lib/Service/AttachmentService.php | 14 ++++---------- lib/Service/DocumentService.php | 12 ++++++------ lib/Service/EncodingService.php | 4 ++-- lib/Service/SessionService.php | 8 ++++---- tests/psalm-baseline.xml | 22 ++++++++++++++-------- 10 files changed, 49 insertions(+), 45 deletions(-) diff --git a/lib/Controller/AttachmentController.php b/lib/Controller/AttachmentController.php index 19ec885da8d..e752b5f1253 100644 --- a/lib/Controller/AttachmentController.php +++ b/lib/Controller/AttachmentController.php @@ -86,7 +86,7 @@ public function __construct( #[NoAdminRequired] #[PublicPage] #[RequireDocumentSessionOrUserOrShareToken] - public function getAttachmentList(?string $shareToken = null): DataResponse { + public function getAttachmentList(string $shareToken = ''): DataResponse { $documentId = $this->getDocument()->getId(); try { $session = $this->getSession(); @@ -98,7 +98,7 @@ public function getAttachmentList(?string $shareToken = null): DataResponse { $attachments = $this->attachmentService->getAttachmentList($documentId, null, $session, $shareToken); } else { $userId = $this->getUserId(); - $attachments = $this->attachmentService->getAttachmentList($documentId, $userId, $session, null); + $attachments = $this->attachmentService->getAttachmentList($documentId, $userId, $session); } return new DataResponse($attachments); @@ -126,7 +126,7 @@ public function insertAttachmentFile(string $filePath): DataResponse { #[NoAdminRequired] #[PublicPage] #[RequireDocumentSession] - public function uploadAttachment(?string $shareToken = null): DataResponse { + public function uploadAttachment(string $shareToken = ''): DataResponse { $documentId = $this->getSession()->getDocumentId(); try { @@ -193,7 +193,7 @@ private function getUploadedFile(string $key): array { #[PublicPage] #[NoCSRFRequired] #[RequireDocumentSessionOrUserOrShareToken] - public function getImageFile(string $imageFileName, ?string $shareToken = null, + public function getImageFile(string $imageFileName, string $shareToken = '', int $preferRawImage = 0): DataResponse|DataDownloadResponse { $documentId = $this->getDocument()->getId(); @@ -228,7 +228,7 @@ public function getImageFile(string $imageFileName, ?string $shareToken = null, #[PublicPage] #[NoCSRFRequired] #[RequireDocumentSessionOrUserOrShareToken] - public function getMediaFile(string $mediaFileName, ?string $shareToken = null): DataResponse|DataDownloadResponse { + public function getMediaFile(string $mediaFileName, string $shareToken = ''): DataResponse|DataDownloadResponse { $documentId = $this->getDocument()->getId(); try { @@ -259,7 +259,7 @@ public function getMediaFile(string $mediaFileName, ?string $shareToken = null): #[PublicPage] #[NoCSRFRequired] #[RequireDocumentSessionOrUserOrShareToken] - public function getMediaFilePreview(string $mediaFileName, ?string $shareToken = null) { + public function getMediaFilePreview(string $mediaFileName, string $shareToken = '') { $documentId = $this->getDocument()->getId(); try { diff --git a/lib/Controller/SessionController.php b/lib/Controller/SessionController.php index 19b241d0ce5..cfc680ad789 100644 --- a/lib/Controller/SessionController.php +++ b/lib/Controller/SessionController.php @@ -59,7 +59,7 @@ public function __construct( #[NoAdminRequired] public function create(?int $fileId = null, ?string $file = null, ?string $baseVersionEtag = null): DataResponse { - return $this->apiService->create($fileId, $file, $baseVersionEtag, null, null); + return $this->apiService->create($fileId, $file, $baseVersionEtag); } #[NoAdminRequired] diff --git a/lib/Controller/UserApiController.php b/lib/Controller/UserApiController.php index 435e32f21ac..efa3c449d93 100644 --- a/lib/Controller/UserApiController.php +++ b/lib/Controller/UserApiController.php @@ -39,11 +39,12 @@ public function index(string $filter = '', int $limit = 5): DataResponse { // Add joined users to the autocomplete list foreach ($sessions as $session) { $sessionUserId = $session['userId']; - if ($sessionUserId !== null && !isset($users[$sessionUserId])) { - $displayName = $this->userManager->getDisplayName($sessionUserId); - if ($displayName && stripos($displayName, $filter) !== false || stripos($sessionUserId, $filter) !== false) { - $users[$sessionUserId] = $displayName; - } + if ($sessionUserId === null || isset($users[$sessionUserId])) { + continue; + } + $displayName = $this->userManager->getDisplayName($sessionUserId) ?? ''; + if (stripos($displayName, $filter) !== false || stripos($sessionUserId, $filter) !== false) { + $users[$sessionUserId] = $displayName; } } diff --git a/lib/Db/StepMapper.php b/lib/Db/StepMapper.php index 8279f7d044b..cc2a43e81f4 100644 --- a/lib/Db/StepMapper.php +++ b/lib/Db/StepMapper.php @@ -51,6 +51,9 @@ public function find(int $documentId, int $fromVersion): array { return $this->findEntities($qb); } + /** + * @psalm-return ?positive-int + */ public function getLatestVersion(int $documentId): ?int { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 760ee908628..48ee52d8efa 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -73,7 +73,7 @@ public function __construct(IRequest $request, public function create(?int $fileId = null, ?string $filePath = null, ?string $baseVersionEtag = null, ?string $token = null, ?string $guestName = null): DataResponse { try { - if ($token) { + if ($token !== null) { $file = $this->documentService->getFileByShareToken($token, $this->request->getParam('filePath')); /* @@ -87,7 +87,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b } catch (NotPermittedException $e) { return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], 404); } - } elseif ($fileId) { + } elseif ($fileId !== null) { try { $file = $this->documentService->getFileById($fileId); } catch (NotFoundException|NotPermittedException $e) { @@ -115,7 +115,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b $this->sessionService->removeInactiveSessionsWithoutSteps($file->getId()); $document = $this->documentService->getDocument($file->getId()); $freshSession = $document === null; - if ($baseVersionEtag && $baseVersionEtag !== $document?->getBaseVersionEtag()) { + if ($baseVersionEtag !== null && $baseVersionEtag !== $document?->getBaseVersionEtag()) { return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED); } diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 968bbf03e81..ab4d8054536 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -203,21 +203,15 @@ private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): } /** - * @param int $documentId - * @param string|null $userId - * @param Session|null $session - * @param string|null $shareToken - * - * @return array * @throws InvalidPathException * @throws NoUserException * @throws NotFoundException * @throws NotPermittedException */ public function getAttachmentList(int $documentId, ?string $userId = null, ?Session $session = null, ?string $shareToken = null): array { - if ($shareToken) { + if ($shareToken !== null) { $textFile = $this->getTextFilePublic($documentId, $shareToken); - } elseif ($userId) { + } elseif ($userId !== null) { $textFile = $this->getTextFile($documentId, $userId); } else { throw new NotPermittedException('Unable to read document'); @@ -229,7 +223,7 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess return []; } - $shareTokenUrlString = $shareToken + $shareTokenUrlString = $shareToken !== null ? '&shareToken=' . rawurlencode($shareToken) : ''; $urlParamsBase = $session @@ -237,7 +231,7 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess : '?documentId=' . $documentId . $shareTokenUrlString; $attachments = []; - $userFolder = $userId ? $this->rootFolder->getUserFolder($userId) : null; + $userFolder = $userId !== null ? $this->rootFolder->getUserFolder($userId) : null; foreach ($attachmentDir->getDirectoryListing() as $node) { if (!($node instanceof File)) { // Ignore anything but files diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index f8532b34cf7..27cf6f2bf95 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -137,7 +137,7 @@ public function createDocument(File $file): Document { // Do not hard reset if changed from outside since this will throw away possible steps // This way the user can still resolve conflicts in the editor view $stepsVersion = $this->stepMapper->getLatestVersion($document->getId()); - if ($stepsVersion && ($document->getLastSavedVersion() !== $stepsVersion)) { + if ($stepsVersion !== null && ($document->getLastSavedVersion() !== $stepsVersion)) { $this->logger->debug('Unsaved steps, continue collaborative editing'); return $document; } @@ -347,7 +347,7 @@ public function autosave(Document $document, ?File $file, int $version, ?string // Do not save if newer version already saved // Note that $version is the version of the steps the client has fetched. // It may have added steps on top of that - so if the versions match we still save. - $stepsVersion = $this->stepMapper->getLatestVersion($documentId)?: 0; + $stepsVersion = $this->stepMapper->getLatestVersion($documentId) ?? 0; $savedVersion = $document->getLastSavedVersion(); $outdated = $savedVersion > 0 && $savedVersion > $version; if (!$force && ($outdated || $version > (string)$stepsVersion)) { @@ -378,7 +378,7 @@ public function autosave(Document $document, ?File $file, int $version, ?string // Version changed but the content remains the same if ($autoSaveDocument === $file->getContent()) { - if ($documentState) { + if ($documentState !== null) { $this->writeDocumentState($file->getId(), $documentState); } $document->setLastSavedVersion($stepsVersion); @@ -397,7 +397,7 @@ public function autosave(Document $document, ?File $file, int $version, ?string ), function () use ($file, $autoSaveDocument, $documentState) { $this->saveFromText = true; $file->putContent($autoSaveDocument); - if ($documentState) { + if ($documentState !== null) { $this->writeDocumentState($file->getId(), $documentState); } }); @@ -546,9 +546,9 @@ public function getFileByShareToken(string $shareToken, ?string $path = null): F } - public function isReadOnly(File $file, string|null $token): bool { + public function isReadOnly(File $file, ?string $token): bool { $readOnly = true; - if ($token) { + if ($token !== null) { try { $this->checkSharePermissions($token, Constants::PERMISSION_UPDATE); $readOnly = false; diff --git a/lib/Service/EncodingService.php b/lib/Service/EncodingService.php index 5521f9811e5..da5e06ab74a 100644 --- a/lib/Service/EncodingService.php +++ b/lib/Service/EncodingService.php @@ -38,7 +38,7 @@ class EncodingService { public function encodeToUtf8(string $string): ?string { $encoding = $this->detectEncoding($string); - if (!$encoding) { + if ($encoding === null) { return null; } @@ -48,7 +48,7 @@ public function encodeToUtf8(string $string): ?string { public function detectEncoding(string $string): ?string { $bomDetect = $this->detectUtfBom($string); - if ($bomDetect) { + if ($bomDetect !== null) { return $bomDetect; } diff --git a/lib/Service/SessionService.php b/lib/Service/SessionService.php index 744b763cbf6..5c22de84af0 100644 --- a/lib/Service/SessionService.php +++ b/lib/Service/SessionService.php @@ -88,10 +88,10 @@ public function __construct( public function initSession(int $documentId, ?string $guestName = null): Session { $session = new Session(); $session->setDocumentId($documentId); - $userName = $this->userId ? $this->userId : $guestName; + $userName = $this->userId ?? $guestName; $session->setUserId($this->userId); $session->setToken($this->secureRandom->generate(64)); - $session->setColor($this->getColorForGuestName($guestName)); + $session->setColor($this->getColorForGuestName($guestName ?? '')); if ($this->userId === null) { $session->setGuestName($guestName ?? ''); } @@ -236,8 +236,8 @@ public function updateSessionAwareness(Session $session, string $message): Sessi return $this->sessionMapper->update($session); } - private function getColorForGuestName(?string $guestName = null): string { - $guestName = $this->userId ?? $guestName; + private function getColorForGuestName(string $guestName = ''): string { + $guestName = $this->userId !== null ? $guestName : ''; $uniqueGuestId = !empty($guestName) ? $guestName : $this->secureRandom->generate(12); $color = $this->avatarManager->getGuestAvatar($uniqueGuestId)->avatarBackgroundColor($uniqueGuestId); return $color->name(); diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 13c9fba0a9e..8c527e2e936 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,26 +1,32 @@ - + - open + - ServerPlugin + - getToken + + + + getEncodings(), true)]]> + + + - $data + - function ($data) { + getData()]]> @@ -28,8 +34,8 @@ - $data - $data + +