From 6d6d83d3d9ec355e2d99d5ddc2ef1ee9b4e1d3ba Mon Sep 17 00:00:00 2001 From: provokateurin Date: Thu, 15 May 2025 00:39:44 +0200 Subject: [PATCH] refactor: Extend rector to all top-level files Signed-off-by: provokateurin --- build/rector.php | 7 +++++++ console.php | 13 ++++++++----- cron.php | 21 +++++++++++++-------- public.php | 4 +++- remote.php | 14 +++++++++----- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/build/rector.php b/build/rector.php index 03e6eab0e87..97c0e5d399b 100644 --- a/build/rector.php +++ b/build/rector.php @@ -53,7 +53,14 @@ $config = RectorConfig::configure() ->withPaths([ $nextcloudDir . '/apps', $nextcloudDir . '/core', + $nextcloudDir . '/console.php', + $nextcloudDir . '/cron.php', + $nextcloudDir . '/index.php', + $nextcloudDir . '/occ', + $nextcloudDir . '/public.php', + $nextcloudDir . '/remote.php', $nextcloudDir . '/status.php', + $nextcloudDir . '/version.php', // $nextcloudDir . '/config', // $nextcloudDir . '/lib', // $nextcloudDir . '/ocs', diff --git a/console.php b/console.php index 4fcb3d6c7a3..bdfe4b63254 100644 --- a/console.php +++ b/console.php @@ -2,6 +2,9 @@ declare(strict_types=1); +use OCP\IConfig; +use OCP\Server; + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -37,7 +40,7 @@ try { exit(1); } - $config = \OCP\Server::get(\OCP\IConfig::class); + $config = Server::get(IConfig::class); set_exception_handler('exceptionHandler'); if (!function_exists('posix_getuid')) { @@ -71,10 +74,10 @@ try { echo "Additionally the function 'pcntl_signal' and 'pcntl_signal_dispatch' need to be enabled in your php.ini." . PHP_EOL; } - $eventLogger = \OCP\Server::get(IEventLogger::class); + $eventLogger = Server::get(IEventLogger::class); $eventLogger->start('console:build_application', 'Build Application instance and load commands'); - $application = \OCP\Server::get(Application::class); + $application = Server::get(Application::class); /* base.php will have removed eventual debug options from argv in $_SERVER */ $argv = $_SERVER['argv']; $input = new ArgvInput($argv); @@ -88,10 +91,10 @@ try { $eventLogger->end('console:run'); - $profiler = \OCP\Server::get(IProfiler::class); + $profiler = Server::get(IProfiler::class); if ($profiler->isEnabled()) { $eventLogger->end('runtime'); - $profile = $profiler->collect(\OCP\Server::get(IRequest::class), new Response()); + $profile = $profiler->collect(Server::get(IRequest::class), new Response()); $profile->setMethod('occ'); $profile->setUrl(implode(' ', $argv)); $profiler->saveProfile($profile); diff --git a/cron.php b/cron.php index 993faae3cae..0501c53ff40 100644 --- a/cron.php +++ b/cron.php @@ -2,6 +2,11 @@ declare(strict_types=1); +use OC\Files\SetupManager; +use OC\Session\CryptoWrapper; +use OC\Session\Memory; +use OCP\ILogger; + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -64,8 +69,8 @@ Options: $verbose = isset($argv[1]) && ($argv[1] === '-v' || $argv[1] === '--verbose'); // initialize a dummy memory session - $session = new \OC\Session\Memory(); - $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); + $session = new Memory(); + $cryptoWrapper = Server::get(CryptoWrapper::class); $session = $cryptoWrapper->wrapSession($session); \OC::$server->setSession($session); @@ -177,11 +182,11 @@ Options: $timeSpent = $timeAfter - $timeBefore; if ($timeSpent > $cronInterval) { $logLevel = match (true) { - $timeSpent > $cronInterval * 128 => \OCP\ILogger::FATAL, - $timeSpent > $cronInterval * 64 => \OCP\ILogger::ERROR, - $timeSpent > $cronInterval * 16 => \OCP\ILogger::WARN, - $timeSpent > $cronInterval * 8 => \OCP\ILogger::INFO, - default => \OCP\ILogger::DEBUG, + $timeSpent > $cronInterval * 128 => ILogger::FATAL, + $timeSpent > $cronInterval * 64 => ILogger::ERROR, + $timeSpent > $cronInterval * 16 => ILogger::WARN, + $timeSpent > $cronInterval * 8 => ILogger::INFO, + default => ILogger::DEBUG, }; $logger->log( $logLevel, @@ -206,7 +211,7 @@ Options: } // clean up after unclean jobs - Server::get(\OC\Files\SetupManager::class)->tearDown(); + Server::get(SetupManager::class)->tearDown(); $tempManager->clean(); if ($verbose) { diff --git a/public.php b/public.php index 682d5cb2538..8ae6deff203 100644 --- a/public.php +++ b/public.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use OC\ServiceUnavailableException; + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -88,7 +90,7 @@ try { require_once $file; } catch (Exception $ex) { $status = 500; - if ($ex instanceof \OC\ServiceUnavailableException) { + if ($ex instanceof ServiceUnavailableException) { $status = 503; } //show the user a detailed error page diff --git a/remote.php b/remote.php index 7058e7aceb4..2cfd9d818c8 100644 --- a/remote.php +++ b/remote.php @@ -1,5 +1,9 @@ addPlugin(new ExceptionLoggerPlugin('webdav', \OCP\Server::get(LoggerInterface::class))); } - $server->on('beforeMethod:*', function () use ($e) { + $server->on('beforeMethod:*', function () use ($e): void { if ($e instanceof RemoteException) { switch ($e->getCode()) { case 503: @@ -51,7 +55,7 @@ function handleException(Exception|Error $e): void { $server->exec(); } else { $statusCode = 500; - if ($e instanceof \OC\ServiceUnavailableException) { + if ($e instanceof ServiceUnavailableException) { $statusCode = 503; } if ($e instanceof RemoteException) { @@ -86,7 +90,7 @@ function resolveService($service) { return $services[$service]; } - return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service); + return \OCP\Server::get(IConfig::class)->getAppValue('core', 'remote_' . $service); } try { @@ -97,13 +101,13 @@ try { // this policy with a softer one if debug mode is enabled. header("Content-Security-Policy: default-src 'none';"); - if (\OCP\Util::needUpgrade()) { + if (Util::needUpgrade()) { // since the behavior of apps or remotes are unpredictable during // an upgrade, return a 503 directly throw new RemoteException('Service unavailable', 503); } - $request = \OC::$server->getRequest(); + $request = \OCP\Server::get(IRequest::class); $pathInfo = $request->getPathInfo(); if ($pathInfo === false || $pathInfo === '') { throw new RemoteException('Path not found', 404);