From 9eedeb401211c7210228b2c87674066d862d0329 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Sun, 4 Jun 2023 23:30:58 +0330 Subject: [PATCH 1/3] Refactors controllers by using PHP8's constructor property promotion. Signed-off-by: Faraz Samapoor --- core/Controller/SearchController.php | 16 ++------ core/Controller/SetupController.php | 7 +--- core/Controller/TranslationApiController.php | 22 ++++------- .../TwoFactorChallengeController.php | 20 ++++------ core/Controller/UnifiedSearchController.php | 18 ++------- core/Controller/UserController.php | 8 +--- core/Controller/WebAuthnController.php | 20 ++++------ core/Controller/WellKnownController.php | 6 +-- core/Controller/WhatsNewController.php | 38 +++++-------------- core/Controller/WipeController.php | 7 +--- 10 files changed, 44 insertions(+), 118 deletions(-) diff --git a/core/Controller/SearchController.php b/core/Controller/SearchController.php index 5881ec2db86..39df2503840 100644 --- a/core/Controller/SearchController.php +++ b/core/Controller/SearchController.php @@ -34,19 +34,11 @@ use OCP\Search\Result; use Psr\Log\LoggerInterface; class SearchController extends Controller { - private ISearch $searcher; - private LoggerInterface $logger; - - public function __construct( - string $appName, - IRequest $request, - ISearch $search, - LoggerInterface $logger - ) { + public function __construct(string $appName, + IRequest $request, + private ISearch $searcher, + private LoggerInterface $logger) { parent::__construct($appName, $request); - - $this->searcher = $search; - $this->logger = $logger; } /** diff --git a/core/Controller/SetupController.php b/core/Controller/SetupController.php index cdab39edf84..634ae93efb5 100644 --- a/core/Controller/SetupController.php +++ b/core/Controller/SetupController.php @@ -35,15 +35,10 @@ use OC\Setup; use OCP\ILogger; class SetupController { - protected Setup $setupHelper; private string $autoConfigFile; - /** - * @param Setup $setupHelper - */ - public function __construct(Setup $setupHelper) { + public function __construct(protected Setup $setupHelper) { $this->autoConfigFile = \OC::$configDir.'autoconfig.php'; - $this->setupHelper = $setupHelper; } public function run(array $post): void { diff --git a/core/Controller/TranslationApiController.php b/core/Controller/TranslationApiController.php index 2d47d99e654..734834d15c9 100644 --- a/core/Controller/TranslationApiController.php +++ b/core/Controller/TranslationApiController.php @@ -36,19 +36,11 @@ use OCP\Translation\CouldNotTranslateException; use OCP\Translation\ITranslationManager; class TranslationApiController extends \OCP\AppFramework\OCSController { - private ITranslationManager $translationManager; - private IL10N $l; - - public function __construct( - string $appName, - IRequest $request, - ITranslationManager $translationManager, - IL10N $l, - ) { + public function __construct(string $appName, + IRequest $request, + private ITranslationManager $translationManager, + private IL10N $l10n) { parent::__construct($appName, $request); - - $this->translationManager = $translationManager; - $this->l = $l; } /** @@ -76,11 +68,11 @@ class TranslationApiController extends \OCP\AppFramework\OCSController { ]); } catch (PreConditionNotMetException) { - return new DataResponse(['message' => $this->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED); + return new DataResponse(['message' => $this->l10n->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED); } catch (InvalidArgumentException) { - return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST); + return new DataResponse(['message' => $this->l10n->t('Could not detect language')], Http::STATUS_BAD_REQUEST); } catch (CouldNotTranslateException $e) { - return new DataResponse(['message' => $this->l->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST); + return new DataResponse(['message' => $this->l10n->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST); } } } diff --git a/core/Controller/TwoFactorChallengeController.php b/core/Controller/TwoFactorChallengeController.php index 7a57d5eeb1a..c8fb6da79d3 100644 --- a/core/Controller/TwoFactorChallengeController.php +++ b/core/Controller/TwoFactorChallengeController.php @@ -42,20 +42,14 @@ use OCP\IUserSession; use Psr\Log\LoggerInterface; class TwoFactorChallengeController extends Controller { - private Manager $twoFactorManager; - private IUserSession $userSession; - private ISession $session; - private LoggerInterface $logger; - private IURLGenerator $urlGenerator; - - public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, - ISession $session, IURLGenerator $urlGenerator, LoggerInterface $logger) { + public function __construct(string $appName, + IRequest $request, + private Manager $twoFactorManager, + private IUserSession $userSession, + private ISession $session, + private IURLGenerator $urlGenerator, + private LoggerInterface $logger) { parent::__construct($appName, $request); - $this->twoFactorManager = $twoFactorManager; - $this->userSession = $userSession; - $this->session = $session; - $this->urlGenerator = $urlGenerator; - $this->logger = $logger; } /** diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index 3290307dc23..a41fa6206b2 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -40,22 +40,12 @@ use OCP\Search\ISearchQuery; use Symfony\Component\Routing\Exception\ResourceNotFoundException; class UnifiedSearchController extends OCSController { - private SearchComposer $composer; - private IUserSession $userSession; - private IRouter $router; - private IURLGenerator $urlGenerator; - public function __construct(IRequest $request, - IUserSession $userSession, - SearchComposer $composer, - IRouter $router, - IURLGenerator $urlGenerator) { + private IUserSession $userSession, + private SearchComposer $composer, + private IRouter $router, + private IURLGenerator $urlGenerator) { parent::__construct('core', $request); - - $this->composer = $composer; - $this->userSession = $userSession; - $this->router = $router; - $this->urlGenerator = $urlGenerator; } /** diff --git a/core/Controller/UserController.php b/core/Controller/UserController.php index b9946dfaec6..51b31afd18f 100644 --- a/core/Controller/UserController.php +++ b/core/Controller/UserController.php @@ -30,14 +30,10 @@ use OCP\IRequest; use OCP\IUserManager; class UserController extends Controller { - protected IUserManager $userManager; - - public function __construct($appName, + public function __construct(string $appName, IRequest $request, - IUserManager $userManager - ) { + protected IUserManager $userManager) { parent::__construct($appName, $request); - $this->userManager = $userManager; } /** diff --git a/core/Controller/WebAuthnController.php b/core/Controller/WebAuthnController.php index bd0726d2aa2..d4ee5637598 100644 --- a/core/Controller/WebAuthnController.php +++ b/core/Controller/WebAuthnController.php @@ -45,20 +45,14 @@ class WebAuthnController extends Controller { private const WEBAUTHN_LOGIN = 'webauthn_login'; private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid'; - private Manager $webAuthnManger; - private ISession $session; - private LoggerInterface $logger; - private WebAuthnChain $webAuthnChain; - private UrlGenerator $urlGenerator; - - public function __construct($appName, IRequest $request, Manager $webAuthnManger, ISession $session, LoggerInterface $logger, WebAuthnChain $webAuthnChain, URLGenerator $urlGenerator) { + public function __construct(string $appName, + IRequest $request, + private Manager $webAuthnManger, + private ISession $session, + private LoggerInterface $logger, + private WebAuthnChain $webAuthnChain, + private URLGenerator $urlGenerator) { parent::__construct($appName, $request); - - $this->webAuthnManger = $webAuthnManger; - $this->session = $session; - $this->logger = $logger; - $this->webAuthnChain = $webAuthnChain; - $this->urlGenerator = $urlGenerator; } /** diff --git a/core/Controller/WellKnownController.php b/core/Controller/WellKnownController.php index 01ae5e4fae8..ea5e77b74bf 100644 --- a/core/Controller/WellKnownController.php +++ b/core/Controller/WellKnownController.php @@ -33,13 +33,9 @@ use OCP\AppFramework\Http\Response; use OCP\IRequest; class WellKnownController extends Controller { - /** @var RequestManager */ - private $requestManager; - public function __construct(IRequest $request, - RequestManager $wellKnownManager) { + private RequestManager $requestManager) { parent::__construct('core', $request); - $this->requestManager = $wellKnownManager; } /** diff --git a/core/Controller/WhatsNewController.php b/core/Controller/WhatsNewController.php index 0dae0f97322..39939a02a63 100644 --- a/core/Controller/WhatsNewController.php +++ b/core/Controller/WhatsNewController.php @@ -37,35 +37,17 @@ use OCP\IUserSession; use OCP\L10N\IFactory; class WhatsNewController extends OCSController { - /** @var IConfig */ - protected $config; - /** @var IUserSession */ - private $userSession; - /** @var ChangesCheck */ - private $whatsNewService; - /** @var IFactory */ - private $langFactory; - /** @var Defaults */ - private $defaults; - - public function __construct( - string $appName, - IRequest $request, - CapabilitiesManager $capabilitiesManager, - IUserSession $userSession, - IUserManager $userManager, - Manager $keyManager, - IConfig $config, - ChangesCheck $whatsNewService, - IFactory $langFactory, - Defaults $defaults - ) { + public function __construct(string $appName, + IRequest $request, + CapabilitiesManager $capabilitiesManager, + private IUserSession $userSession, + IUserManager $userManager, + Manager $keyManager, + private IConfig $config, + private ChangesCheck $whatsNewService, + private IFactory $langFactory, + private Defaults $defaults) { parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager); - $this->config = $config; - $this->userSession = $userSession; - $this->whatsNewService = $whatsNewService; - $this->langFactory = $langFactory; - $this->defaults = $defaults; } /** diff --git a/core/Controller/WipeController.php b/core/Controller/WipeController.php index 44ec5fc598a..70bd66e2d8b 100644 --- a/core/Controller/WipeController.php +++ b/core/Controller/WipeController.php @@ -33,15 +33,10 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; class WipeController extends Controller { - /** @var RemoteWipe */ - private $remoteWipe; - public function __construct(string $appName, IRequest $request, - RemoteWipe $remoteWipe) { + private RemoteWipe $remoteWipe) { parent::__construct($appName, $request); - - $this->remoteWipe = $remoteWipe; } /** From 2800436948acbceb45362fc5cab7b2f5257caccd Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Mon, 5 Jun 2023 18:47:57 +0330 Subject: [PATCH 2/3] Applies agreed-upon indentation convention to the changed controllers. Based on https://github.com/nextcloud/server/pull/38636#discussion_r1218167753 Signed-off-by: Faraz Samapoor --- core/Controller/SearchController.php | 10 +++++---- core/Controller/TranslationApiController.php | 10 +++++---- .../TwoFactorChallengeController.php | 16 ++++++++------ core/Controller/UnifiedSearchController.php | 12 +++++----- core/Controller/UserController.php | 8 ++++--- core/Controller/WebAuthnController.php | 16 ++++++++------ core/Controller/WellKnownController.php | 6 +++-- core/Controller/WhatsNewController.php | 22 ++++++++++--------- core/Controller/WipeController.php | 8 ++++--- 9 files changed, 63 insertions(+), 45 deletions(-) diff --git a/core/Controller/SearchController.php b/core/Controller/SearchController.php index 39df2503840..f839c16e8da 100644 --- a/core/Controller/SearchController.php +++ b/core/Controller/SearchController.php @@ -34,10 +34,12 @@ use OCP\Search\Result; use Psr\Log\LoggerInterface; class SearchController extends Controller { - public function __construct(string $appName, - IRequest $request, - private ISearch $searcher, - private LoggerInterface $logger) { + public function __construct( + string $appName, + IRequest $request, + private ISearch $searcher, + private LoggerInterface $logger, + ) { parent::__construct($appName, $request); } diff --git a/core/Controller/TranslationApiController.php b/core/Controller/TranslationApiController.php index 734834d15c9..c1628c24555 100644 --- a/core/Controller/TranslationApiController.php +++ b/core/Controller/TranslationApiController.php @@ -36,10 +36,12 @@ use OCP\Translation\CouldNotTranslateException; use OCP\Translation\ITranslationManager; class TranslationApiController extends \OCP\AppFramework\OCSController { - public function __construct(string $appName, - IRequest $request, - private ITranslationManager $translationManager, - private IL10N $l10n) { + public function __construct( + string $appName, + IRequest $request, + private ITranslationManager $translationManager, + private IL10N $l10n, + ) { parent::__construct($appName, $request); } diff --git a/core/Controller/TwoFactorChallengeController.php b/core/Controller/TwoFactorChallengeController.php index c8fb6da79d3..40b100c41bd 100644 --- a/core/Controller/TwoFactorChallengeController.php +++ b/core/Controller/TwoFactorChallengeController.php @@ -42,13 +42,15 @@ use OCP\IUserSession; use Psr\Log\LoggerInterface; class TwoFactorChallengeController extends Controller { - public function __construct(string $appName, - IRequest $request, - private Manager $twoFactorManager, - private IUserSession $userSession, - private ISession $session, - private IURLGenerator $urlGenerator, - private LoggerInterface $logger) { + public function __construct( + string $appName, + IRequest $request, + private Manager $twoFactorManager, + private IUserSession $userSession, + private ISession $session, + private IURLGenerator $urlGenerator, + private LoggerInterface $logger, + ) { parent::__construct($appName, $request); } diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index a41fa6206b2..7e73ac8100f 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -40,11 +40,13 @@ use OCP\Search\ISearchQuery; use Symfony\Component\Routing\Exception\ResourceNotFoundException; class UnifiedSearchController extends OCSController { - public function __construct(IRequest $request, - private IUserSession $userSession, - private SearchComposer $composer, - private IRouter $router, - private IURLGenerator $urlGenerator) { + public function __construct( + IRequest $request, + private IUserSession $userSession, + private SearchComposer $composer, + private IRouter $router, + private IURLGenerator $urlGenerator, + ) { parent::__construct('core', $request); } diff --git a/core/Controller/UserController.php b/core/Controller/UserController.php index 51b31afd18f..f8dbc1af027 100644 --- a/core/Controller/UserController.php +++ b/core/Controller/UserController.php @@ -30,9 +30,11 @@ use OCP\IRequest; use OCP\IUserManager; class UserController extends Controller { - public function __construct(string $appName, - IRequest $request, - protected IUserManager $userManager) { + public function __construct( + string $appName, + IRequest $request, + protected IUserManager $userManager, + ) { parent::__construct($appName, $request); } diff --git a/core/Controller/WebAuthnController.php b/core/Controller/WebAuthnController.php index d4ee5637598..08a6b36d276 100644 --- a/core/Controller/WebAuthnController.php +++ b/core/Controller/WebAuthnController.php @@ -45,13 +45,15 @@ class WebAuthnController extends Controller { private const WEBAUTHN_LOGIN = 'webauthn_login'; private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid'; - public function __construct(string $appName, - IRequest $request, - private Manager $webAuthnManger, - private ISession $session, - private LoggerInterface $logger, - private WebAuthnChain $webAuthnChain, - private URLGenerator $urlGenerator) { + public function __construct( + string $appName, + IRequest $request, + private Manager $webAuthnManger, + private ISession $session, + private LoggerInterface $logger, + private WebAuthnChain $webAuthnChain, + private URLGenerator $urlGenerator, + ) { parent::__construct($appName, $request); } diff --git a/core/Controller/WellKnownController.php b/core/Controller/WellKnownController.php index ea5e77b74bf..2e317ae01b5 100644 --- a/core/Controller/WellKnownController.php +++ b/core/Controller/WellKnownController.php @@ -33,8 +33,10 @@ use OCP\AppFramework\Http\Response; use OCP\IRequest; class WellKnownController extends Controller { - public function __construct(IRequest $request, - private RequestManager $requestManager) { + public function __construct( + IRequest $request, + private RequestManager $requestManager, + ) { parent::__construct('core', $request); } diff --git a/core/Controller/WhatsNewController.php b/core/Controller/WhatsNewController.php index 39939a02a63..0791ce616f5 100644 --- a/core/Controller/WhatsNewController.php +++ b/core/Controller/WhatsNewController.php @@ -37,16 +37,18 @@ use OCP\IUserSession; use OCP\L10N\IFactory; class WhatsNewController extends OCSController { - public function __construct(string $appName, - IRequest $request, - CapabilitiesManager $capabilitiesManager, - private IUserSession $userSession, - IUserManager $userManager, - Manager $keyManager, - private IConfig $config, - private ChangesCheck $whatsNewService, - private IFactory $langFactory, - private Defaults $defaults) { + public function __construct( + string $appName, + IRequest $request, + CapabilitiesManager $capabilitiesManager, + private IUserSession $userSession, + IUserManager $userManager, + Manager $keyManager, + private IConfig $config, + private ChangesCheck $whatsNewService, + private IFactory $langFactory, + private Defaults $defaults, + ) { parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager); } diff --git a/core/Controller/WipeController.php b/core/Controller/WipeController.php index 70bd66e2d8b..6ffb950ca86 100644 --- a/core/Controller/WipeController.php +++ b/core/Controller/WipeController.php @@ -33,9 +33,11 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; class WipeController extends Controller { - public function __construct(string $appName, - IRequest $request, - private RemoteWipe $remoteWipe) { + public function __construct( + string $appName, + IRequest $request, + private RemoteWipe $remoteWipe, + ) { parent::__construct($appName, $request); } From 4ce7173f7ed22a25dd8149421ca778e47a3b3c46 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Tue, 20 Jun 2023 11:47:04 +0330 Subject: [PATCH 3/3] Update core/Controller/SetupController.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com> Signed-off-by: Faraz Samapoor --- core/Controller/SetupController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/Controller/SetupController.php b/core/Controller/SetupController.php index 634ae93efb5..69c3b2f2a23 100644 --- a/core/Controller/SetupController.php +++ b/core/Controller/SetupController.php @@ -37,7 +37,9 @@ use OCP\ILogger; class SetupController { private string $autoConfigFile; - public function __construct(protected Setup $setupHelper) { + public function __construct( + protected Setup $setupHelper, + ) { $this->autoConfigFile = \OC::$configDir.'autoconfig.php'; }