From a107773c11e39f02a078ec32aad29239ae604951 Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Sat, 5 Apr 2025 13:56:56 +0200 Subject: [PATCH] Modernize to PHP 8.0 --- app/AppKernel.php | 19 +++---- rector.php | 2 +- src/Command/CleanDuplicatesCommand.php | 2 +- src/Command/GenerateUrlHashesCommand.php | 2 +- src/Command/Import/ImportCommand.php | 51 +++++-------------- src/Command/InstallCommand.php | 2 +- src/Command/ReloadEntryCommand.php | 2 +- src/Command/ShowUserCommand.php | 2 +- src/Command/TagAllCommand.php | 2 +- src/Controller/EntryController.php | 6 +-- src/Controller/FeedController.php | 27 ++++------ src/Controller/Import/ImportController.php | 4 +- src/Controller/TagController.php | 2 +- src/Controller/UserController.php | 2 +- src/Entity/Tag.php | 4 +- .../AuthenticatorProvider.php | 2 +- src/Helper/DownloadImages.php | 4 +- src/Security/Voter/AdminVoter.php | 13 ++--- src/Security/Voter/AnnotationVoter.php | 11 ++-- src/Security/Voter/EntryVoter.php | 23 ++------- .../Voter/IgnoreOriginInstanceRuleVoter.php | 11 ++-- .../Voter/IgnoreOriginUserRuleVoter.php | 11 ++-- src/Security/Voter/MainVoter.php | 21 ++------ src/Security/Voter/SiteCredentialVoter.php | 11 ++-- src/Security/Voter/TagVoter.php | 12 ++--- src/Security/Voter/TaggingRuleVoter.php | 11 ++-- src/SiteConfig/ArraySiteConfigBuilder.php | 2 +- src/SiteConfig/GrabySiteConfigBuilder.php | 2 +- src/SiteConfig/LoginFormAuthenticator.php | 4 +- src/Twig/WallabagExtension.php | 27 +++------- tests/WallabagTestCase.php | 2 +- 31 files changed, 97 insertions(+), 199 deletions(-) diff --git a/app/AppKernel.php b/app/AppKernel.php index 7beb05630..0b0cf7455 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -120,19 +120,12 @@ class AppKernel extends Kernel private function processDatabaseParameters(ContainerBuilder $container) { - switch ($container->getParameter('database_driver')) { - case 'pdo_mysql': - $scheme = 'mysql'; - break; - case 'pdo_pgsql': - $scheme = 'pgsql'; - break; - case 'pdo_sqlite': - $scheme = 'sqlite'; - break; - default: - throw new RuntimeException('Unsupported database driver: ' . $container->getParameter('database_driver')); - } + $scheme = match ($container->getParameter('database_driver')) { + 'pdo_mysql' => 'mysql', + 'pdo_pgsql' => 'pgsql', + 'pdo_sqlite' => 'sqlite', + default => throw new RuntimeException('Unsupported database driver: ' . $container->getParameter('database_driver')), + }; $container->setParameter('database_scheme', $scheme); diff --git a/rector.php b/rector.php index c2a859f6f..7480ccf1c 100644 --- a/rector.php +++ b/rector.php @@ -23,5 +23,5 @@ return RectorConfig::configure() __DIR__ . '/src/Entity/*', ], ]) - ->withPhpSets(php74: true) + ->withPhpSets(php80: true) ->withTypeCoverageLevel(0); diff --git a/src/Command/CleanDuplicatesCommand.php b/src/Command/CleanDuplicatesCommand.php index 0da8d630c..100c5b9e6 100644 --- a/src/Command/CleanDuplicatesCommand.php +++ b/src/Command/CleanDuplicatesCommand.php @@ -51,7 +51,7 @@ class CleanDuplicatesCommand extends Command try { $user = $this->getUser($username); $this->cleanDuplicates($user); - } catch (NoResultException $e) { + } catch (NoResultException) { $this->io->error(\sprintf('User "%s" not found.', $username)); return 1; diff --git a/src/Command/GenerateUrlHashesCommand.php b/src/Command/GenerateUrlHashesCommand.php index 47d9c7b38..82a8574c6 100644 --- a/src/Command/GenerateUrlHashesCommand.php +++ b/src/Command/GenerateUrlHashesCommand.php @@ -45,7 +45,7 @@ class GenerateUrlHashesCommand extends Command try { $user = $this->getUser($username); $this->generateHashedUrls($user); - } catch (NoResultException $e) { + } catch (NoResultException) { $output->writeln(\sprintf('User "%s" not found.', $username)); return 1; diff --git a/src/Command/Import/ImportCommand.php b/src/Command/Import/ImportCommand.php index 56aa04122..e3bff092c 100644 --- a/src/Command/Import/ImportCommand.php +++ b/src/Command/Import/ImportCommand.php @@ -97,43 +97,20 @@ class ImportCommand extends Command $this->tokenStorage->setToken($token); $user = $this->tokenStorage->getToken()->getUser(); - switch ($input->getOption('importer')) { - case 'v2': - $import = $this->wallabagV2Import; - break; - case 'firefox': - $import = $this->firefoxImport; - break; - case 'chrome': - $import = $this->chromeImport; - break; - case 'readability': - $import = $this->readabilityImport; - break; - case 'instapaper': - $import = $this->instapaperImport; - break; - case 'pinboard': - $import = $this->pinboardImport; - break; - case 'delicious': - $import = $this->deliciousImport; - break; - case 'elcurator': - $import = $this->elcuratorImport; - break; - case 'shaarli': - $import = $this->shaarliImport; - break; - case 'pocket': - $import = $this->pocketHtmlImport; - break; - case 'omnivore': - $import = $this->omnivoreImport; - break; - default: - $import = $this->wallabagV1Import; - } + $import = match ($input->getOption('importer')) { + 'v2' => $this->wallabagV2Import, + 'firefox' => $this->firefoxImport, + 'chrome' => $this->chromeImport, + 'readability' => $this->readabilityImport, + 'instapaper' => $this->instapaperImport, + 'pinboard' => $this->pinboardImport, + 'delicious' => $this->deliciousImport, + 'elcurator' => $this->elcuratorImport, + 'shaarli' => $this->shaarliImport, + 'pocket' => $this->pocketHtmlImport, + 'omnivore' => $this->omnivoreImport, + default => $this->wallabagV1Import, + }; $import->setMarkAsRead($input->getOption('markAsRead')); $import->setDisableContentUpdate($input->getOption('disableContentUpdate')); diff --git a/src/Command/InstallCommand.php b/src/Command/InstallCommand.php index af9f3a1a1..66a1cc1fe 100644 --- a/src/Command/InstallCommand.php +++ b/src/Command/InstallCommand.php @@ -402,7 +402,7 @@ class InstallCommand extends Command try { return \in_array($databaseName, $schemaManager->listDatabases(), true); - } catch (DriverException $e) { + } catch (DriverException) { // it means we weren't able to get database list, assume the database doesn't exist return false; diff --git a/src/Command/ReloadEntryCommand.php b/src/Command/ReloadEntryCommand.php index 68a47e5d0..7bfbaef3c 100644 --- a/src/Command/ReloadEntryCommand.php +++ b/src/Command/ReloadEntryCommand.php @@ -55,7 +55,7 @@ class ReloadEntryCommand extends Command $userId = $this->userRepository ->findOneByUserName($username) ->getId(); - } catch (NoResultException $e) { + } catch (NoResultException) { $io->error(\sprintf('User "%s" not found.', $username)); return 1; diff --git a/src/Command/ShowUserCommand.php b/src/Command/ShowUserCommand.php index be771df1c..f1b68c4cd 100644 --- a/src/Command/ShowUserCommand.php +++ b/src/Command/ShowUserCommand.php @@ -44,7 +44,7 @@ class ShowUserCommand extends Command try { $user = $this->getUser($username); $this->showUser($user); - } catch (NoResultException $e) { + } catch (NoResultException) { $this->io->error(\sprintf('User "%s" not found.', $username)); return 1; diff --git a/src/Command/TagAllCommand.php b/src/Command/TagAllCommand.php index bc135f42a..99fcf32ba 100644 --- a/src/Command/TagAllCommand.php +++ b/src/Command/TagAllCommand.php @@ -43,7 +43,7 @@ class TagAllCommand extends Command try { $user = $this->getUser($input->getArgument('username')); - } catch (NoResultException $e) { + } catch (NoResultException) { $io->error(\sprintf('User "%s" not found.', $input->getArgument('username'))); return 1; diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php index 278731f23..84ba851f8 100644 --- a/src/Controller/EntryController.php +++ b/src/Controller/EntryController.php @@ -379,7 +379,7 @@ class EntryController extends AbstractController try { $entry = $this->entryRepository ->getRandomEntry($this->getUser()->getId(), $type); - } catch (NoResultException $e) { + } catch (NoResultException) { $this->addFlash('notice', 'flashes.entry.notice.no_random_entry'); return $this->redirect($this->generateUrl($type)); @@ -670,7 +670,7 @@ class EntryController extends AbstractController try { $entries->setCurrentPage($page); - } catch (OutOfRangeCurrentPageException $e) { + } catch (OutOfRangeCurrentPageException) { if ($page > 1) { return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); } @@ -699,7 +699,7 @@ class EntryController extends AbstractController try { $this->contentProxy->updateEntry($entry, $entry->getUrl()); - } catch (\Exception $e) { + } catch (\Exception) { // $this->logger->error('Error while saving an entry', [ // 'exception' => $e, // 'entry' => $entry, diff --git a/src/Controller/FeedController.php b/src/Controller/FeedController.php index 6317ed8dd..c643052a8 100644 --- a/src/Controller/FeedController.php +++ b/src/Controller/FeedController.php @@ -141,7 +141,7 @@ class FeedController extends AbstractController try { $entries->setCurrentPage($page); - } catch (OutOfRangeCurrentPageException $e) { + } catch (OutOfRangeCurrentPageException) { if ($page > 1) { return $this->redirect($url . '?page=' . $entries->getNbPages(), 302); } @@ -189,22 +189,13 @@ class FeedController extends AbstractController */ private function showEntries(string $type, User $user, $page = 1) { - switch ($type) { - case 'starred': - $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); - break; - case 'archive': - $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); - break; - case 'unread': - $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); - break; - case 'all': - $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); - break; - default: - throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)); - } + $qb = match ($type) { + 'starred' => $this->entryRepository->getBuilderForStarredByUser($user->getId()), + 'archive' => $this->entryRepository->getBuilderForArchiveByUser($user->getId()), + 'unread' => $this->entryRepository->getBuilderForUnreadByUser($user->getId()), + 'all' => $this->entryRepository->getBuilderForAllByUser($user->getId()), + default => throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)), + }; $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); $entries = new Pagerfanta($pagerAdapter); @@ -223,7 +214,7 @@ class FeedController extends AbstractController try { $entries->setCurrentPage((int) $page); - } catch (OutOfRangeCurrentPageException $e) { + } catch (OutOfRangeCurrentPageException) { if ($page > 1) { return $this->redirect($url . '/' . $entries->getNbPages()); } diff --git a/src/Controller/Import/ImportController.php b/src/Controller/Import/ImportController.php index fe09e06a2..17a72a567 100644 --- a/src/Controller/Import/ImportController.php +++ b/src/Controller/Import/ImportController.php @@ -62,7 +62,7 @@ class ImportController extends AbstractController + $this->rabbitMQConsumerTotalProxy->getTotalMessage('pocket_html') + $this->rabbitMQConsumerTotalProxy->getTotalMessage('omnivore') ; - } catch (\Exception $e) { + } catch (\Exception) { $rabbitNotInstalled = true; } } elseif ($craueConfig->get('import_with_redis')) { @@ -81,7 +81,7 @@ class ImportController extends AbstractController + $this->redisClient->llen('wallabag.import.pocket_html') + $this->redisClient->llen('wallabag.import.omnivore') ; - } catch (\Exception $e) { + } catch (\Exception) { $redisNotInstalled = true; } } diff --git a/src/Controller/TagController.php b/src/Controller/TagController.php index 859a9305d..87cb38322 100644 --- a/src/Controller/TagController.php +++ b/src/Controller/TagController.php @@ -153,7 +153,7 @@ class TagController extends AbstractController try { $entries->setCurrentPage($page); - } catch (OutOfRangeCurrentPageException $e) { + } catch (OutOfRangeCurrentPageException) { if ($page > 1) { return $this->redirect($this->generateUrl($request->attributes->get('_route'), [ 'slug' => $tag->getSlug(), diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index ae9568407..ef37bfe0c 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -169,7 +169,7 @@ class UserController extends AbstractController try { $pagerFanta->setCurrentPage($page); - } catch (OutOfRangeCurrentPageException $e) { + } catch (OutOfRangeCurrentPageException) { if ($page > 1) { return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302); } diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php index 47102f3ab..0816a5633 100644 --- a/src/Entity/Tag.php +++ b/src/Entity/Tag.php @@ -19,7 +19,7 @@ use Wallabag\Repository\TagRepository; #[ORM\Table(name: '`tag`')] #[ORM\Index(columns: ['label'])] #[ORM\Entity(repositoryClass: TagRepository::class)] -class Tag +class Tag implements \Stringable { /** * @var int @@ -54,7 +54,7 @@ class Tag $this->entries = new ArrayCollection(); } - public function __toString() + public function __toString(): string { return $this->label; } diff --git a/src/ExpressionLanguage/AuthenticatorProvider.php b/src/ExpressionLanguage/AuthenticatorProvider.php index 2d26a5ef0..f7a3d6392 100644 --- a/src/ExpressionLanguage/AuthenticatorProvider.php +++ b/src/ExpressionLanguage/AuthenticatorProvider.php @@ -67,7 +67,7 @@ class AuthenticatorProvider implements ExpressionFunctionProviderInterface $crawler = new Crawler((string) $html); $crawler = $crawler->filterXPath($xpathQuery); - } catch (\Throwable $e) { + } catch (\Throwable) { return ''; } diff --git a/src/Helper/DownloadImages.php b/src/Helper/DownloadImages.php index a997d3a0f..b3eedfc14 100644 --- a/src/Helper/DownloadImages.php +++ b/src/Helper/DownloadImages.php @@ -171,7 +171,7 @@ class DownloadImages try { $im = imagecreatefromstring($res->getContent()); - } catch (\Exception $e) { + } catch (\Exception) { $im = false; } @@ -190,7 +190,7 @@ class DownloadImages $imagick->readImageBlob($res->getContent()); $imagick->setImageFormat('gif'); $imagick->writeImages($localPath, true); - } catch (\Exception $e) { + } catch (\Exception) { // if Imagick fail, fallback to the default solution imagegif($im, $localPath); } diff --git a/src/Security/Voter/AdminVoter.php b/src/Security/Voter/AdminVoter.php index fdd5fef47..4baa12612 100644 --- a/src/Security/Voter/AdminVoter.php +++ b/src/Security/Voter/AdminVoter.php @@ -36,14 +36,9 @@ class AdminVoter extends Voter return false; } - switch ($attribute) { - case self::LIST_USERS: - case self::CREATE_USERS: - case self::LIST_IGNORE_ORIGIN_INSTANCE_RULES: - case self::CREATE_IGNORE_ORIGIN_INSTANCE_RULES: - return $this->security->isGranted('ROLE_SUPER_ADMIN'); - } - - return false; + return match ($attribute) { + self::LIST_USERS, self::CREATE_USERS, self::LIST_IGNORE_ORIGIN_INSTANCE_RULES, self::CREATE_IGNORE_ORIGIN_INSTANCE_RULES => $this->security->isGranted('ROLE_SUPER_ADMIN'), + default => false, + }; } } diff --git a/src/Security/Voter/AnnotationVoter.php b/src/Security/Voter/AnnotationVoter.php index 7bed72dbc..a4b021de6 100644 --- a/src/Security/Voter/AnnotationVoter.php +++ b/src/Security/Voter/AnnotationVoter.php @@ -35,12 +35,9 @@ class AnnotationVoter extends Voter return false; } - switch ($attribute) { - case self::EDIT: - case self::DELETE: - return $subject->getUser() === $user; - } - - return false; + return match ($attribute) { + self::EDIT, self::DELETE => $subject->getUser() === $user, + default => false, + }; } } diff --git a/src/Security/Voter/EntryVoter.php b/src/Security/Voter/EntryVoter.php index a9b354fea..202dacb8c 100644 --- a/src/Security/Voter/EntryVoter.php +++ b/src/Security/Voter/EntryVoter.php @@ -47,24 +47,9 @@ class EntryVoter extends Voter return false; } - switch ($attribute) { - case self::VIEW: - case self::EDIT: - case self::RELOAD: - case self::STAR: - case self::ARCHIVE: - case self::SHARE: - case self::UNSHARE: - case self::EXPORT: - case self::DELETE: - case self::LIST_ANNOTATIONS: - case self::CREATE_ANNOTATIONS: - case self::LIST_TAGS: - case self::TAG: - case self::UNTAG: - return $user === $subject->getUser(); - } - - return false; + return match ($attribute) { + self::VIEW, self::EDIT, self::RELOAD, self::STAR, self::ARCHIVE, self::SHARE, self::UNSHARE, self::EXPORT, self::DELETE, self::LIST_ANNOTATIONS, self::CREATE_ANNOTATIONS, self::LIST_TAGS, self::TAG, self::UNTAG => $user === $subject->getUser(), + default => false, + }; } } diff --git a/src/Security/Voter/IgnoreOriginInstanceRuleVoter.php b/src/Security/Voter/IgnoreOriginInstanceRuleVoter.php index a63c24772..9fc6eb4a9 100644 --- a/src/Security/Voter/IgnoreOriginInstanceRuleVoter.php +++ b/src/Security/Voter/IgnoreOriginInstanceRuleVoter.php @@ -32,12 +32,9 @@ class IgnoreOriginInstanceRuleVoter extends Voter protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool { - switch ($attribute) { - case self::EDIT: - case self::DELETE: - return $this->security->isGranted('ROLE_SUPER_ADMIN'); - } - - return false; + return match ($attribute) { + self::EDIT, self::DELETE => $this->security->isGranted('ROLE_SUPER_ADMIN'), + default => false, + }; } } diff --git a/src/Security/Voter/IgnoreOriginUserRuleVoter.php b/src/Security/Voter/IgnoreOriginUserRuleVoter.php index f7186f835..d6f9b15b6 100644 --- a/src/Security/Voter/IgnoreOriginUserRuleVoter.php +++ b/src/Security/Voter/IgnoreOriginUserRuleVoter.php @@ -35,12 +35,9 @@ class IgnoreOriginUserRuleVoter extends Voter return false; } - switch ($attribute) { - case self::EDIT: - case self::DELETE: - return $subject->getConfig()->getUser() === $user; - } - - return false; + return match ($attribute) { + self::EDIT, self::DELETE => $subject->getConfig()->getUser() === $user, + default => false, + }; } } diff --git a/src/Security/Voter/MainVoter.php b/src/Security/Voter/MainVoter.php index 3b5ff92cd..7915ac9c9 100644 --- a/src/Security/Voter/MainVoter.php +++ b/src/Security/Voter/MainVoter.php @@ -41,22 +41,9 @@ class MainVoter extends Voter protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool { - switch ($attribute) { - case self::LIST_ENTRIES: - case self::CREATE_ENTRIES: - case self::EDIT_ENTRIES: - case self::EXPORT_ENTRIES: - case self::IMPORT_ENTRIES: - case self::DELETE_ENTRIES: - case self::LIST_TAGS: - case self::CREATE_TAGS: - case self::DELETE_TAGS: - case self::LIST_SITE_CREDENTIALS: - case self::CREATE_SITE_CREDENTIALS: - case self::EDIT_CONFIG: - return $this->security->isGranted('ROLE_USER'); - } - - return false; + return match ($attribute) { + self::LIST_ENTRIES, self::CREATE_ENTRIES, self::EDIT_ENTRIES, self::EXPORT_ENTRIES, self::IMPORT_ENTRIES, self::DELETE_ENTRIES, self::LIST_TAGS, self::CREATE_TAGS, self::DELETE_TAGS, self::LIST_SITE_CREDENTIALS, self::CREATE_SITE_CREDENTIALS, self::EDIT_CONFIG => $this->security->isGranted('ROLE_USER'), + default => false, + }; } } diff --git a/src/Security/Voter/SiteCredentialVoter.php b/src/Security/Voter/SiteCredentialVoter.php index 35ab28484..89c6982d4 100644 --- a/src/Security/Voter/SiteCredentialVoter.php +++ b/src/Security/Voter/SiteCredentialVoter.php @@ -35,12 +35,9 @@ class SiteCredentialVoter extends Voter return false; } - switch ($attribute) { - case self::EDIT: - case self::DELETE: - return $user === $subject->getUser(); - } - - return false; + return match ($attribute) { + self::EDIT, self::DELETE => $user === $subject->getUser(), + default => false, + }; } } diff --git a/src/Security/Voter/TagVoter.php b/src/Security/Voter/TagVoter.php index e81b1944d..f5f239b1b 100644 --- a/src/Security/Voter/TagVoter.php +++ b/src/Security/Voter/TagVoter.php @@ -42,13 +42,9 @@ class TagVoter extends Voter return false; } - switch ($attribute) { - case self::VIEW: - case self::EDIT: - case self::DELETE: - return $this->security->isGranted('ROLE_USER'); - } - - return false; + return match ($attribute) { + self::VIEW, self::EDIT, self::DELETE => $this->security->isGranted('ROLE_USER'), + default => false, + }; } } diff --git a/src/Security/Voter/TaggingRuleVoter.php b/src/Security/Voter/TaggingRuleVoter.php index 01113957d..fc98ac66b 100644 --- a/src/Security/Voter/TaggingRuleVoter.php +++ b/src/Security/Voter/TaggingRuleVoter.php @@ -35,12 +35,9 @@ class TaggingRuleVoter extends Voter return false; } - switch ($attribute) { - case self::EDIT: - case self::DELETE: - return $subject->getConfig()->getUser() === $user; - } - - return false; + return match ($attribute) { + self::EDIT, self::DELETE => $subject->getConfig()->getUser() === $user, + default => false, + }; } } diff --git a/src/SiteConfig/ArraySiteConfigBuilder.php b/src/SiteConfig/ArraySiteConfigBuilder.php index 188ca42c9..c6d20a501 100644 --- a/src/SiteConfig/ArraySiteConfigBuilder.php +++ b/src/SiteConfig/ArraySiteConfigBuilder.php @@ -21,7 +21,7 @@ class ArraySiteConfigBuilder implements SiteConfigBuilder { $host = strtolower($host); - if ('www.' === substr($host, 0, 4)) { + if (str_starts_with($host, 'www.')) { $host = substr($host, 4); } diff --git a/src/SiteConfig/GrabySiteConfigBuilder.php b/src/SiteConfig/GrabySiteConfigBuilder.php index 9bb01f6a1..fe8ba1173 100644 --- a/src/SiteConfig/GrabySiteConfigBuilder.php +++ b/src/SiteConfig/GrabySiteConfigBuilder.php @@ -26,7 +26,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder // required by credentials below $host = strtolower($host); - if ('www.' === substr($host, 0, 4)) { + if (str_starts_with($host, 'www.')) { $host = substr($host, 4); } diff --git a/src/SiteConfig/LoginFormAuthenticator.php b/src/SiteConfig/LoginFormAuthenticator.php index f59013684..3a4050714 100644 --- a/src/SiteConfig/LoginFormAuthenticator.php +++ b/src/SiteConfig/LoginFormAuthenticator.php @@ -66,7 +66,7 @@ class LoginFormAuthenticator $crawler = new Crawler((string) $html); $loggedIn = $crawler->evaluate((string) $siteConfig->getNotLoggedInXpath()); - } catch (\Throwable $e) { + } catch (\Throwable) { return false; } @@ -98,7 +98,7 @@ class LoginFormAuthenticator $extraFields = []; foreach ($siteConfig->getExtraFields() as $fieldName => $fieldValue) { - if ('@=' === substr($fieldValue, 0, 2)) { + if (str_starts_with($fieldValue, '@=')) { $fieldValue = $this->expressionLanguage->evaluate( substr($fieldValue, 2), [ diff --git a/src/Twig/WallabagExtension.php b/src/Twig/WallabagExtension.php index 2aa5c7a66..649bc3503 100644 --- a/src/Twig/WallabagExtension.php +++ b/src/Twig/WallabagExtension.php @@ -89,25 +89,14 @@ class WallabagExtension extends AbstractExtension implements GlobalsInterface return 0; } - switch ($type) { - case 'starred': - $qb = $this->entryRepository->getCountBuilderForStarredByUser($user->getId())->select('COUNT(e.id)'); - break; - case 'archive': - $qb = $this->entryRepository->getCountBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)'); - break; - case 'unread': - $qb = $this->entryRepository->getCountBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)'); - break; - case 'annotated': - $qb = $this->annotationRepository->getCountBuilderByUser($user->getId())->select('COUNT(DISTINCT e.entry)'); - break; - case 'all': - $qb = $this->entryRepository->getCountBuilderForAllByUser($user->getId())->select('COUNT(e.id)'); - break; - default: - throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)); - } + $qb = match ($type) { + 'starred' => $this->entryRepository->getCountBuilderForStarredByUser($user->getId())->select('COUNT(e.id)'), + 'archive' => $this->entryRepository->getCountBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)'), + 'unread' => $this->entryRepository->getCountBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)'), + 'annotated' => $this->annotationRepository->getCountBuilderByUser($user->getId())->select('COUNT(DISTINCT e.entry)'), + 'all' => $this->entryRepository->getCountBuilderForAllByUser($user->getId())->select('COUNT(e.id)'), + default => throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)), + }; $query = $qb->getQuery(); $query->useQueryCache(true); diff --git a/tests/WallabagTestCase.php b/tests/WallabagTestCase.php index eed714980..9b31f8029 100644 --- a/tests/WallabagTestCase.php +++ b/tests/WallabagTestCase.php @@ -120,7 +120,7 @@ abstract class WallabagTestCase extends WebTestCase { try { $this->client->getContainer()->get(Client::class)->connect(); - } catch (\Exception $e) { + } catch (\Exception) { $this->markTestSkipped('Redis is not installed/activated'); } }