Skip to content

Commit 3c494fb

Browse files
committed
refactor: Strict types and property promotions
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
1 parent 28847ef commit 3c494fb

33 files changed

Lines changed: 269 additions & 434 deletions

lib/AppConfigOverwrite.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
67
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -12,21 +13,13 @@
1213

1314
class AppConfigOverwrite extends AppConfig {
1415

15-
/** @var string[][] */
16-
private $overWrite = [];
16+
private array $overWrite = [];
1717

1818
public function setOverwrite(array $overwrite): void {
1919
$this->overWrite = $overwrite;
2020
}
2121

22-
23-
/**
24-
* @param $app
25-
* @param $key
26-
* @param $default
27-
* @return string
28-
*/
29-
public function getValue($app, $key, $default = '') {
22+
public function getValue($app, $key, string $default = ''): string {
3023
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
3124
return $this->overWrite[$app][$key];
3225
}

lib/AppWhitelist.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2017 ownCloud GmbH
@@ -29,23 +31,13 @@ class AppWhitelist {
2931

3032
public const DEFAULT_WHITELIST = 'files_trashbin,files_versions,files_sharing,files_texteditor,text,activity,firstrunwizard,photos,notifications,dashboard,user_status,weather_status';
3133

32-
/**
33-
* AppWhitelist constructor.
34-
*
35-
* @param Config $config
36-
* @param GuestManager $guestManager
37-
* @param IL10N $l10n
38-
* @param IAppManager $appManager
39-
* @param IURLGenerator $urlGenerator
40-
* @param LoggerInterface $logger
41-
*/
4234
public function __construct(
4335
IURLGenerator $urlGenerator,
44-
private Config $config,
45-
private GuestManager $guestManager,
46-
private IL10N $l10n,
47-
private IAppManager $appManager,
48-
private LoggerInterface $logger,
36+
private readonly Config $config,
37+
private readonly GuestManager $guestManager,
38+
private readonly IL10N $l10n,
39+
private readonly IAppManager $appManager,
40+
private readonly LoggerInterface $logger,
4941
) {
5042
$this->baseUrl = $urlGenerator->getBaseUrl();
5143
$this->baseUrlLength = strlen($this->baseUrl);
@@ -62,9 +54,6 @@ public function isWhitelistEnabled(): bool {
6254
return $this->config->useWhitelist();
6355
}
6456

65-
/**
66-
* @param false|string $url
67-
*/
6857
public function isUrlAllowed(IUser $user, string|false $url): bool {
6958
if ($this->guestManager->isGuest($user) && $this->isWhitelistEnabled()) {
7059
$app = $this->getRequestedApp($url);
@@ -80,9 +69,9 @@ public function isUrlAllowed(IUser $user, string|false $url): bool {
8069
$this->logger->notice("Blocking access to non-whitelisted app ($app) for guest", ['app' => 'guests']);
8170
return false;
8271
}
83-
} else {
84-
return true;
8572
}
73+
74+
return true;
8675
}
8776

8877
public function verifyAccess(IUser $user, IRequest $request): void {
@@ -98,8 +87,6 @@ public function verifyAccess(IUser $user, IRequest $request): void {
9887
/**
9988
* Core has \OC::$REQUESTEDAPP but it isn't set until the routes are matched
10089
* taken from \OC\Route\Router::match()
101-
*
102-
* @param false|string $url
10390
*/
10491
private function getRequestedApp(string|false $url): string {
10592
if (substr($url, 0, $this->baseUrlLength) === $this->baseUrl) {

lib/BackgroundJob/TransferJob.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
class TransferJob extends QueuedJob {
2626
public function __construct(
2727
ITimeFactory $time,
28-
private IUserManager $userManager,
29-
private ISecureRandom $secureRandom,
30-
private NotificationManager $notificationManager,
31-
private IURLGenerator $urlGenerator,
32-
private TransferService $transferService,
33-
private TransferMapper $transferMapper,
34-
private LoggerInterface $logger,
28+
private readonly IUserManager $userManager,
29+
private readonly ISecureRandom $secureRandom,
30+
private readonly NotificationManager $notificationManager,
31+
private readonly IURLGenerator $urlGenerator,
32+
private readonly TransferService $transferService,
33+
private readonly TransferMapper $transferMapper,
34+
private readonly LoggerInterface $logger,
3535
) {
3636
parent::__construct($time);
3737
}
@@ -48,6 +48,7 @@ private function notifyFailure(Transfer $transfer): void {
4848
'source' => $transfer->getSource(),
4949
'target' => $transfer->getTarget(),
5050
]);
51+
5152
$this->notificationManager->notify($notification);
5253
}
5354

@@ -63,13 +64,14 @@ private function notifySuccess(Transfer $transfer): void {
6364
'source' => $transfer->getSource(),
6465
'target' => $transfer->getTarget(),
6566
]);
67+
6668
$this->notificationManager->notify($notification);
6769
}
6870

6971
private function fail(Transfer $transfer, ?IUser $targetUser = null): void {
7072
$this->notifyFailure($transfer);
7173
$this->transferMapper->delete($transfer);
72-
if (!($targetUser instanceof IUser)) {
74+
if (!$targetUser instanceof IUser) {
7375
return;
7476
}
7577
$result = $targetUser->delete(); // Rollback created user
@@ -90,7 +92,7 @@ public function run($argument): void {
9092
$target = $transfer->getTarget();
9193

9294
$sourceUser = $this->userManager->get($source);
93-
if (!($sourceUser instanceof IUser)) {
95+
if (!$sourceUser instanceof IUser) {
9496
$this->logger->error('Failed to transfer missing guest user: ' . $source);
9597
$this->fail($transfer);
9698
return;
@@ -107,7 +109,7 @@ public function run($argument): void {
107109
$this->secureRandom->generate(20), // Password hash will be copied to target user from source user
108110
);
109111

110-
if (!($targetUser instanceof IUser)) {
112+
if (!$targetUser instanceof IUser) {
111113
$this->logger->error('Failed to create new user: ' . $target);
112114
$this->fail($transfer);
113115
return;
@@ -141,6 +143,7 @@ public function run($argument): void {
141143
if (!$result) {
142144
$this->logger->error('Failed to delete guest user', ['user' => $sourceUser->getUID()]);
143145
}
146+
144147
$this->notifySuccess($transfer);
145148
$this->transferMapper->delete($transfer);
146149
}

lib/Capabilities.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -19,7 +21,7 @@ class Capabilities implements ICapability {
1921
/**
2022
* @inheritDoc
2123
*/
22-
public function getCapabilities() {
24+
public function getCapabilities(): array {
2325
return [
2426
'guests' => [
2527
'enabled' => true,

lib/Command/AddCommand.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
67
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -21,18 +22,13 @@
2122
use Symfony\Component\Console\Question\Question;
2223

2324
class AddCommand extends Command {
24-
/** @var IUserManager */
25-
private $userManager;
26-
/** @var GuestManager */
27-
private $guestManager;
28-
/** @var IMailer */
29-
private $mailer;
30-
31-
public function __construct(IUserManager $userManager, IMailer $mailer, GuestManager $guestManager) {
25+
26+
public function __construct(
27+
private readonly IUserManager $userManager,
28+
private readonly IMailer $mailer,
29+
private readonly GuestManager $guestManager,
30+
) {
3231
parent::__construct();
33-
$this->userManager = $userManager;
34-
$this->guestManager = $guestManager;
35-
$this->mailer = $mailer;
3632
}
3733

3834
protected function configure() {

lib/Command/ListCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use Symfony\Component\Console\Output\OutputInterface;
1616

1717
class ListCommand extends Base {
18-
private $guestManager;
1918

20-
public function __construct(GuestManager $guestManager) {
19+
public function __construct(
20+
private readonly GuestManager $guestManager,
21+
) {
2122
parent::__construct();
22-
$this->guestManager = $guestManager;
2323
}
2424

2525
protected function configure(): void {
@@ -50,6 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5050
$table->setRows($guests);
5151
$table->render();
5252
}
53+
5354
return 0;
5455
}
5556
}

lib/Config.php

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
67
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -16,33 +17,27 @@
1617

1718
class Config {
1819
public function __construct(
19-
private IConfig $config,
20-
private IAppConfig $appConfig,
21-
private ISubAdmin $subAdmin,
22-
private IUserSession $userSession,
23-
private IGroupManager $groupManager,
20+
private readonly IConfig $config,
21+
private readonly IAppConfig $appConfig,
22+
private readonly ISubAdmin $subAdmin,
23+
private readonly IUserSession $userSession,
24+
private readonly IGroupManager $groupManager,
2425
) {
2526
}
2627

2728
public function allowExternalStorage(): bool {
2829
return $this->appConfig->getAppValueBool('allow_external_storage', false);
2930
}
3031

31-
/**
32-
* @param string|bool $allow
33-
*/
34-
public function setAllowExternalStorage($allow): void {
32+
public function setAllowExternalStorage(string|bool $allow): void {
3533
$this->appConfig->setAppValueBool('allow_external_storage', $allow === true || $allow === 'true') ;
3634
}
3735

3836
public function hideOtherUsers(): bool {
3937
return $this->appConfig->getAppValueBool('hide_users', true);
4038
}
4139

42-
/**
43-
* @param string|bool $hide
44-
*/
45-
public function setHideOtherUsers($hide): void {
40+
public function setHideOtherUsers(string|bool $hide): void {
4641
$this->appConfig->setAppValueBool('hide_users', $hide === true || $hide === 'true') ;
4742
}
4843

@@ -54,25 +49,16 @@ public function useWhitelist(): bool {
5449
return $this->appConfig->getAppValueBool('usewhitelist', true);
5550
}
5651

57-
/**
58-
* @param string|bool $use
59-
*/
60-
public function setUseWhitelist($use): void {
52+
public function setUseWhitelist(string|bool $use): void {
6153
$this->appConfig->setAppValueBool('usewhitelist', $use === true || $use === 'true') ;
6254
}
6355

64-
/**
65-
* @return string[]
66-
*/
6756
public function getAppWhitelist(): array {
6857
$whitelist = $this->appConfig->getAppValueString('whitelist', AppWhitelist::DEFAULT_WHITELIST);
6958
return explode(',', $whitelist);
7059
}
7160

72-
/**
73-
* @param array|string $whitelist
74-
*/
75-
public function setAppWhitelist($whitelist): void {
61+
public function setAppWhitelist(array|string $whitelist): void {
7662
if (is_array($whitelist)) {
7763
$whitelist = implode(',', $whitelist);
7864
}
@@ -106,13 +92,9 @@ public function canCreateGuests(): bool {
10692
}
10793
}
10894

109-
11095
return !$this->isSharingRestrictedToGroup();
11196
}
11297

113-
/**
114-
* @return string[]
115-
*/
11698
public function getCreateRestrictedToGroup(): array {
11799
$groups = $this->appConfig->getAppValueArray('create_restricted_to_group', []);
118100
// If empty, it means there is no restriction
@@ -126,9 +108,6 @@ public function getCreateRestrictedToGroup(): array {
126108
return array_values(array_unique($this->appConfig->getAppValueArray('create_restricted_to_group', [])));
127109
}
128110

129-
/**
130-
* @param string[] $groups
131-
*/
132111
public function setCreateRestrictedToGroup(array $groups): void {
133112
$this->appConfig->setAppValueArray('create_restricted_to_group', $groups);
134113
}

0 commit comments

Comments
 (0)