Skip to content

Commit

Permalink
chore: Type all the things
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jul 24, 2023
1 parent e2e8358 commit e908731
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 80 deletions.
12 changes: 10 additions & 2 deletions lib/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,17 @@ private function getUploadedFile(string $key): array {

/**
* Serve the image files in the editor
*
* @return DataDownloadResponse|DataResponse
*
* @psalm-return DataDownloadResponse<200, string, array<never, never>>|DataResponse<404, '', array<never, never>>
*/
#[NoAdminRequired]
#[PublicPage]
#[NoCSRFRequired]
#[RequireDocumentSession]
public function getImageFile(string $imageFileName, ?string $shareToken = null,
int $preferRawImage = 0) {
int $preferRawImage = 0): DataResponse|DataDownloadResponse {
$documentId = $this->getSession()->getDocumentId();

try {
Expand All @@ -192,12 +196,16 @@ public function getImageFile(string $imageFileName, ?string $shareToken = null,

/**
* Serve the media files in the editor
*
* @return DataDownloadResponse|DataResponse
*
* @psalm-return DataDownloadResponse<200, string, array<never, never>>|DataResponse<404, '', array<never, never>>
*/
#[NoAdminRequired]
#[PublicPage]
#[NoCSRFRequired]
#[RequireDocumentSession]
public function getMediaFile(string $mediaFileName, ?string $shareToken = null) {
public function getMediaFile(string $mediaFileName, ?string $shareToken = null): DataResponse|DataDownloadResponse {
$documentId = $this->getSession()->getDocumentId();

try {
Expand Down
22 changes: 17 additions & 5 deletions lib/Controller/PublicSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
class PublicSessionController extends PublicShareController implements ISessionAwareController {
use TSessionAwareController;

private IShare $share;
private ?IShare $share = null;

public function __construct(
string $appName,
Expand All @@ -52,8 +52,16 @@ public function __construct(
parent::__construct($appName, $request, $session);
}

private function getShare(): IShare {
if ($this->share === null) {
throw new \Exception('Share has not been set yet');
}

return $this->share;
}

protected function getPasswordHash(): string {
return $this->share->getPassword();
return $this->getShare()->getPassword();
}

public function isValidToken(): bool {
Expand All @@ -66,12 +74,13 @@ public function isValidToken(): bool {
}

protected function isPasswordProtected(): bool {
return $this->share->getPassword() !== null;
/** @psalm-suppress RedundantConditionGivenDocblockType */
return $this->getShare()->getPassword() !== null;
}

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

Expand All @@ -95,10 +104,13 @@ public function sync(string $token, int $documentId, int $sessionId, string $ses
return $this->apiService->sync($this->getSession(), $this->getDocument(), $version, $autosaveContent, $documentState, $force, $manualSave, $token);
}

/**
* @psalm-return DataResponse<int, array|null|object|scalar, array<string, mixed>>
*/
#[NoAdminRequired]
#[PublicPage]
#[RequireDocumentSession]
public function updateSession(string $guestName) {
public function updateSession(string $guestName): DataResponse {
return $this->apiService->updateSession($this->getSession(), $guestName);
}
}
2 changes: 1 addition & 1 deletion lib/Controller/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function mention(string $mention): DataResponse {
return new DataResponse($this->notificationService->mention($this->getDocument()->getId(), $mention));
}

private function loginSessionUser() {
private function loginSessionUser(): void {
$currentSession = $this->getSession();
if (!$this->userSession->isLoggedIn()) {
$user = $this->userManager->get($currentSession->getUserId());
Expand Down
12 changes: 4 additions & 8 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,21 @@
use OCP\IRequest;

class SettingsController extends Controller {
private IConfig $config;
private ?string $userId;

public const ACCEPTED_KEYS = [
'workspace_enabled'
];

public function __construct($appName, IRequest $request, IConfig $config, $userId) {
public function __construct(string $appName, IRequest $request, private IConfig $config, private ?string $userId) {
parent::__construct($appName, $request);

$this->config = $config;
$this->userId = $userId;
}

/**
* @throws \OCP\PreConditionNotMetException
*
* @psalm-return DataResponse<200|400, array{workspace_enabled?: mixed, message?: 'Invalid config key'}, array<never, never>>
*/
#[NoAdminRequired]
public function updateConfig(string $key, $value) {
public function updateConfig(string $key, string $value): DataResponse {
if (!in_array($key, self::ACCEPTED_KEYS, true)) {
return new DataResponse(['message' => 'Invalid config key'], Http::STATUS_BAD_REQUEST);
}
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function publicFolder(string $shareToken, string $path = '/'): DataRespon
if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
throw new ShareNotFound();
}
/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($share->getPassword() !== null) {
$shareId = $this->session->get('public_link_authenticated');
if ($share->getId() !== $shareId) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Cron/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function __construct(ITimeFactory $time,
$this->setInterval(SessionService::SESSION_VALID_TIME);
}

/**
* @param array $argument
* @return void
*/
protected function run($argument) {
$this->logger->debug('Run cleanup job for text documents');
$documents = $this->documentService->getAll();
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @method setBaseVersionEtag(string $etag): void
*/
class Document extends Entity implements \JsonSerializable {
public $id;
public $id = null;
// TODO: Remove obsolete field `currentVersion`
protected int $currentVersion = 0;
protected int $lastSavedVersion = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/DocumentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(IDBConnection $db) {
* @return Document
* @throws DoesNotExistException
*/
public function find($documentId): Document {
public function find(int $documentId): Document {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
Expand Down
27 changes: 21 additions & 6 deletions lib/Db/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function findAllDocuments(): array {
* @return Session
* @throws DoesNotExistException
*/
public function find($documentId, $sessionId, $token): Session {
public function find(int $documentId, int $sessionId, string $token): Session {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
Expand All @@ -71,7 +71,12 @@ public function find($documentId, $sessionId, $token): Session {
return Session::fromRow($data);
}

public function findAll($documentId) {
/**
* @return Session[]
*
* @psalm-return array<Session>
*/
public function findAll(int $documentId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'color', 'document_id', 'last_awareness_message', 'last_contact', 'user_id', 'guest_name')
->from($this->getTableName())
Expand All @@ -80,7 +85,12 @@ public function findAll($documentId) {
return $this->findEntities($qb);
}

public function findAllActive($documentId) {
/**
* @return Session[]
*
* @psalm-return array<Session>
*/
public function findAllActive(int $documentId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'color', 'document_id', 'last_awareness_message', 'last_contact', 'user_id', 'guest_name')
->from($this->getTableName())
Expand All @@ -90,7 +100,12 @@ public function findAllActive($documentId) {
return $this->findEntities($qb);
}

public function findAllInactive() {
/**
* @return Session[]
*
* @psalm-return array<Session>
*/
public function findAllInactive(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'color', 'document_id', 'last_awareness_message', 'last_contact', 'user_id', 'guest_name')
->from($this->getTableName())
Expand Down Expand Up @@ -132,14 +147,14 @@ public function deleteInactiveWithoutSteps(?int $documentId = null): int {
return $deletedCount;
}

public function deleteByDocumentId($documentId): int {
public function deleteByDocumentId(int $documentId): int {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)));
return $qb->executeStatement();
}

public function isUserInDocument($documentId, $userId): bool {
public function isUserInDocument(int $documentId, string $userId): bool {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
->from($this->getTableName())
Expand Down
7 changes: 4 additions & 3 deletions lib/Db/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @method setDocumentId(int $documentId): void
*/
class Step extends Entity implements JsonSerializable {
public $id = null;
protected string $data = '';
protected int $version = 0;
protected int $sessionId = 0;
Expand All @@ -55,10 +56,10 @@ public function jsonSerialize(): array {
throw new \InvalidArgumentException('Failed to parse step data');
}
return [
'id' => $this->id,
'id' => $this->getId(),
'data' => $jsonData,
'version' => $this->version,
'sessionId' => $this->sessionId
'version' => $this->getVersion(),
'sessionId' => $this->getSessionId()
];
}
}
15 changes: 9 additions & 6 deletions lib/Db/StepMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function __construct(IDBConnection $db) {
parent::__construct($db, 'text_steps', Step::class);
}

public function find($documentId, $fromVersion, $lastAckedVersion = null) {
/**
* @return Step[]
*/
public function find(int $documentId, int $fromVersion, int $lastAckedVersion = null): array {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
Expand All @@ -51,7 +54,7 @@ public function find($documentId, $fromVersion, $lastAckedVersion = null) {
return $this->findEntities($qb);
}

public function getLatestVersion($documentId): ?int {
public function getLatestVersion(int $documentId): ?int {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$result = $qb->select('version')
Expand All @@ -69,22 +72,22 @@ public function getLatestVersion($documentId): ?int {
return $data['version'];
}

public function deleteAll($documentId): void {
public function deleteAll(int $documentId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
->executeStatement();
}

public function deleteBeforeVersion($documentId, $version): void {
public function deleteBeforeVersion(int $documentId, int $version): int {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
return $qb->delete($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
->andWhere($qb->expr()->lte('version', $qb->createNamedParameter($version)))
->executeStatement();
}

public function deleteAfterVersion($documentId, $version): int {
public function deleteAfterVersion(int $documentId, int $version): int {
$qb = $this->db->getQueryBuilder();
return $qb->delete($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
Expand Down
4 changes: 4 additions & 0 deletions lib/Migration/Version010000Date20190617184535.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Version010000Date20190617184535 extends SimpleMigrationStep {
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*
* @return void
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
Expand Down Expand Up @@ -139,6 +141,8 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*
* @return void
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
Expand Down
15 changes: 11 additions & 4 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(IRequest $request,
$this->l10n = $l10n;
}

public function create($fileId = null, $filePath = null, ?string $token = null, $guestName = null): DataResponse {
public function create(?int $fileId = null, ?string $filePath = null, ?string $token = null, ?string $guestName = null): DataResponse {
try {
if ($token) {
$file = $this->documentService->getFileByShareToken($token, $this->request->getParam('filePath'));
Expand Down Expand Up @@ -170,7 +170,7 @@ public function create($fileId = null, $filePath = null, ?string $token = null,
]);
}

public function close($documentId, $sessionId, $sessionToken): DataResponse {
public function close(int $documentId, int $sessionId, string $sessionToken): DataResponse {
$this->sessionService->closeSession($documentId, $sessionId, $sessionToken);
$this->sessionService->removeInactiveSessionsWithoutSteps($documentId);
$activeSessions = $this->sessionService->getActiveSessions($documentId);
Expand All @@ -183,8 +183,10 @@ public function close($documentId, $sessionId, $sessionToken): DataResponse {
/**
* @throws NotFoundException
* @throws DoesNotExistException
*
* @param null|string $token
*/
public function push(Session $session, Document $document, $version, $steps, $awareness, $token = null): DataResponse {
public function push(Session $session, Document $document, int $version, array $steps, string $awareness, string|null $token = null): DataResponse {
try {
$session = $this->sessionService->updateSessionAwareness($session, $awareness);
} catch (DoesNotExistException $e) {
Expand All @@ -209,7 +211,12 @@ public function push(Session $session, Document $document, $version, $steps, $aw
return new DataResponse($result);
}

public function sync(Session $session, Document $document, $version = 0, $autosaveContent = null, $documentState = null, bool $force = false, bool $manualSave = false, $token = null): DataResponse {
/**
* @param null|string $autosaveContent
* @param null|string $documentState
* @param null|string $token
*/
public function sync(Session $session, Document $document, int $version = 0, string|null $autosaveContent = null, string|null $documentState = null, bool $force = false, bool $manualSave = false, string|null $token = null): DataResponse {
$documentId = $session->getDocumentId();
try {
$result = [
Expand Down
6 changes: 6 additions & 0 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ private function getMediaFileMetadata(string $mediaFileName, File $textFile): ?a
* @param string $newFileName
* @param string $newFileContent
* @param string $userId
* @param resource $newFileResource
*
* @return array
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
Expand Down Expand Up @@ -301,7 +304,10 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
* @param string $newFileName
* @param string $newFileContent
* @param string $shareToken
* @param resource $newFileResource
*
* @return array
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws \OCP\Files\InvalidPathException
Expand Down
Loading

0 comments on commit e908731

Please sign in to comment.