Skip to content

Commit 470858f

Browse files
committed
refactor: Modernize code with rector
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 8a83601 commit 470858f

47 files changed

Lines changed: 535 additions & 530 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
}
1818
},
1919
"scripts": {
20+
"post-install-cmd": [
21+
"@composer bin all install --ansi"
22+
],
2023
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
2124
"cs:check": "php-cs-fixer fix --dry-run --diff",
2225
"cs:fix": "php-cs-fixer fix",
@@ -25,10 +28,12 @@
2528
"psalm:update-baseline:force": "psalm --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
2629
"psalm:clear": "psalm --clear-cache && psalm --clear-global-cache",
2730
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MismatchingDocblockParamType,MismatchingDocblockReturnType,MissingParamType,InvalidFalsableReturnType",
28-
"test:unit": "vendor/bin/phpunit -c phpunit.xml"
31+
"test:unit": "vendor/bin/phpunit -c phpunit.xml",
32+
"rector": "rector && composer cs:fix"
2933
},
3034
"require": {
31-
"php": ">=8.1 <=8.4"
35+
"php": ">=8.1 <=8.4",
36+
"bamarni/composer-bin-plugin": "^1.9"
3237
},
3338
"require-dev": {
3439
"nextcloud/coding-standard": "^1.3.2",

composer.lock

Lines changed: 60 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/AppConfigOverwrite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class AppConfigOverwrite extends AppConfig {
1414

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

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

lib/AppInfo/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function setupGuestRestrictions(ContainerInterface $container, Container
8181
$restrictionManager->verifyAccess();
8282
$restrictionManager->setupRestrictions();
8383
} else {
84-
$userSession->listen('\OC\User', 'postLogin', function () use ($restrictionManager) {
84+
$userSession->listen('\OC\User', 'postLogin', function () use ($restrictionManager): void {
8585
$restrictionManager->verifyAccess();
8686
$restrictionManager->setupRestrictions();
8787
});

lib/AppWhitelist.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,23 @@
2222
* @package OCA\Guests
2323
*/
2424
class AppWhitelist {
25-
private string $baseUrl;
26-
private int $baseUrlLength;
25+
private readonly string $baseUrl;
26+
private readonly int $baseUrlLength;
2727

2828
public const WHITELIST_ALWAYS = ',core,theming,settings,avatar,files,heartbeat,dav,guests,impersonate,accessibility,terms_of_service,dashboard,weather_status,user_status,apporder,twofactor_totp,twofactor_webauthn,twofactor_backupcodes,twofactor_nextcloud_notification';
2929

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

3232
/**
3333
* 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
4134
*/
4235
public function __construct(
4336
IURLGenerator $urlGenerator,
44-
private Config $config,
45-
private GuestManager $guestManager,
46-
private IL10N $l10n,
47-
private IAppManager $appManager,
48-
private LoggerInterface $logger,
37+
private readonly Config $config,
38+
private readonly GuestManager $guestManager,
39+
private readonly IL10N $l10n,
40+
private readonly IAppManager $appManager,
41+
private readonly LoggerInterface $logger,
4942
) {
5043
$this->baseUrl = $urlGenerator->getBaseUrl();
5144
$this->baseUrlLength = strlen($this->baseUrl);
@@ -62,9 +55,6 @@ public function isWhitelistEnabled(): bool {
6255
return $this->config->useWhitelist();
6356
}
6457

65-
/**
66-
* @param false|string $url
67-
*/
6858
public function isUrlAllowed(IUser $user, string|false $url): bool {
6959
if ($this->guestManager->isGuest($user) && $this->isWhitelistEnabled()) {
7060
$app = $this->getRequestedApp($url);
@@ -98,45 +88,43 @@ public function verifyAccess(IUser $user, IRequest $request): void {
9888
/**
9989
* Core has \OC::$REQUESTEDAPP but it isn't set until the routes are matched
10090
* taken from \OC\Route\Router::match()
101-
*
102-
* @param false|string $url
10391
*/
10492
private function getRequestedApp(string|false $url): string {
10593
if (substr($url, 0, $this->baseUrlLength) === $this->baseUrl) {
10694
$url = substr($url, $this->baseUrlLength);
10795
}
108-
if (strpos($url, '/index.php/') === 0) {
96+
if (str_starts_with($url, '/index.php/')) {
10997
$url = substr($url, 10);
11098
}
111-
if (substr($url, 0, 6) === '/apps/') {
99+
if (str_starts_with($url, '/apps/')) {
112100
// empty string / 'apps' / $app / rest of the route
113101
[, , $app,] = explode('/', $url, 4);
114102
return \OC_App::cleanAppId($app);
115103
} elseif ($url === '/cron.php') {
116104
return 'core';
117-
} elseif (substr($url, 0, 6) === '/core/') {
105+
} elseif (str_starts_with($url, '/core/')) {
118106
return 'core';
119-
} elseif (substr($url, 0, 4) === '/js/') {
107+
} elseif (str_starts_with($url, '/js/')) {
120108
return 'core';
121-
} elseif (substr($url, 0, 5) === '/css/') {
109+
} elseif (str_starts_with($url, '/css/')) {
122110
return 'core';
123-
} elseif (substr($url, 0, 6) === '/login') {
111+
} elseif (str_starts_with($url, '/login')) {
124112
return 'core';
125-
} elseif (substr($url, 0, 7) === '/logout') {
113+
} elseif (str_starts_with($url, '/logout')) {
126114
return 'core';
127-
} elseif (substr($url, 0, 3) === '/f/') {
115+
} elseif (str_starts_with($url, '/f/')) {
128116
return 'files';
129-
} elseif (substr($url, 0, 8) === '/webdav/') {
117+
} elseif (str_starts_with($url, '/webdav/')) {
130118
return 'dav';
131-
} elseif (substr($url, 0, 5) === '/dav/') {
119+
} elseif (str_starts_with($url, '/dav/')) {
132120
return 'dav';
133-
} elseif (substr($url, 0, 6) === '/call/') {
121+
} elseif (str_starts_with($url, '/call/')) {
134122
return 'spreed';
135-
} elseif (substr($url, 0, 10) === '/settings/') {
123+
} elseif (str_starts_with($url, '/settings/')) {
136124
return 'settings';
137-
} elseif (substr($url, 0, 8) === '/avatar/') {
125+
} elseif (str_starts_with($url, '/avatar/')) {
138126
return 'avatar';
139-
} elseif (substr($url, 0, 10) === '/heartbeat') {
127+
} elseif (str_starts_with($url, '/heartbeat')) {
140128
return 'heartbeat';
141129
}
142130
return 'core';

lib/BackgroundJob/TransferJob.php

Lines changed: 7 additions & 7 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
}

lib/Capabilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Capabilities implements ICapability {
1919
/**
2020
* @inheritDoc
2121
*/
22-
public function getCapabilities() {
22+
public function getCapabilities(): array {
2323
return [
2424
'guests' => [
2525
'enabled' => true,

lib/Command/AddCommand.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace OCA\Guests\Command;
1010

11-
use OCA\Guests\GuestManager;
1211
use OCP\IUser;
1312
use OCP\IUserManager;
1413
use OCP\Mail\IMailer;
@@ -19,23 +18,21 @@
1918
use Symfony\Component\Console\Input\InputOption;
2019
use Symfony\Component\Console\Output\OutputInterface;
2120
use Symfony\Component\Console\Question\Question;
21+
use Override;
2222

2323
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) {
24+
public function __construct(
25+
private readonly IUserManager $userManager,
26+
private readonly IMailer $mailer,
27+
private readonly \OCA\Guests\GuestManager $guestManager,
28+
) {
3229
parent::__construct();
3330
$this->userManager = $userManager;
34-
$this->guestManager = $guestManager;
3531
$this->mailer = $mailer;
3632
}
3733

38-
protected function configure() {
34+
#[Override]
35+
protected function configure(): void {
3936
$this
4037
->setName('guests:add')
4138
->setDescription('Add a new guest account')
@@ -78,24 +75,25 @@ protected function configure() {
7875
parent::configure();
7976
}
8077

81-
protected function execute(InputInterface $input, OutputInterface $output) {
78+
#[Override]
79+
protected function execute(InputInterface $input, OutputInterface $output): int {
8280
$creatorUser = $this->userManager->get($input->getArgument('created-by'));
8381
if ($creatorUser === null) {
8482
$output->writeln('<error>The user "' . $input->getArgument('created-by') . '" does not exist.</error>');
85-
return 1;
83+
return self::FAILURE;
8684
}
8785

8886
// same behavior like in the UsersController
8987
$uid = $input->getArgument('email');
9088
if ($this->userManager->userExists($uid)) {
9189
$output->writeln('<error>The user "' . $uid . '" already exists.</error>');
92-
return 1;
90+
return self::FAILURE;
9391
}
9492

9593
$email = $input->getArgument('email');
9694
if (!$this->mailer->validateMailAddress($email)) {
9795
$output->writeln('<error>Invalid email address "' . $email . '".</error>');
98-
return 1;
96+
return self::FAILURE;
9997
}
10098

10199
$password = null;
@@ -104,7 +102,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
104102
$password = getenv('OC_PASS');
105103
if (!$password) {
106104
$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
107-
return 1;
105+
return self::FAILURE;
108106
}
109107
} elseif ($input->isInteractive()) {
110108
/** @var QuestionHelper $helper */
@@ -120,11 +118,11 @@ protected function execute(InputInterface $input, OutputInterface $output) {
120118

121119
if ($password !== $confirm) {
122120
$output->writeln('<error>Passwords did not match!</error>');
123-
return 1;
121+
return self::FAILURE;
124122
}
125123
} else {
126124
$output->writeln('<error>Interactive input or --password-from-env is needed for entering a password!</error>');
127-
return 1;
125+
return self::FAILURE;
128126
}
129127
}
130128

@@ -139,10 +137,10 @@ protected function execute(InputInterface $input, OutputInterface $output) {
139137

140138
if ($user instanceof IUser) {
141139
$output->writeln('<info>The guest account user "' . $user->getUID() . '" was created successfully</info>');
142-
return 0;
140+
return self::SUCCESS;
143141
} else {
144142
$output->writeln('<error>An error occurred while creating the user</error>');
145-
return 1;
143+
return self::FAILURE;
146144
}
147145
}
148146
}

lib/Command/ListCommand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
namespace OCA\Guests\Command;
1010

1111
use OC\Core\Command\Base;
12-
use OCA\Guests\GuestManager;
1312
use Symfony\Component\Console\Helper\Table;
1413
use Symfony\Component\Console\Input\InputInterface;
1514
use Symfony\Component\Console\Output\OutputInterface;
1615

1716
class ListCommand extends Base {
18-
private $guestManager;
19-
20-
public function __construct(GuestManager $guestManager) {
17+
public function __construct(
18+
private readonly \OCA\Guests\GuestManager $guestManager,
19+
) {
2120
parent::__construct();
22-
$this->guestManager = $guestManager;
2321
}
2422

2523
protected function configure(): void {

lib/Config.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
class Config {
1818
public function __construct(
19-
private IConfig $config,
20-
private IAppConfig $appConfig,
21-
private ISubAdmin $subAdmin,
22-
private IUserSession $userSession,
23-
private IGroupManager $groupManager,
19+
private readonly IConfig $config,
20+
private readonly IAppConfig $appConfig,
21+
private readonly ISubAdmin $subAdmin,
22+
private readonly IUserSession $userSession,
23+
private readonly IGroupManager $groupManager,
2424
) {
2525
}
2626

0 commit comments

Comments
 (0)