Skip to content

Commit

Permalink
fix: php lint
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Apr 18, 2024
1 parent 3e06cf0 commit 18d3ec7
Show file tree
Hide file tree
Showing 58 changed files with 417 additions and 1,299 deletions.
61 changes: 33 additions & 28 deletions lib/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ class AppConfig {
'watermark_linkTagsList' => 'array'
];

public function __construct(
private IConfig $config,
private IAppManager $appManager,
private GlobalScaleConfig $globalScaleConfig,
) {
}

public function getAppNamespace(string $key) {
public function getAppNamespace(string $key): string {
if (str_starts_with($key, 'watermark_')) {
return self::WATERMARK_APP_NAMESPACE;
}
Expand Down Expand Up @@ -105,9 +98,12 @@ public function setAppValue($key, $value) {

/**
* Get all app settings
* @return array
*
* @return (bool|string|string[])[]
*
* @psalm-return array<string, bool|list<string>|string>
*/
public function getAppSettings() {
public function getAppSettings(): array {
$result = [];
$keys = $this->config->getAppKeys(Application::APPNAME);

Check failure on line 108 in lib/AppConfig.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedThisPropertyFetch

lib/AppConfig.php:108:11: UndefinedThisPropertyFetch: Instance property OCA\Richdocuments\AppConfig::$config is not defined (see https://psalm.dev/041)
foreach ($keys as $key) {
Expand Down Expand Up @@ -157,7 +153,12 @@ public function getDisableCertificateValidation(): bool {
return $this->config->getAppValue(Application::APPNAME, 'disable_certificate_verification', 'no') === 'yes';

Check failure on line 153 in lib/AppConfig.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedThisPropertyFetch

lib/AppConfig.php:153:10: UndefinedThisPropertyFetch: Instance property OCA\Richdocuments\AppConfig::$config is not defined (see https://psalm.dev/041)
}

public function getUseGroups(): ?array {
/**
* @return null|string[]
*
* @psalm-return non-empty-list<string>|null
*/
public function getUseGroups(): array|null {
$groups = $this->config->getAppValue(Application::APPNAME, 'use_groups', '');
if ($groups === '') {
return null;
Expand All @@ -166,7 +167,12 @@ public function getUseGroups(): ?array {
return $this->splitGroups($groups);
}

public function getEditGroups(): ?array {
/**
* @return null|string[]
*
* @psalm-return non-empty-list<string>|null
*/
public function getEditGroups(): array|null {
$groups = $this->config->getAppValue(Application::APPNAME, 'edit_groups', '');
if ($groups === '') {
return null;
Expand All @@ -175,30 +181,24 @@ public function getEditGroups(): ?array {
return $this->splitGroups($groups);
}

public function isReadOnlyFeatureLocked(): bool {
return $this->config->getAppValue(Application::APPNAME, self::READ_ONLY_FEATURE_LOCK, 'no') === 'yes';
}

private function splitGroups(string $groupString): array {
return explode('|', $groupString);
}

/**
* Allow to override values from the WOPI checkFileInfo response through app config
* @return string[]
*
* @psalm-return non-empty-list<string>
*/
public function getWopiOverride(): array {
$wopiOverride = $this->config->getAppValue(Application::APPNAME, 'wopi_override', '');
if ($wopiOverride !== '') {
$wopiOverride = json_decode($wopiOverride, true);
return $wopiOverride ?: [];
}
return [];
private function splitGroups(string $groupString): array {
return explode('|', $groupString);
}

public function useSecureViewAdditionalMimes(): bool {
return $this->config->getAppValue(Application::APPNAME, self::USE_SECURE_VIEW_ADDITIONAL_MIMES, 'no') === 'yes';
}

/**
* @return (false|string)[]
*
* @psalm-return array<false|string>
*/
public function getDomainList(): array {
$urls = array_merge(
[ $this->domainOnly($this->getCollaboraUrlPublic()) ],
Expand All @@ -209,6 +209,11 @@ public function getDomainList(): array {
return array_map(fn ($url) => idn_to_ascii($url), array_filter($urls));
}

/**
* @return string[]
*
* @psalm-return array<string>
*/
private function getFederationDomains(): array {
if (!$this->appManager->isEnabledForUser('federation')) {
return [];
Expand Down
7 changes: 3 additions & 4 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@
class Application extends App implements IBootstrap {
public const APPNAME = 'richdocuments';

public function __construct(array $urlParams = []) {
parent::__construct(self::APPNAME, $urlParams);
}


public function register(IRegistrationContext $context): void {
$context->registerTemplateProvider(CollaboraTemplateProvider::class);
Expand Down Expand Up @@ -164,6 +160,9 @@ public function boot(IBootContext $context): void {
$this->checkAndEnableCODEServer();
}

/**
* @return void
*/
public function checkAndEnableCODEServer() {
// Supported only on Linux OS, and x86_64 & ARM64 platforms
$supportedArchs = ['x86_64', 'aarch64'];
Expand Down
2 changes: 1 addition & 1 deletion lib/Backgroundjobs/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function run($argument) {
$this->cleanUpWopiTokens();
}

private function cleanUpWopiTokens() {
private function cleanUpWopiTokens(): void {
$tokenIds = $this->wopiMapper->getExpiredTokenIds(1000);
$query = $this->db->getQueryBuilder();
$query->delete('richdocuments_wopi')
Expand Down
10 changes: 0 additions & 10 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ class Capabilities implements ICapability {

private ?array $capabilities = null;

public function __construct(
private AppConfig $config,
private CapabilitiesService $capabilitiesService,
private PermissionManager $permissionManager,
private IAppManager $appManager,
private ?string $userId,
private IURLGenerator $urlGenerator
) {
}

public function getCapabilities() {
// Only expose capabilities for users with enabled office or guests (where it depends on the share owner if they have access)
if (!$this->permissionManager->isEnabledForUser() && $this->userId !== null) {
Expand Down
14 changes: 11 additions & 3 deletions lib/Controller/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ public function __construct($appName,

/**
* @NoAdminRequired
*
* @NoCSRFRequired
*
* @param string $path
*
* @return JSONResponse
*
* @psalm-return JSONResponse<200|404, array{url?: string}, array<never, never>>
*/
public function create($path) {
public function create($path): JSONResponse {
$userFolder = $this->rootFolder->getUserFolder($this->userId);

try {
Expand All @@ -88,13 +92,17 @@ public function create($path) {

/**
* @PublicPage
*
* @NoCSRFRequired
*
* @param string $token
* @return Http\Response
*
* @return DataResponse|StreamResponse
*
* @psalm-return DataResponse<404, array<never, never>, array<never, never>>|StreamResponse<200, array<never, never>>
*/
#[RestrictToWopiServer]
public function get($token) {
public function get($token): StreamResponse|DataResponse {
try {
$asset = $this->assetMapper->getAssetByToken($token);
} catch (DoesNotExistException $e) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Controller/DirectViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ public function showPublicShare(Direct $direct) {

/**
* @psalm-param 'Failed to open the requested file.' $message
*
* @psalm-return TemplateResponse<200, array<never, never>>
*/
private function renderErrorPage(string $message) {
private function renderErrorPage(string $message): TemplateResponse {
$params = [
'errors' => [['error' => $message]]
];
Expand Down
21 changes: 14 additions & 7 deletions lib/Controller/DocumentAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
use Throwable;

class DocumentAPIController extends \OCP\AppFramework\OCSController {
private $rootFolder;
private $shareManager;
private $templateManager;
private $l10n;
private $logger;
private $lockManager;
private IRootFolder $rootFolder;
private IManager $shareManager;
private TemplateManager $templateManager;
private IL10N $l10n;
private LoggerInterface $logger;
private ILockManager $lockManager;
private $userId;

public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, ILockManager $lockManager, $userId) {
Expand All @@ -73,12 +73,16 @@ public function __construct(IRequest $request, IRootFolder $rootFolder, IManager
* actions in src/view/NewFileMenu.js
*
* @NoAdminRequired
*
* @PublicPage
*
* @psalm-return JSONResponse<200|400, array{status: 'error'|'success', data?: mixed, message?: string}, array<never, never>>
*/
public function create(string $mimeType, string $fileName, string $directoryPath = '/', ?string $shareToken = null, ?int $templateId = null): JSONResponse {
try {
$share = null;
if ($shareToken !== null) {
$share = $this->shareManager->getShareByToken($shareToken);
$this->shareManager->getShareByToken($shareToken);
}

$rootFolder = $shareToken !== null ? $share->getNode() : $this->rootFolder->getUserFolder($this->userId);
Expand Down Expand Up @@ -156,6 +160,9 @@ public function create(string $mimeType, string $fileName, string $directoryPath
]);
}

/**
* @psalm-return DataResponse<200|400|500, array<never, never>, array<never, never>>
*/
#[Http\Attribute\NoAdminRequired]
public function openLocal(int $fileId): DataResponse {
try {
Expand Down
37 changes: 31 additions & 6 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public function __construct(
* Requests is accepted only when a secret_token is provided set by admin in
* settings page
*
* @return array access_token, urlsrc
* @return string[] access_token, urlsrc
*
* @psalm-return array{status: 'error'|'success', message?: 'Permission denied', urlsrc?: string, token?: string}
*/
#[PublicPage]
#[NoCSRFRequired]
Expand Down Expand Up @@ -202,9 +204,14 @@ public function createFromTemplate(int $templateId, string $fileName, string $di
return $this->documentTemplateResponse($wopi, $params);
}

/**
* @return RedirectResponse|TemplateResponse
*
* @psalm-return RedirectResponse<303, array<never, never>>|TemplateResponse<int, array<string, mixed>>
*/
#[PublicPage]
#[NoCSRFRequired]
public function publicPage(string $shareToken, ?string $fileName = null, ?int $fileId = null): TemplateResponse|RedirectResponse {
public function publicPage(string $shareToken, ?string $fileName = null, ?int $fileId = null): TemplateResponse|RedirectResponse|RedirectResponse {
try {
$share = $this->shareManager->getShareByToken($shareToken);
$file = $this->getFileForShare($share, $fileId, $fileName);
Expand Down Expand Up @@ -247,7 +254,7 @@ public function remote(string $shareToken, string $remoteServer, string $remoteS
// not authenticated ?
if ($share->getPassword()) {
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
|| $this->session->get('public_link_authenticated') !== $share->getId()
) {
throw new Exception('Invalid password');
}
Expand Down Expand Up @@ -300,6 +307,9 @@ public function remote(string $shareToken, string $remoteServer, string $remoteS
return new TemplateResponse('core', '403', [], 'guest');
}

/**
* @psalm-return TemplateResponse<int, array<string, mixed>>
*/
private function renderErrorPage(string $message, int $status = Http::STATUS_INTERNAL_SERVER_ERROR): TemplateResponse {
$params = [
'errors' => [['error' => $message]]
Expand All @@ -309,10 +319,15 @@ private function renderErrorPage(string $message, int $status = Http::STATUS_INT
return $response;
}

/**
* @return RedirectResponse|TemplateResponse
*
* @psalm-return RedirectResponse<303, array<never, never>>|TemplateResponse<int, array<string, mixed>>
*/
#[NoCSRFRequired]
#[NoAdminRequired]
#[UseSession]
public function editOnline(?string $path = null, ?string $userId = null, ?string $target = null): RedirectResponse|TemplateResponse {
public function editOnline(?string $path = null, ?string $userId = null, ?string $target = null): TemplateResponse|RedirectResponse|TemplateResponse {
if ($path === null) {
return $this->renderErrorPage('No path provided');
}
Expand Down Expand Up @@ -350,10 +365,15 @@ public function editOnline(?string $path = null, ?string $userId = null, ?string
return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
}

/**
* @return RedirectResponse|TemplateResponse
*
* @psalm-return RedirectResponse<303, array<never, never>>|TemplateResponse<int, array<string, mixed>>
*/
#[NoCSRFRequired]
#[NoAdminRequired]
#[UseSession]
public function editOnlineTarget(int $fileId, ?string $target = null): RedirectResponse|TemplateResponse {
public function editOnlineTarget(int $fileId, ?string $target = null): TemplateResponse|RedirectResponse|TemplateResponse {
if (!$this->userId) {
return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
}
Expand All @@ -375,6 +395,9 @@ public function editOnlineTarget(int $fileId, ?string $target = null): RedirectR
return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
}

/**
* @psalm-return DataResponse<200, array, array<never, never>>|DataResponse<500, 'Failed to generate token', array<never, never>>
*/
#[PublicPage]
public function token(int $fileId, ?string $shareToken = null, ?string $path = null): DataResponse {
try {
Expand All @@ -395,6 +418,8 @@ public function token(int $fileId, ?string $shareToken = null, ?string $path = n

/**
* Since collabora does not extend the session on interaction we need to manually trigger this while editing
*
* @psalm-return DataResponse<200, array<never, never>, array<never, never>>
*/
#[NoAdminRequired]
public function heartbeat(): DataResponse {
Expand Down Expand Up @@ -431,7 +456,7 @@ private function getFileForShare(IShare $share, ?int $fileId, ?string $path = nu
// not authenticated ?
if ($share->getPassword()) {
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
|| $this->session->get('public_link_authenticated') !== $share->getId()
) {
throw new NotPermittedException('Invalid password');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/DocumentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function documentTemplateResponse(Wopi $wopi, array $params): TemplateRe
/**
* Setup policy headers for the response
*/
private function applyPolicies($response) {
private function applyPolicies($response): void {
$collaboraHost = $this->domainOnly($this->appConfig->getCollaboraUrlPublic());

$featurePolicy = new FeaturePolicy();
Expand Down
Loading

0 comments on commit 18d3ec7

Please sign in to comment.