From fa266165d6d38f963dd44e227ee076799697f482 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 15 Mar 2022 17:17:36 +0100 Subject: Modernize comments app * Use php 7.4 typed properties * More typed methods (and replace existing php doc in some cases) * query -> get for Container Signed-off-by: Carl Schwan --- apps/comments/lib/Activity/Listener.php | 25 ++------ apps/comments/lib/Activity/Provider.php | 70 +++++----------------- apps/comments/lib/AppInfo/Application.php | 7 ++- .../lib/Collaboration/CommentersSorter.php | 16 ++--- apps/comments/lib/Controller/Notifications.php | 37 +++--------- apps/comments/lib/Listener/LoadSidebarScripts.php | 3 +- apps/comments/lib/Notification/Listener.php | 9 +-- apps/comments/lib/Notification/Notifier.php | 19 ++---- .../comments/lib/Search/CommentsSearchProvider.php | 15 ++--- apps/comments/lib/Search/LegacyProvider.php | 3 +- .../tests/Unit/AppInfo/ApplicationTest.php | 4 +- 11 files changed, 54 insertions(+), 154 deletions(-) diff --git a/apps/comments/lib/Activity/Listener.php b/apps/comments/lib/Activity/Listener.php index 94d10bb4c23..69315b6ac00 100644 --- a/apps/comments/lib/Activity/Listener.php +++ b/apps/comments/lib/Activity/Listener.php @@ -35,28 +35,15 @@ use OCP\IUserSession; use OCP\Share\IShareHelper; class Listener { - /** @var IManager */ - protected $activityManager; - /** @var IUserSession */ - protected $session; - /** @var \OCP\App\IAppManager */ - protected $appManager; - /** @var \OCP\Files\Config\IMountProviderCollection */ - protected $mountCollection; - /** @var \OCP\Files\IRootFolder */ - protected $rootFolder; - /** @var IShareHelper */ - protected $shareHelper; + protected IManager $activityManager; + protected IUserSession $session; + protected IAppManager $appManager; + protected IMountProviderCollection $mountCollection; + protected IRootFolder $rootFolder; + protected IShareHelper $shareHelper; /** * Listener constructor. - * - * @param IManager $activityManager - * @param IUserSession $session - * @param IAppManager $appManager - * @param IMountProviderCollection $mountCollection - * @param IRootFolder $rootFolder - * @param IShareHelper $shareHelper */ public function __construct(IManager $activityManager, IUserSession $session, diff --git a/apps/comments/lib/Activity/Provider.php b/apps/comments/lib/Activity/Provider.php index e42e119c617..715be29e2ed 100644 --- a/apps/comments/lib/Activity/Provider.php +++ b/apps/comments/lib/Activity/Provider.php @@ -37,34 +37,15 @@ use OCP\L10N\IFactory; class Provider implements IProvider { - /** @var IFactory */ - protected $languageFactory; - - /** @var IL10N */ - protected $l; - - /** @var IURLGenerator */ - protected $url; - - /** @var ICommentsManager */ - protected $commentsManager; - - /** @var IUserManager */ - protected $userManager; - - /** @var IManager */ - protected $activityManager; - + protected IFactory $languageFactory; + protected ?IL10N $l = null; + protected IUrlGenerator $url; + protected ICommentsManager $commentsManager; + protected IUserManager $userManager; + protected IManager $activityManager; /** @var string[] */ - protected $displayNames = []; + protected array $displayNames = []; - /** - * @param IFactory $languageFactory - * @param IURLGenerator $url - * @param ICommentsManager $commentsManager - * @param IUserManager $userManager - * @param IManager $activityManager - */ public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { $this->languageFactory = $languageFactory; $this->url = $url; @@ -111,11 +92,9 @@ class Provider implements IProvider { } /** - * @param IEvent $event - * @return IEvent * @throws \InvalidArgumentException */ - protected function parseShortVersion(IEvent $event) { + protected function parseShortVersion(IEvent $event): IEvent { $subjectParameters = $this->getSubjectParameters($event); if ($event->getSubject() === 'add_comment_subject') { @@ -137,11 +116,9 @@ class Provider implements IProvider { } /** - * @param IEvent $event - * @return IEvent * @throws \InvalidArgumentException */ - protected function parseLongVersion(IEvent $event) { + protected function parseLongVersion(IEvent $event): IEvent { $subjectParameters = $this->getSubjectParameters($event); if ($event->getSubject() === 'add_comment_subject') { @@ -170,7 +147,7 @@ class Provider implements IProvider { return $event; } - protected function getSubjectParameters(IEvent $event) { + protected function getSubjectParameters(IEvent $event): array { $subjectParameters = $event->getSubjectParameters(); if (isset($subjectParameters['fileId'])) { return $subjectParameters; @@ -190,10 +167,7 @@ class Provider implements IProvider { ]; } - /** - * @param IEvent $event - */ - protected function parseMessage(IEvent $event) { + protected function parseMessage(IEvent $event): void { $messageParameters = $event->getMessageParameters(); if (empty($messageParameters)) { // Email @@ -228,12 +202,7 @@ class Provider implements IProvider { } } - /** - * @param int $id - * @param string $path - * @return array - */ - protected function generateFileParameter($id, $path) { + protected function generateFileParameter(int $id, string $path): array { return [ 'type' => 'file', 'id' => $id, @@ -243,11 +212,7 @@ class Provider implements IProvider { ]; } - /** - * @param string $uid - * @return array - */ - protected function generateUserParameter($uid) { + protected function generateUserParameter(string $uid): array { if (!isset($this->displayNames[$uid])) { $this->displayNames[$uid] = $this->getDisplayName($uid); } @@ -259,16 +224,11 @@ class Provider implements IProvider { ]; } - /** - * @param string $uid - * @return string - */ - protected function getDisplayName($uid) { + protected function getDisplayName(string $uid): string { $user = $this->userManager->get($uid); if ($user instanceof IUser) { return $user->getDisplayName(); - } else { - return $uid; } + return $uid; } } diff --git a/apps/comments/lib/AppInfo/Application.php b/apps/comments/lib/AppInfo/Application.php index 090d62df639..527c5d99fc3 100644 --- a/apps/comments/lib/AppInfo/Application.php +++ b/apps/comments/lib/AppInfo/Application.php @@ -46,6 +46,7 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Comments\CommentsEntityEvent; use OCP\ISearch; use OCP\IServerContainer; +use OCP\Comments\ICommentsManager; class Application extends App implements IBootstrap { public const APP_ID = 'comments'; @@ -84,9 +85,9 @@ class Application extends App implements IBootstrap { $context->getServerContainer()->get(ISearch::class)->registerProvider(LegacyProvider::class, ['apps' => ['files']]); } - protected function registerCommentsEventHandler(IServerContainer $container) { - $container->getCommentsManager()->registerEventHandler(function () { - return $this->getContainer()->query(EventHandler::class); + protected function registerCommentsEventHandler(IServerContainer $container): void { + $container->get(ICommentsManager::class)->registerEventHandler(function (): EventHandler { + return $this->getContainer()->get(EventHandler::class); }); } } diff --git a/apps/comments/lib/Collaboration/CommentersSorter.php b/apps/comments/lib/Collaboration/CommentersSorter.php index 065e932fc9f..8723b132e03 100644 --- a/apps/comments/lib/Collaboration/CommentersSorter.php +++ b/apps/comments/lib/Collaboration/CommentersSorter.php @@ -28,8 +28,7 @@ use OCP\Comments\ICommentsManager; class CommentersSorter implements ISorter { - /** @var ICommentsManager */ - private $commentsManager; + private ICommentsManager $commentsManager; public function __construct(ICommentsManager $commentsManager) { $this->commentsManager = $commentsManager; @@ -77,12 +76,7 @@ class CommentersSorter implements ISorter { } } - /** - * @param $type - * @param $id - * @return array - */ - protected function retrieveCommentsInformation($type, $id) { + protected function retrieveCommentsInformation(string $type, string $id): array { $comments = $this->commentsManager->getForObject($type, $id); if (count($comments) === 0) { return []; @@ -102,12 +96,12 @@ class CommentersSorter implements ISorter { return $actors; } - protected function compare(array $a, array $b, array $commenters) { + protected function compare(array $a, array $b, array $commenters): int { $a = $a['value']['shareWith']; $b = $b['value']['shareWith']; - $valueA = isset($commenters[$a]) ? $commenters[$a] : 0; - $valueB = isset($commenters[$b]) ? $commenters[$b] : 0; + $valueA = $commenters[$a] ?? 0; + $valueB = $commenters[$b] ?? 0; return $valueB - $valueA; } diff --git a/apps/comments/lib/Controller/Notifications.php b/apps/comments/lib/Controller/Notifications.php index a538eddc023..ba61a0d33cd 100644 --- a/apps/comments/lib/Controller/Notifications.php +++ b/apps/comments/lib/Controller/Notifications.php @@ -43,34 +43,18 @@ use OCP\Notification\IManager; * @package OCA\Comments\Controller */ class Notifications extends Controller { - /** @var IRootFolder */ - protected $rootFolder; - /** @var ICommentsManager */ - protected $commentsManager; - - /** @var IURLGenerator */ - protected $urlGenerator; - - /** @var IManager */ - protected $notificationManager; - - /** @var IUserSession */ - protected $userSession; + protected IRootFolder $rootFolder; + protected ICommentsManager $commentsManager; + protected IURLGenerator $urlGenerator; + protected IManager $notificationManager; + protected IUserSession $userSession; /** * Notifications constructor. - * - * @param string $appName - * @param IRequest $request - * @param ICommentsManager $commentsManager - * @param IRootFolder $rootFolder - * @param IURLGenerator $urlGenerator - * @param IManager $notificationManager - * @param IUserSession $userSession */ public function __construct( - $appName, + string $appName, IRequest $request, ICommentsManager $commentsManager, IRootFolder $rootFolder, @@ -89,11 +73,8 @@ class Notifications extends Controller { /** * @PublicPage * @NoCSRFRequired - * - * @param string $id the comment ID - * @return Response */ - public function view($id) { + public function view(string $id): Response { $currentUser = $this->userSession->getUser(); if (!$currentUser instanceof IUser) { return new RedirectResponse( @@ -133,10 +114,8 @@ class Notifications extends Controller { /** * Marks the notification about a comment as processed - * @param IComment $comment - * @param IUser $currentUser */ - protected function markProcessed(IComment $comment, IUser $currentUser) { + protected function markProcessed(IComment $comment, IUser $currentUser): void { $notification = $this->notificationManager->createNotification(); $notification->setApp('comments') ->setObject('comment', $comment->getId()) diff --git a/apps/comments/lib/Listener/LoadSidebarScripts.php b/apps/comments/lib/Listener/LoadSidebarScripts.php index a73c11ebf18..2c4dca97188 100644 --- a/apps/comments/lib/Listener/LoadSidebarScripts.php +++ b/apps/comments/lib/Listener/LoadSidebarScripts.php @@ -35,8 +35,7 @@ use OCP\Util; class LoadSidebarScripts implements IEventListener { - /** @var ICommentsManager */ - private $commentsManager; + private ICommentsManager $commentsManager; public function __construct(ICommentsManager $commentsManager) { $this->commentsManager = $commentsManager; diff --git a/apps/comments/lib/Notification/Listener.php b/apps/comments/lib/Notification/Listener.php index 3f92ac35540..d1662f84266 100644 --- a/apps/comments/lib/Notification/Listener.php +++ b/apps/comments/lib/Notification/Listener.php @@ -29,17 +29,12 @@ use OCP\IUserManager; use OCP\Notification\IManager; class Listener { - /** @var IManager */ - protected $notificationManager; - /** @var IUserManager */ - protected $userManager; + protected IManager $notificationManager; + protected IUserManager $userManager; /** * Listener constructor. - * - * @param IManager $notificationManager - * @param IUserManager $userManager */ public function __construct( IManager $notificationManager, diff --git a/apps/comments/lib/Notification/Notifier.php b/apps/comments/lib/Notification/Notifier.php index 745cb513978..7c6a40133ee 100644 --- a/apps/comments/lib/Notification/Notifier.php +++ b/apps/comments/lib/Notification/Notifier.php @@ -38,20 +38,11 @@ use OCP\Notification\INotifier; class Notifier implements INotifier { - /** @var IFactory */ - protected $l10nFactory; - - /** @var IRootFolder */ - protected $rootFolder; - - /** @var ICommentsManager */ - protected $commentsManager; - - /** @var IURLGenerator */ - protected $url; - - /** @var IUserManager */ - protected $userManager; + protected IFactory $l10nFactory; + protected IRootFolder $rootFolder; + protected ICommentsManager $commentsManager; + protected IURLGenerator $url; + protected IUserManager $userManager; public function __construct( IFactory $l10nFactory, diff --git a/apps/comments/lib/Search/CommentsSearchProvider.php b/apps/comments/lib/Search/CommentsSearchProvider.php index 796e87ad4bb..b36f82f8401 100644 --- a/apps/comments/lib/Search/CommentsSearchProvider.php +++ b/apps/comments/lib/Search/CommentsSearchProvider.php @@ -40,17 +40,10 @@ use function pathinfo; class CommentsSearchProvider implements IProvider { - /** @var IUserManager */ - private $userManager; - - /** @var IL10N */ - private $l10n; - - /** @var IURLGenerator */ - private $urlGenerator; - - /** @var LegacyProvider */ - private $legacyProvider; + private IUserManager $userManager; + private IL10N $l10n; + private IURLGenerator $urlGenerator; + private LegacyProvider $legacyProvider; public function __construct(IUserManager $userManager, IL10N $l10n, diff --git a/apps/comments/lib/Search/LegacyProvider.php b/apps/comments/lib/Search/LegacyProvider.php index cba48678e8b..d22caad7e3d 100644 --- a/apps/comments/lib/Search/LegacyProvider.php +++ b/apps/comments/lib/Search/LegacyProvider.php @@ -31,6 +31,7 @@ use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IUser; use OCP\Search\Provider; +use OCP\Comments\ICommentsManager; use function count; class LegacyProvider extends Provider { @@ -43,7 +44,7 @@ class LegacyProvider extends Provider { * @since 7.0.0 */ public function search($query): array { - $cm = \OC::$server->getCommentsManager(); + $cm = \OC::$server->get(ICommentsManager::class); $us = \OC::$server->getUserSession(); $user = $us->getUser(); diff --git a/apps/comments/tests/Unit/AppInfo/ApplicationTest.php b/apps/comments/tests/Unit/AppInfo/ApplicationTest.php index 710a517f035..6e3658c0e5b 100644 --- a/apps/comments/tests/Unit/AppInfo/ApplicationTest.php +++ b/apps/comments/tests/Unit/AppInfo/ApplicationTest.php @@ -52,7 +52,7 @@ class ApplicationTest extends TestCase { $c = $app->getContainer(); // assert service instances in the container are properly setup - $s = $c->query('NotificationsController'); + $s = $c->get('NotificationsController'); $this->assertInstanceOf('OCA\Comments\Controller\Notifications', $s); $services = [ @@ -65,7 +65,7 @@ class ApplicationTest extends TestCase { ]; foreach ($services as $service) { - $s = $c->query($service); + $s = $c->get($service); $this->assertInstanceOf($service, $s); } } -- cgit v1.2.3