From c6ebdca488db644e72ee00121cb867aae4accaa7 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Sun, 25 Jun 2023 13:58:30 +0330 Subject: [PATCH] Refactors lib/private/Calendar. Mainly using PHP8's constructor property promotion. Signed-off-by: Faraz Samapoor --- lib/private/Calendar/CalendarQuery.php | 31 ++++----- lib/private/Calendar/Manager.php | 80 +++++++++++------------ lib/private/Calendar/Resource/Manager.php | 31 ++++----- lib/private/Calendar/Room/Manager.php | 26 +++----- 4 files changed, 69 insertions(+), 99 deletions(-) diff --git a/lib/private/Calendar/CalendarQuery.php b/lib/private/Calendar/CalendarQuery.php index 64f74c2728b1a..b06220e3ce495 100644 --- a/lib/private/Calendar/CalendarQuery.php +++ b/lib/private/Calendar/CalendarQuery.php @@ -28,33 +28,24 @@ use OCP\Calendar\ICalendarQuery; class CalendarQuery implements ICalendarQuery { - /** @var string */ - private $principalUri; + public array $searchProperties = []; - /** @var array */ - public $searchProperties; + private ?string $searchPattern; - /** @var string|null */ - private $searchPattern; + private array $options = [ + 'types' => [], + ]; - /** @var array */ - private $options; + private ?int $offset; - /** @var int|null */ - private $offset; - - /** @var int|null */ - private $limit; + private ?int $limit; /** @var string[] */ - private $calendarUris = []; + private array $calendarUris = []; - public function __construct(string $principalUri) { - $this->principalUri = $principalUri; - $this->searchProperties = []; - $this->options = [ - 'types' => [], - ]; + public function __construct( + private string $principalUri, + ) { } public function getPrincipalUri(): string { diff --git a/lib/private/Calendar/Manager.php b/lib/private/Calendar/Manager.php index 7ef9dc585ae3c..f29920e17939f 100644 --- a/lib/private/Calendar/Manager.php +++ b/lib/private/Calendar/Manager.php @@ -50,33 +50,19 @@ class Manager implements IManager { /** * @var ICalendar[] holds all registered calendars */ - private $calendars = []; + private array $calendars = []; /** * @var \Closure[] to call to load/register calendar providers */ - private $calendarLoaders = []; - - /** @var Coordinator */ - private $coordinator; - - /** @var ContainerInterface */ - private $container; - - /** @var LoggerInterface */ - private $logger; - - private ITimeFactory $timeFactory; - - - public function __construct(Coordinator $coordinator, - ContainerInterface $container, - LoggerInterface $logger, - ITimeFactory $timeFactory) { - $this->coordinator = $coordinator; - $this->container = $container; - $this->logger = $logger; - $this->timeFactory = $timeFactory; + private array $calendarLoaders = []; + + public function __construct( + private Coordinator $coordinator, + private ContainerInterface $container, + private LoggerInterface $logger, + private ITimeFactory $timeFactory, + ) { } /** @@ -92,7 +78,13 @@ public function __construct(Coordinator $coordinator, * @return array an array of events/journals/todos which are arrays of arrays of key-value-pairs * @since 13.0.0 */ - public function search($pattern, array $searchProperties = [], array $options = [], $limit = null, $offset = null) { + public function search( + $pattern, + array $searchProperties = [], + array $options = [], + $limit = null, + $offset = null, + ): array { $this->loadCalendars(); $result = []; foreach ($this->calendars as $calendar) { @@ -112,29 +104,25 @@ public function search($pattern, array $searchProperties = [], array $options = * @return bool true if enabled, false if not * @since 13.0.0 */ - public function isEnabled() { + public function isEnabled(): bool { return !empty($this->calendars) || !empty($this->calendarLoaders); } /** * Registers a calendar * - * @param ICalendar $calendar - * @return void * @since 13.0.0 */ - public function registerCalendar(ICalendar $calendar) { + public function registerCalendar(ICalendar $calendar): void { $this->calendars[$calendar->getKey()] = $calendar; } /** * Unregisters a calendar * - * @param ICalendar $calendar - * @return void * @since 13.0.0 */ - public function unregisterCalendar(ICalendar $calendar) { + public function unregisterCalendar(ICalendar $calendar): void { unset($this->calendars[$calendar->getKey()]); } @@ -142,19 +130,18 @@ public function unregisterCalendar(ICalendar $calendar) { * In order to improve lazy loading a closure can be registered which will be called in case * calendars are actually requested * - * @param \Closure $callable - * @return void * @since 13.0.0 */ - public function register(\Closure $callable) { + public function register(\Closure $callable): void { $this->calendarLoaders[] = $callable; } /** * @return ICalendar[] + * * @since 13.0.0 */ - public function getCalendars() { + public function getCalendars(): array { $this->loadCalendars(); return array_values($this->calendars); @@ -162,10 +149,10 @@ public function getCalendars() { /** * removes all registered calendar instances - * @return void + * * @since 13.0.0 */ - public function clear() { + public function clear(): void { $this->calendars = []; $this->calendarLoaders = []; } @@ -173,7 +160,7 @@ public function clear() { /** * loads all calendars */ - private function loadCalendars() { + private function loadCalendars(): void { foreach ($this->calendarLoaders as $callable) { $callable($this); } @@ -181,8 +168,6 @@ private function loadCalendars() { } /** - * @param string $principalUri - * @param array $calendarUris * @return ICreateFromString[] */ public function getCalendarsForPrincipal(string $principalUri, array $calendarUris = []): array { @@ -240,7 +225,12 @@ public function newQuery(string $principalUri): ICalendarQuery { /** * @throws \OCP\DB\Exception */ - public function handleIMipReply(string $principalUri, string $sender, string $recipient, string $calendarData): bool { + public function handleIMipReply( + string $principalUri, + string $sender, + string $recipient, + string $calendarData, + ): bool { /** @var VCalendar $vObject */ $vObject = Reader::read($calendarData); /** @var VEvent $vEvent */ @@ -309,7 +299,13 @@ public function handleIMipReply(string $principalUri, string $sender, string $re * @since 25.0.0 * @throws \OCP\DB\Exception */ - public function handleIMipCancel(string $principalUri, string $sender, ?string $replyTo, string $recipient, string $calendarData): bool { + public function handleIMipCancel( + string $principalUri, + string $sender, + ?string $replyTo, + string $recipient, + string $calendarData, + ): bool { $vObject = Reader::read($calendarData); /** @var VEvent $vEvent */ $vEvent = $vObject->{'VEVENT'}; diff --git a/lib/private/Calendar/Resource/Manager.php b/lib/private/Calendar/Resource/Manager.php index ebde4ac5eb5d5..88e733f3f24d0 100644 --- a/lib/private/Calendar/Resource/Manager.php +++ b/lib/private/Calendar/Resource/Manager.php @@ -33,46 +33,38 @@ use OCP\IServerContainer; class Manager implements IManager { - private Coordinator $bootstrapCoordinator; - - private IServerContainer $server; - private bool $bootstrapBackendsLoaded = false; /** * @var string[] holds all registered resource backends * @psalm-var class-string[] */ - private $backends = []; + private array $backends = []; /** @var IBackend[] holds all backends that have been initialized already */ - private $initializedBackends = []; + private array $initializedBackends = []; - public function __construct(Coordinator $bootstrapCoordinator, - IServerContainer $server) { - $this->bootstrapCoordinator = $bootstrapCoordinator; - $this->server = $server; + public function __construct( + private Coordinator $bootstrapCoordinator, + private IServerContainer $server, + ) { } /** * Registers a resource backend * - * @param string $backendClass - * @return void * @since 14.0.0 */ - public function registerBackend(string $backendClass) { + public function registerBackend(string $backendClass): void { $this->backends[$backendClass] = $backendClass; } /** * Unregisters a resource backend * - * @param string $backendClass - * @return void * @since 14.0.0 */ - public function unregisterBackend(string $backendClass) { + public function unregisterBackend(string $backendClass): void { unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]); } @@ -114,9 +106,8 @@ public function getBackends():array { /** * @param string $backendId * @throws \OCP\AppFramework\QueryException - * @return IBackend|null */ - public function getBackend($backendId) { + public function getBackend($backendId): ?IBackend { $backends = $this->getBackends(); foreach ($backends as $backend) { if ($backend->getBackendIdentifier() === $backendId) { @@ -129,10 +120,10 @@ public function getBackend($backendId) { /** * removes all registered backend instances - * @return void + * * @since 14.0.0 */ - public function clear() { + public function clear(): void { $this->backends = []; $this->initializedBackends = []; } diff --git a/lib/private/Calendar/Room/Manager.php b/lib/private/Calendar/Room/Manager.php index 7e6af2823a3ef..d7ecdfd8b3663 100644 --- a/lib/private/Calendar/Room/Manager.php +++ b/lib/private/Calendar/Room/Manager.php @@ -33,10 +33,6 @@ use OCP\IServerContainer; class Manager implements IManager { - private Coordinator $bootstrapCoordinator; - - private IServerContainer $server; - private bool $bootstrapBackendsLoaded = false; /** @@ -48,20 +44,18 @@ class Manager implements IManager { /** @var IBackend[] holds all backends that have been initialized already */ private array $initializedBackends = []; - public function __construct(Coordinator $bootstrapCoordinator, - IServerContainer $server) { - $this->bootstrapCoordinator = $bootstrapCoordinator; - $this->server = $server; + public function __construct( + private Coordinator $bootstrapCoordinator, + private IServerContainer $server, + ) { } /** * Registers a resource backend * - * @param string $backendClass - * @return void * @since 14.0.0 */ - public function registerBackend(string $backendClass) { + public function registerBackend(string $backendClass): void { $this->backends[$backendClass] = $backendClass; } @@ -69,10 +63,9 @@ public function registerBackend(string $backendClass) { * Unregisters a resource backend * * @param string $backendClass - * @return void * @since 14.0.0 */ - public function unregisterBackend(string $backendClass) { + public function unregisterBackend(string $backendClass): void { unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]); } @@ -120,9 +113,8 @@ public function getBackends():array { /** * @param string $backendId * @throws \OCP\AppFramework\QueryException - * @return IBackend|null */ - public function getBackend($backendId) { + public function getBackend($backendId): ?IBackend { $backends = $this->getBackends(); foreach ($backends as $backend) { if ($backend->getBackendIdentifier() === $backendId) { @@ -135,10 +127,10 @@ public function getBackend($backendId) { /** * removes all registered backend instances - * @return void + * * @since 14.0.0 */ - public function clear() { + public function clear(): void { $this->backends = []; $this->initializedBackends = []; }