Skip to content

Commit

Permalink
fix(lint): avoid risky truthy falsy comparison
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jan 23, 2024
1 parent 1e1be81 commit b29c5e0
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
12 changes: 6 additions & 6 deletions lib/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(

#[NoAdminRequired]
public function create(int $fileId = null, string $file = null): DataResponse {
return $this->apiService->create($fileId, $file, null, null);
return $this->apiService->create($fileId, $file);
}

#[NoAdminRequired]
Expand Down
11 changes: 6 additions & 5 deletions lib/Controller/UserApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?: '';

Check failure on line 45 in lib/Controller/UserApiController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

RiskyTruthyFalsyComparison

lib/Controller/UserApiController.php:45:19: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
if (stripos($displayName, $filter) !== false || stripos($sessionUserId, $filter) !== false) {
$users[$sessionUserId] = $displayName;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(IRequest $request,
$this->l10n = $l10n;
}

public function create(?int $fileId = null, ?string $filePath = null, ?string $token = null, ?string $guestName = null): DataResponse {
public function create(?int $fileId = null, ?string $filePath = null, string $token = '', ?string $guestName = null): DataResponse {
try {
if ($token) {
$file = $this->documentService->getFileByShareToken($token, $this->request->getParam('filePath'));
Expand All @@ -86,7 +86,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $t
} catch (NotPermittedException $e) {
return new DataResponse($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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private function getMediaFilePreviewFile(string $mediaFileName, File $textFile):
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getAttachmentList(int $documentId, ?string $userId = null, ?Session $session = null, ?string $shareToken = null): array {
public function getAttachmentList(int $documentId, string $userId = '', ?Session $session = null, string $shareToken = ''): array {
if ($shareToken) {

Check failure on line 218 in lib/Service/AttachmentService.php

View workflow job for this annotation

GitHub Actions / Nextcloud

RiskyTruthyFalsyComparison

lib/Service/AttachmentService.php:218:7: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
$textFile = $this->getTextFilePublic($documentId, $shareToken);
} elseif ($userId) {

Check failure on line 220 in lib/Service/AttachmentService.php

View workflow job for this annotation

GitHub Actions / Nextcloud

RiskyTruthyFalsyComparison

lib/Service/AttachmentService.php:220:13: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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;
}
Expand Down Expand Up @@ -335,7 +335,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)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/SessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? $guestName : '';
$uniqueGuestId = !empty($guestName) ? $guestName : $this->secureRandom->generate(12);
$color = $this->avatarManager->getGuestAvatar($uniqueGuestId)->avatarBackgroundColor($uniqueGuestId);
return $color->name();
Expand Down

0 comments on commit b29c5e0

Please sign in to comment.