Skip to content

Commit 8f7153f

Browse files
committed
chore: Improve getPermissions typing
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 0f82477 commit 8f7153f

19 files changed

Lines changed: 73 additions & 96 deletions

File tree

apps/dav/lib/Connector/Sabre/Node.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,17 @@ public function getInternalPath(): string {
236236
return $this->info->getInternalPath();
237237
}
238238

239+
/**
240+
* @param string|null $user
241+
* @return int-mask-of<Constants::PERMISSION_*>
242+
*/
239243
public function getSharePermissions(?string $user): int {
240244
// check of we access a federated share
241245
if ($user !== null) {
242246
try {
243-
return $this->shareManager->getShareByToken($user)->getPermissions();
247+
/** @var int-mask-of<Constants::PERMISSION_*> $permissions */
248+
$permissions = $this->shareManager->getShareByToken($user)->getPermissions();
249+
return $permissions;
244250
} catch (ShareNotFound) {
245251
// ignore
246252
}
@@ -259,21 +265,21 @@ public function getSharePermissions(?string $user): int {
259265
}
260266

261267
/*
262-
* We can always share non moveable mount points with DELETE and UPDATE
268+
* We can always share non-moveable mount points with DELETE and UPDATE
263269
* Eventually we need to do this properly
264270
*/
265271
$mountpoint = $this->info->getMountPoint();
266272
if (!($mountpoint instanceof IMovableMount)) {
267273
/**
268274
* @psalm-suppress UnnecessaryVarAnnotation Rector doesn't trust the return type annotation
269-
* @var string $mountpointpath
275+
* @var string $mountpointPath
270276
*/
271-
$mountpointpath = $mountpoint->getMountPoint();
272-
if (str_ends_with($mountpointpath, '/')) {
273-
$mountpointpath = substr($mountpointpath, 0, -1);
277+
$mountpointPath = $mountpoint->getMountPoint();
278+
if (str_ends_with($mountpointPath, '/')) {
279+
$mountpointPath = substr($mountpointPath, 0, -1);
274280
}
275281

276-
if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
282+
if (!$mountpoint->getOption('readonly', false) && $mountpointPath === $this->info->getPath()) {
277283
$permissions |= Constants::PERMISSION_DELETE | Constants::PERMISSION_UPDATE;
278284
}
279285
}
@@ -285,6 +291,7 @@ public function getSharePermissions(?string $user): int {
285291
$permissions &= ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE);
286292
}
287293

294+
/** @var int-mask-of<Constants::PERMISSION_*> $permissions */
288295
return $permissions;
289296
}
290297

apps/files_sharing/lib/External/Storage.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,27 @@ public function getPermissions(string $path): int {
362362
$ocsPermissions = $response['{http://open-collaboration-services.org/ns}share-permissions'] ?? null;
363363
$ocmPermissions = $response['{http://open-cloud-mesh.org/ns}share-permissions'] ?? null;
364364
$ocPermissions = $response['{http://owncloud.org/ns}permissions'] ?? null;
365+
365366
// old federated sharing permissions
366367
if ($ocsPermissions !== null) {
367-
$permissions = (int)$ocsPermissions;
368-
} elseif ($ocmPermissions !== null) {
368+
$permission = (int)$ocsPermissions;
369+
if ($permission < 0 || $permission > Constants::PERMISSION_ALL) {
370+
throw new \RuntimeException('Got invalid permissions: ' . $ocsPermissions);
371+
}
372+
return $permission;
373+
}
374+
375+
if ($ocmPermissions !== null) {
369376
// permissions provided by the OCM API
370-
$permissions = $this->ocmPermissions2ncPermissions($ocmPermissions, $path);
371-
} elseif ($ocPermissions !== null) {
377+
return $this->ocmPermissions2ncPermissions($ocmPermissions, $path);
378+
}
379+
380+
if ($ocPermissions !== null) {
372381
return $this->parsePermissions($ocPermissions);
373-
} else {
374-
// use default permission if remote server doesn't provide the share permissions
375-
$permissions = $this->getDefaultPermissions($path);
376382
}
377383

378-
return $permissions;
384+
// use default permission if remote server doesn't provide the share permissions
385+
return $this->getDefaultPermissions($path);
379386
}
380387

381388
#[\Override]
@@ -388,36 +395,30 @@ public function needsPartFile(): bool {
388395
*
389396
* @param string $ocmPermissions json encoded OCM permissions
390397
* @param string $path path to file
391-
* @return int
398+
* @return int-mask-of<Constants::PERMISSION_*>
392399
*/
393400
protected function ocmPermissions2ncPermissions(string $ocmPermissions, string $path): int {
394401
try {
395-
$ocmPermissions = json_decode($ocmPermissions);
402+
$ocmPermissions = json_decode($ocmPermissions, flags: JSON_THROW_ON_ERROR);
396403
$ncPermissions = 0;
397404
foreach ($ocmPermissions as $permission) {
398-
switch (strtolower($permission)) {
399-
case 'read':
400-
$ncPermissions += Constants::PERMISSION_READ;
401-
break;
402-
case 'write':
403-
$ncPermissions += Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE;
404-
break;
405-
case 'share':
406-
$ncPermissions += Constants::PERMISSION_SHARE;
407-
break;
408-
default:
409-
throw new \Exception();
410-
}
405+
$ncPermissions |= match (strtolower($permission)) {
406+
'read' => Constants::PERMISSION_READ,
407+
'write' => Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE,
408+
'share' => Constants::PERMISSION_SHARE,
409+
default => throw new \Exception(),
410+
};
411411
}
412-
} catch (\Exception $e) {
413-
$ncPermissions = $this->getDefaultPermissions($path);
412+
/** @var int-mask-of<Constants::PERMISSION_*> $ncPermissions */
413+
return $ncPermissions;
414+
} catch (\Exception) {
415+
return $this->getDefaultPermissions($path);
414416
}
415-
416-
return $ncPermissions;
417417
}
418418

419419
/**
420420
* Calculate the default permissions in case no permissions are provided
421+
* @return int-mask-of<Constants::PERMISSION_*>
421422
*/
422423
protected function getDefaultPermissions(string $path): int {
423424
if ($this->is_dir($path)) {

apps/files_sharing/lib/SharedStorage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ public function getPermissions(string $path = ''): int {
249249
if (!$this->isValid()) {
250250
return 0;
251251
}
252+
/** @var int-mask-of<Constants::PERMISSION_*> $permissions */
252253
$permissions = parent::getPermissions($path) & $this->superShare->getPermissions();
253254

254255
// part files and the mount point always have delete permissions

apps/files_trashbin/lib/Trash/TrashItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function isEncrypted() {
109109
}
110110

111111
#[\Override]
112-
public function getPermissions() {
112+
public function getPermissions(): int {
113113
return $this->fileInfo->getPermissions();
114114
}
115115

lib/private/Files/FileInfo.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,8 @@ public function getEncryptedVersion(): int {
207207
return isset($this->data['encryptedVersion']) ? (int)$this->data['encryptedVersion'] : 1;
208208
}
209209

210-
/**
211-
* @return int
212-
*/
213210
#[\Override]
214-
public function getPermissions() {
211+
public function getPermissions(): int {
215212
return (int)$this->data['permissions'];
216213
}
217214

lib/private/Files/Node/LazyFolder.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,8 @@ public function getEtag() {
256256
return $this->__call(__FUNCTION__, func_get_args());
257257
}
258258

259-
/**
260-
* @inheritDoc
261-
*/
262259
#[\Override]
263-
public function getPermissions() {
260+
public function getPermissions(): int {
264261
if (isset($this->data['permissions'])) {
265262
return $this->data['permissions'];
266263
}

lib/private/Files/Node/Node.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,11 @@ public function getEtag() {
228228
}
229229

230230
/**
231-
* @return int
232231
* @throws InvalidPathException
233232
* @throws NotFoundException
234233
*/
235234
#[\Override]
236-
public function getPermissions() {
235+
public function getPermissions(): int {
237236
return $this->getFileInfo(false)->getPermissions();
238237
}
239238

lib/private/Files/Node/NonExistingFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getEtag() {
8484
}
8585

8686
#[\Override]
87-
public function getPermissions() {
87+
public function getPermissions(): int {
8888
if ($this->fileInfo) {
8989
return parent::getPermissions();
9090
} else {

lib/private/Files/Node/NonExistingFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getEtag() {
8686
}
8787

8888
#[\Override]
89-
public function getPermissions() {
89+
public function getPermissions(): int {
9090
if ($this->fileInfo) {
9191
return parent::getPermissions();
9292
} else {

lib/private/Files/Node/Root.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,35 +254,23 @@ public function getSize($includeMounts = true): int|float {
254254
return 0;
255255
}
256256

257-
/**
258-
* @return string
259-
*/
260257
#[\Override]
261-
public function getEtag() {
258+
public function getEtag(): string {
262259
return '';
263260
}
264261

265-
/**
266-
* @return int
267-
*/
268262
#[\Override]
269-
public function getPermissions() {
263+
public function getPermissions(): int {
270264
return Constants::PERMISSION_CREATE;
271265
}
272266

273-
/**
274-
* @return bool
275-
*/
276267
#[\Override]
277-
public function isReadable() {
268+
public function isReadable(): bool {
278269
return false;
279270
}
280271

281-
/**
282-
* @return bool
283-
*/
284272
#[\Override]
285-
public function isUpdateable() {
273+
public function isUpdateable(): bool {
286274
return false;
287275
}
288276

0 commit comments

Comments
 (0)