From 09fd00ca07bb94b04e87d2cacf38fd863b5af325 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Sep 2024 16:16:58 +0200 Subject: [PATCH] fix(activity): Update more activity providers to use new exceptions Signed-off-by: Joas Schilling --- apps/comments/lib/Activity/Provider.php | 10 ++++---- apps/files/lib/Activity/FavoriteProvider.php | 14 +++++------ apps/files/lib/Activity/Provider.php | 24 +++++++++---------- apps/files/tests/Activity/ProviderTest.php | 3 ++- apps/settings/lib/Activity/GroupProvider.php | 3 --- apps/settings/lib/Activity/Provider.php | 7 ++---- .../tests/Activity/SecurityProviderTest.php | 6 ++--- apps/systemtags/lib/Activity/Provider.php | 14 +++++------ .../tests/Unit/Activity/ProviderTest.php | 6 ++--- 9 files changed, 41 insertions(+), 46 deletions(-) diff --git a/apps/comments/lib/Activity/Provider.php b/apps/comments/lib/Activity/Provider.php index 4fb0c8d58beb8..f2fa84adc17df 100644 --- a/apps/comments/lib/Activity/Provider.php +++ b/apps/comments/lib/Activity/Provider.php @@ -54,7 +54,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null): if ($this->activityManager->isFormattingFilteredObject()) { try { return $this->parseShortVersion($event); - } catch (\InvalidArgumentException $e) { + } catch (UnknownActivityException) { // Ignore and simply use the long version... } } @@ -66,7 +66,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null): } /** - * @throws \InvalidArgumentException + * @throws UnknownActivityException */ protected function parseShortVersion(IEvent $event): IEvent { $subjectParameters = $this->getSubjectParameters($event); @@ -81,14 +81,14 @@ protected function parseShortVersion(IEvent $event): IEvent { ]); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; } /** - * @throws \InvalidArgumentException + * @throws UnknownActivityException */ protected function parseLongVersion(IEvent $event): IEvent { $subjectParameters = $this->getSubjectParameters($event); @@ -113,7 +113,7 @@ protected function parseLongVersion(IEvent $event): IEvent { ]); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; diff --git a/apps/files/lib/Activity/FavoriteProvider.php b/apps/files/lib/Activity/FavoriteProvider.php index 8cfc1a8379881..dd24eaedd2cca 100644 --- a/apps/files/lib/Activity/FavoriteProvider.php +++ b/apps/files/lib/Activity/FavoriteProvider.php @@ -64,7 +64,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { if ($this->activityManager->isFormattingFilteredObject()) { try { return $this->parseShortVersion($event); - } catch (\InvalidArgumentException $e) { + } catch (UnknownActivityException) { // Ignore and simply use the long version... } } @@ -75,10 +75,10 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { /** * @param IEvent $event * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseShortVersion(IEvent $event) { + public function parseShortVersion(IEvent $event): IEvent { if ($event->getSubject() === self::SUBJECT_ADDED) { $event->setParsedSubject($this->l->t('Added to favorites')); if ($this->activityManager->getRequirePNG()) { @@ -95,7 +95,7 @@ public function parseShortVersion(IEvent $event) { $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg'))); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; @@ -105,10 +105,10 @@ public function parseShortVersion(IEvent $event) { * @param IEvent $event * @param IEvent|null $previousEvent * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) { + public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent { if ($event->getSubject() === self::SUBJECT_ADDED) { $subject = $this->l->t('You added {file} to your favorites'); if ($this->activityManager->getRequirePNG()) { @@ -125,7 +125,7 @@ public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) { $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg'))); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } $this->setSubjects($event, $subject); diff --git a/apps/files/lib/Activity/Provider.php b/apps/files/lib/Activity/Provider.php index f777d11c02ed4..cb5eb5af1b0f6 100644 --- a/apps/files/lib/Activity/Provider.php +++ b/apps/files/lib/Activity/Provider.php @@ -94,7 +94,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { if ($this->activityManager->isFormattingFilteredObject()) { try { return $this->parseShortVersion($event, $previousEvent); - } catch (\InvalidArgumentException $e) { + } catch (UnknownActivityException) { // Ignore and simply use the long version... } } @@ -114,10 +114,10 @@ protected function setIcon(IEvent $event, string $icon, string $app = 'files') { * @param IEvent $event * @param IEvent|null $previousEvent * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null) { + public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent { $parsedParameters = $this->getParameters($event); if ($event->getSubject() === 'created_by') { @@ -139,7 +139,7 @@ public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null) $subject = $this->l->t('Moved by {user}'); $this->setIcon($event, 'change'); } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } if (!isset($parsedParameters['user'])) { @@ -156,10 +156,10 @@ public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null) * @param IEvent $event * @param IEvent|null $previousEvent * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) { + public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent { $this->fileIsEncrypted = false; $parsedParameters = $this->getParameters($event); @@ -253,7 +253,7 @@ public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) { $subject = $this->l->t('{user} moved {oldfile} to {newfile}'); $this->setIcon($event, 'change'); } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } if ($this->fileIsEncrypted) { @@ -292,9 +292,9 @@ protected function setSubjects(IEvent $event, string $subject, array $parameters /** * @param IEvent $event * @return array - * @throws \InvalidArgumentException + * @throws UnknownActivityException */ - protected function getParameters(IEvent $event) { + protected function getParameters(IEvent $event): array { $parameters = $event->getSubjectParameters(); switch ($event->getSubject()) { case 'created_self': @@ -347,9 +347,9 @@ protected function getParameters(IEvent $event) { * @param array|string $parameter * @param IEvent|null $event * @return array - * @throws \InvalidArgumentException + * @throws UnknownActivityException */ - protected function getFile($parameter, ?IEvent $event = null) { + protected function getFile($parameter, ?IEvent $event = null): array { if (is_array($parameter)) { $path = reset($parameter); $id = (string)key($parameter); @@ -358,7 +358,7 @@ protected function getFile($parameter, ?IEvent $event = null) { $path = $parameter; $id = $event->getObjectId(); } else { - throw new \InvalidArgumentException('Could not generate file parameter'); + throw new UnknownActivityException('Could not generate file parameter'); } $encryptionContainer = $this->getEndToEndEncryptionContainer($id, $path); diff --git a/apps/files/tests/Activity/ProviderTest.php b/apps/files/tests/Activity/ProviderTest.php index 73c726dac3c02..7803563a11c50 100644 --- a/apps/files/tests/Activity/ProviderTest.php +++ b/apps/files/tests/Activity/ProviderTest.php @@ -6,6 +6,7 @@ namespace OCA\Files\Tests\Activity; use OCA\Files\Activity\Provider; +use OCP\Activity\Exceptions\UnknownActivityException; use OCP\Activity\IEvent; use OCP\Activity\IEventMerger; use OCP\Activity\IManager; @@ -132,7 +133,7 @@ public function testGetFile($parameter, $eventId, $id, $name, $path): void { public function testGetFileThrows(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(UnknownActivityException::class); $provider = $this->getProvider(); self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]); diff --git a/apps/settings/lib/Activity/GroupProvider.php b/apps/settings/lib/Activity/GroupProvider.php index fc7189c774db5..abf3e5613c0b6 100644 --- a/apps/settings/lib/Activity/GroupProvider.php +++ b/apps/settings/lib/Activity/GroupProvider.php @@ -103,9 +103,6 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { return $event; } - /** - * @throws \InvalidArgumentException - */ protected function setSubjects(IEvent $event, string $subject, array $parameters): void { $event->setRichSubject($subject, $parameters); } diff --git a/apps/settings/lib/Activity/Provider.php b/apps/settings/lib/Activity/Provider.php index 8d726c8da22b1..e1de18554d951 100644 --- a/apps/settings/lib/Activity/Provider.php +++ b/apps/settings/lib/Activity/Provider.php @@ -118,7 +118,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null): /** * @param IEvent $event * @return array - * @throws \InvalidArgumentException + * @throws UnknownActivityException */ protected function getParameters(IEvent $event): array { $subject = $event->getSubject(); @@ -162,12 +162,9 @@ protected function getParameters(IEvent $event): array { ]; } - throw new \InvalidArgumentException('Unknown subject'); + throw new UnknownActivityException('Unknown subject'); } - /** - * @throws \InvalidArgumentException - */ protected function setSubjects(IEvent $event, string $subject, array $parameters): void { $event->setRichSubject($subject, $parameters); } diff --git a/apps/settings/tests/Activity/SecurityProviderTest.php b/apps/settings/tests/Activity/SecurityProviderTest.php index b00c62281cc89..35526995a6740 100644 --- a/apps/settings/tests/Activity/SecurityProviderTest.php +++ b/apps/settings/tests/Activity/SecurityProviderTest.php @@ -5,8 +5,8 @@ */ namespace OCA\Settings\Tests; -use InvalidArgumentException; use OCA\Settings\Activity\SecurityProvider; +use OCP\Activity\Exceptions\UnknownActivityException; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\IL10N; @@ -45,7 +45,7 @@ public function testParseUnrelated(): void { $event->expects($this->once()) ->method('getType') ->willReturn('comments'); - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnknownActivityException::class); $this->provider->parse($lang, $event); } @@ -112,7 +112,7 @@ public function testParseInvalidSubject(): void { ->method('getSubject') ->willReturn('unrelated'); - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnknownActivityException::class); $this->provider->parse($lang, $event); } } diff --git a/apps/systemtags/lib/Activity/Provider.php b/apps/systemtags/lib/Activity/Provider.php index ba24dff31a875..05c19fe81cc28 100644 --- a/apps/systemtags/lib/Activity/Provider.php +++ b/apps/systemtags/lib/Activity/Provider.php @@ -68,7 +68,7 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { if ($this->activityManager->isFormattingFilteredObject()) { try { return $this->parseShortVersion($event); - } catch (\InvalidArgumentException $e) { + } catch (UnknownActivityException) { // Ignore and simply use the long version... } } @@ -79,10 +79,10 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) { /** * @param IEvent $event * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseShortVersion(IEvent $event) { + public function parseShortVersion(IEvent $event): IEvent { $parsedParameters = $this->getParameters($event); if ($this->activityManager->getRequirePNG()) { @@ -142,7 +142,7 @@ public function parseShortVersion(IEvent $event) { ]); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; @@ -151,10 +151,10 @@ public function parseShortVersion(IEvent $event) { /** * @param IEvent $event * @return IEvent - * @throws \InvalidArgumentException + * @throws UnknownActivityException * @since 11.0.0 */ - public function parseLongVersion(IEvent $event) { + public function parseLongVersion(IEvent $event): IEvent { $parsedParameters = $this->getParameters($event); if ($this->activityManager->getRequirePNG()) { @@ -249,7 +249,7 @@ public function parseLongVersion(IEvent $event) { ->setRichSubject($this->l->t('{actor} removed system tag {systemtag} from {file}'), $parsedParameters); } } else { - throw new \InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; diff --git a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php index fb2c419f52177..e41db8b0a3284 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Activity/ProviderTest.php @@ -8,8 +8,8 @@ */ namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity; -use InvalidArgumentException; use OCA\TwoFactorBackupCodes\Activity\Provider; +use OCP\Activity\Exceptions\UnknownActivityException; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\IL10N; @@ -47,7 +47,7 @@ public function testParseUnrelated(): void { $event->expects($this->once()) ->method('getApp') ->willReturn('comments'); - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnknownActivityException::class); $this->provider->parse($lang, $event); } @@ -109,7 +109,7 @@ public function testParseInvalidSubject(): void { ->method('getSubject') ->willReturn('unrelated'); - $this->expectException(InvalidArgumentException::class); + $this->expectException(UnknownActivityException::class); $this->provider->parse($lang, $event); } }