Skip to content

Commit

Permalink
refactor: Some PHP8+ optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
solracsf committed Jan 24, 2025
1 parent df4e92b commit 7227a48
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 137 deletions.
13 changes: 4 additions & 9 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions lib/Activity/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 3 additions & 15 deletions lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 10 additions & 16 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@

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;
}

/**
* @NoAdminRequired
* @return JSONResponse
*/
public function state(): JSONResponse {
$user = $this->userSession->getUser();
$user = $this->userSession?->getUser();

Check failure on line 39 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

TypeDoesNotContainNull

lib/Controller/SettingsController.php:39:11: TypeDoesNotContainNull: OCP\IUserSession does not contain null (see https://psalm.dev/090)

Check failure on line 39 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantCondition

lib/Controller/SettingsController.php:39:11: RedundantCondition: Type OCP\IUserSession for $__tmp_nullsafe__847 is never null (see https://psalm.dev/122)
if (is_null($user)) {
throw new \Exception('user not available');
}
Expand All @@ -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();

Check failure on line 56 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

TypeDoesNotContainNull

lib/Controller/SettingsController.php:56:11: TypeDoesNotContainNull: OCP\IUserSession does not contain null (see https://psalm.dev/090)

Check failure on line 56 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantCondition

lib/Controller/SettingsController.php:56:11: RedundantCondition: Type OCP\IUserSession for $__tmp_nullsafe__1305 is never null (see https://psalm.dev/122)
if (is_null($user)) {
throw new \Exception('user not available');
}
Expand Down Expand Up @@ -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();

Check failure on line 97 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

TypeDoesNotContainNull

lib/Controller/SettingsController.php:97:11: TypeDoesNotContainNull: OCP\IUserSession does not contain null (see https://psalm.dev/090)

Check failure on line 97 in lib/Controller/SettingsController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantCondition

lib/Controller/SettingsController.php:97:11: RedundantCondition: Type OCP\IUserSession for $__tmp_nullsafe__2503 is never null (see https://psalm.dev/122)
if ($user === null) {
throw new RuntimeException('No user in this context');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/TotpSecretMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 4 additions & 16 deletions lib/Event/StateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/Listener/StateChangeActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 4 additions & 9 deletions lib/Listener/StateChangeRegistryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 6 additions & 11 deletions lib/Listener/UserDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check failure on line 33 in lib/Listener/UserDeleted.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

TypeDoesNotContainNull

lib/Listener/UserDeleted.php:33:51: TypeDoesNotContainNull: OCP\IUser does not contain null (see https://psalm.dev/090)

Check failure on line 33 in lib/Listener/UserDeleted.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantCondition

lib/Listener/UserDeleted.php:33:51: RedundantCondition: Type OCP\IUser for $__tmp_nullsafe__785 is never null (see https://psalm.dev/122)

Check failure on line 33 in lib/Listener/UserDeleted.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

PossiblyNullArgument

lib/Listener/UserDeleted.php:33:51: PossiblyNullArgument: Argument 1 of OCA\TwoFactorTOTP\Db\TotpSecretMapper::deleteSecretByUserId cannot be null, possibly null value provided (see https://psalm.dev/078)
} catch (Exception $e) {
$this->logger->warning($e->getMessage(), ['uid' => $event->getUser()->getUID()]);
$this->logger->warning($e->getMessage(), ['uid' => $event->getUser()?->getUID()]);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions lib/Provider/AtLoginProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 7 additions & 25 deletions lib/Provider/TotpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

/**
Expand Down
26 changes: 6 additions & 20 deletions lib/Service/Totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7227a48

Please sign in to comment.