From 7227a48bb28dc1017b516bed20373267d36c8498 Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:33:11 +0100 Subject: [PATCH] refactor: Some PHP8+ optimizations --- lib/Activity/Provider.php | 13 +++------ lib/Activity/Setting.php | 8 ++---- lib/Command/CleanUp.php | 18 ++---------- lib/Controller/SettingsController.php | 26 +++++++---------- lib/Db/TotpSecretMapper.php | 2 +- lib/Event/StateChanged.php | 20 +++---------- lib/Listener/StateChangeActivity.php | 8 ++---- lib/Listener/StateChangeRegistryUpdater.php | 13 +++------ lib/Listener/UserDeleted.php | 17 ++++------- lib/Provider/AtLoginProvider.php | 8 ++---- lib/Provider/TotpProvider.php | 32 +++++---------------- lib/Service/Totp.php | 26 ++++------------- 12 files changed, 54 insertions(+), 137 deletions(-) diff --git a/lib/Activity/Provider.php b/lib/Activity/Provider.php index b1e470c9..8e5447b7 100644 --- a/lib/Activity/Provider.php +++ b/lib/Activity/Provider.php @@ -17,15 +17,10 @@ class Provider implements IProvider { - /** @var L10nFactory */ - private $l10n; - - /** @var IURLGenerator */ - private $urlGenerator; - - public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator) { - $this->urlGenerator = $urlGenerator; - $this->l10n = $l10n; + public function __construct( + private L10nFactory $l10n, + private IURLGenerator $urlGenerator, + ) { } public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent { diff --git a/lib/Activity/Setting.php b/lib/Activity/Setting.php index 7cf6ccd0..0ecf73f5 100644 --- a/lib/Activity/Setting.php +++ b/lib/Activity/Setting.php @@ -14,11 +14,9 @@ class Setting implements ISetting { - /** @var IL10N */ - private $l10n; - - public function __construct(IL10N $l10n) { - $this->l10n = $l10n; + public function __construct( + private IL10N $l10n, + ) { } public function canChangeMail(): bool { diff --git a/lib/Command/CleanUp.php b/lib/Command/CleanUp.php index a7b9cf14..41529cb8 100644 --- a/lib/Command/CleanUp.php +++ b/lib/Command/CleanUp.php @@ -19,25 +19,13 @@ use Symfony\Component\Console\Style\SymfonyStyle; class CleanUp extends Command { - /** @var IDBConnection */ - private $db; - - /** @var IUserManager */ - private $userManager; - - /** @var TotpSecretMapper */ - private $totpSecretMapper; public function __construct( - IDBConnection $db, - IUserManager $userManager, - TotpSecretMapper $totpSecretMapper, + private IDBConnection $db, + private IUserManager $userManager, + private TotpSecretMapper $totpSecretMapper, ) { parent::__construct(); - - $this->db = $db; - $this->userManager = $userManager; - $this->totpSecretMapper = $totpSecretMapper; } protected function configure(): void { diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 749492b4..595ba8c6 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -21,20 +21,14 @@ class SettingsController extends ALoginSetupController { - /** @var ITotp */ - private $totp; - - /** @var IUserSession */ - private $userSession; - - /** @var Defaults */ - private $defaults; - - public function __construct(string $appName, IRequest $request, IUserSession $userSession, ITotp $totp, Defaults $defaults) { + public function __construct( + string $appName, + IRequest $request, + private IUserSession $userSession, + private ITotp $totp, + private Defaults $defaults, + ) { parent::__construct($appName, $request); - $this->userSession = $userSession; - $this->totp = $totp; - $this->defaults = $defaults; } /** @@ -42,7 +36,7 @@ public function __construct(string $appName, IRequest $request, IUserSession $us * @return JSONResponse */ public function state(): JSONResponse { - $user = $this->userSession->getUser(); + $user = $this->userSession?->getUser(); if (is_null($user)) { throw new \Exception('user not available'); } @@ -59,7 +53,7 @@ public function state(): JSONResponse { * @param string|null $code for verification */ public function enable(int $state, ?string $code = null): JSONResponse { - $user = $this->userSession->getUser(); + $user = $this->userSession?->getUser(); if (is_null($user)) { throw new \Exception('user not available'); } @@ -100,7 +94,7 @@ public function enable(int $state, ?string $code = null): JSONResponse { */ private function getSecretName(): string { $productName = $this->defaults->getName(); - $user = $this->userSession->getUser(); + $user = $this->userSession?->getUser(); if ($user === null) { throw new RuntimeException('No user in this context'); } diff --git a/lib/Db/TotpSecretMapper.php b/lib/Db/TotpSecretMapper.php index 2f78e96d..63e9222c 100644 --- a/lib/Db/TotpSecretMapper.php +++ b/lib/Db/TotpSecretMapper.php @@ -39,7 +39,7 @@ public function getSecret(IUser $user): TotpSecret { ->from('twofactor_totp_secrets') ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); /** @var Statement $result */ - $result = $qb->execute(); + $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); diff --git a/lib/Event/StateChanged.php b/lib/Event/StateChanged.php index c9f982e1..7d85f20e 100644 --- a/lib/Event/StateChanged.php +++ b/lib/Event/StateChanged.php @@ -14,29 +14,17 @@ class StateChanged extends Event { - /** @var IUser */ - private $user; - - /** @var bool */ - private $enabled; - - public function __construct(IUser $user, bool $enabled) { + public function __construct( + private IUser $user, + private bool $enabled, + ) { parent::__construct(); - - $this->user = $user; - $this->enabled = $enabled; } - /** - * @return IUser - */ public function getUser(): IUser { return $this->user; } - /** - * @return bool - */ public function isEnabled(): bool { return $this->enabled; } diff --git a/lib/Listener/StateChangeActivity.php b/lib/Listener/StateChangeActivity.php index 81d2651c..f17073a9 100644 --- a/lib/Listener/StateChangeActivity.php +++ b/lib/Listener/StateChangeActivity.php @@ -20,11 +20,9 @@ */ class StateChangeActivity implements IEventListener { - /** @var ActivityManager */ - private $activityManager; - - public function __construct(ActivityManager $activityManager) { - $this->activityManager = $activityManager; + public function __construct( + private ActivityManager $activityManager, + ) { } public function handle(Event $event): void { diff --git a/lib/Listener/StateChangeRegistryUpdater.php b/lib/Listener/StateChangeRegistryUpdater.php index f6212fe1..7e0fec1d 100644 --- a/lib/Listener/StateChangeRegistryUpdater.php +++ b/lib/Listener/StateChangeRegistryUpdater.php @@ -20,15 +20,10 @@ */ class StateChangeRegistryUpdater implements IEventListener { - /** @var IRegistry */ - private $registry; - - /** @var TotpProvider */ - private $provider; - - public function __construct(IRegistry $registry, TotpProvider $provider) { - $this->registry = $registry; - $this->provider = $provider; + public function __construct( + private IRegistry $registry, + private TotpProvider $provider, + ) { } public function handle(Event $event): void { diff --git a/lib/Listener/UserDeleted.php b/lib/Listener/UserDeleted.php index 3b571702..7e9d0876 100644 --- a/lib/Listener/UserDeleted.php +++ b/lib/Listener/UserDeleted.php @@ -21,23 +21,18 @@ */ class UserDeleted implements IEventListener { - /** @var TotpSecretMapper */ - private $totpSecretMapper; - - /** @var LoggerInterface */ - private $logger; - - public function __construct(TotpSecretMapper $totpSecretMapper, LoggerInterface $logger) { - $this->totpSecretMapper = $totpSecretMapper; - $this->logger = $logger; + public function __construct( + private TotpSecretMapper $totpSecretMapper, + private LoggerInterface $logger, + ) { } public function handle(Event $event): void { if ($event instanceof UserDeletedEvent) { try { - $this->totpSecretMapper->deleteSecretByUserId($event->getUser()->getUID()); + $this->totpSecretMapper->deleteSecretByUserId($event->getUser()?->getUID()); } catch (Exception $e) { - $this->logger->warning($e->getMessage(), ['uid' => $event->getUser()->getUID()]); + $this->logger->warning($e->getMessage(), ['uid' => $event->getUser()?->getUID()]); } } } diff --git a/lib/Provider/AtLoginProvider.php b/lib/Provider/AtLoginProvider.php index 07bfaaa4..e6d83826 100644 --- a/lib/Provider/AtLoginProvider.php +++ b/lib/Provider/AtLoginProvider.php @@ -16,11 +16,9 @@ class AtLoginProvider implements ILoginSetupProvider { - /** @var IURLGenerator */ - private $urlGenerator; - - public function __construct(IURLGenerator $urlGenerator) { - $this->urlGenerator = $urlGenerator; + public function __construct( + private IURLGenerator $urlGenerator, + ) { } public function getBody(): Template { diff --git a/lib/Provider/TotpProvider.php b/lib/Provider/TotpProvider.php index 02b83793..c1aa0589 100644 --- a/lib/Provider/TotpProvider.php +++ b/lib/Provider/TotpProvider.php @@ -28,31 +28,13 @@ class TotpProvider implements IProvider, IProvidesIcons, IProvidesPersonalSettings, IDeactivatableByAdmin, IActivatableAtLogin { - /** @var ITotp */ - private $totp; - - /** @var IL10N */ - private $l10n; - - /** @var IAppContainer */ - private $container; - - /** @var IInitialState */ - private $initialState; - - /** @var IURLGenerator */ - private $urlGenerator; - - public function __construct(ITotp $totp, - IL10N $l10n, - IAppContainer $container, - IInitialState $initialStateService, - IURLGenerator $urlGenerator) { - $this->totp = $totp; - $this->l10n = $l10n; - $this->container = $container; - $this->initialState = $initialStateService; - $this->urlGenerator = $urlGenerator; + public function __construct( + private ITotp $totp, + private IL10N $l10n, + private IAppContainer $container, + private IInitialState $initialStateService, + private IURLGenerator $urlGenerator, + ) { } /** diff --git a/lib/Service/Totp.php b/lib/Service/Totp.php index e6c82374..b88d89a8 100644 --- a/lib/Service/Totp.php +++ b/lib/Service/Totp.php @@ -25,26 +25,12 @@ class Totp implements ITotp { - /** @var TotpSecretMapper */ - private $secretMapper; - - /** @var ICrypto */ - private $crypto; - - /** @var IEventDispatcher */ - private $eventDispatcher; - - /** @var ISecureRandom */ - private $random; - - public function __construct(TotpSecretMapper $secretMapper, - ICrypto $crypto, - IEventDispatcher $eventDispatcher, - ISecureRandom $random) { - $this->secretMapper = $secretMapper; - $this->crypto = $crypto; - $this->eventDispatcher = $eventDispatcher; - $this->random = $random; + public function __construct( + private TotpSecretMapper $secretMapper, + private ICrypto $crypto, + private IEventDispatcher $eventDispatcher, + private ISecureRandom $random, + ) { } public function hasSecret(IUser $user): bool {