From aa95f0e381293c7016e89f9dc5b26c1f163f48ff Mon Sep 17 00:00:00 2001
From: Torben Hansen <derhansen@gmail.com>
Date: Sun, 5 Mar 2023 12:52:05 +0100
Subject: [PATCH] [TASK] Cleanup middlewares

This change cleans up middlewares by

* using constructor property promotion where possible
* strict types where possible
* Remove `@throws` annotations for exceptions never thrown
* Remove value in function call, if it equals the default

Resolves: #100090
Releases: main
Signed-off-by: Torben Hansen <derhansen@gmail.com>
Change-Id: I1f81a17f54a1b569cc3ef351cbbb601aa76355eb
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78030
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Nikita Hovratov <nikita.h@live.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
---
 .../Middleware/BackendUserAuthenticator.php   |  4 +---
 .../Classes/Middleware/LockedBackendGuard.php |  3 +--
 .../Classes/Middleware/OutputCompression.php  |  2 +-
 .../Middleware/BackendUserAuthenticator.php   |  8 +------
 .../Middleware/BackendUserAuthenticator.php   | 17 +++++----------
 .../Classes/Middleware/EidHandler.php         |  3 ---
 .../Middleware/FrontendUserAuthenticator.php  | 14 ++++---------
 .../Classes/Middleware/OutputCompression.php  |  2 +-
 .../Middleware/PageArgumentValidator.php      | 20 +++---------------
 .../Classes/Middleware/PreviewSimulator.php   |  5 +----
 .../Middleware/SiteBaseRedirectResolver.php   |  4 +---
 .../Classes/Middleware/SiteResolver.php       |  8 +------
 .../Middleware/StaticRouteResolver.php        | 21 +++----------------
 .../Middleware/TimeTrackerInitialization.php  |  8 +------
 14 files changed, 24 insertions(+), 95 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Middleware/BackendUserAuthenticator.php b/typo3/sysext/backend/Classes/Middleware/BackendUserAuthenticator.php
index b22f6617ff1f..4d88ad4a7c49 100644
--- a/typo3/sysext/backend/Classes/Middleware/BackendUserAuthenticator.php
+++ b/typo3/sysext/backend/Classes/Middleware/BackendUserAuthenticator.php
@@ -50,10 +50,8 @@ class BackendUserAuthenticator extends \TYPO3\CMS\Core\Middleware\BackendUserAut
 
     /**
      * List of requests that don't need a valid BE user
-     *
-     * @var array
      */
-    protected $publicRoutes = [
+    protected array $publicRoutes = [
         '/login',
         '/login/frame',
         '/login/password-reset/forget',
diff --git a/typo3/sysext/backend/Classes/Middleware/LockedBackendGuard.php b/typo3/sysext/backend/Classes/Middleware/LockedBackendGuard.php
index b28365712def..d80858392e0a 100644
--- a/typo3/sysext/backend/Classes/Middleware/LockedBackendGuard.php
+++ b/typo3/sysext/backend/Classes/Middleware/LockedBackendGuard.php
@@ -74,10 +74,9 @@ class LockedBackendGuard implements MiddlewareInterface
     /**
      * Check adminOnly configuration variable and redirects to an URL in file typo3conf/LOCK_BACKEND
      *
-     * @return string|null
      * @throws BackendLockedException
      */
-    protected function checkLockedBackend()
+    protected function checkLockedBackend(): ?string
     {
         if ($GLOBALS['TYPO3_CONF_VARS']['BE']['adminOnly'] < 0) {
             throw new BackendLockedException(
diff --git a/typo3/sysext/backend/Classes/Middleware/OutputCompression.php b/typo3/sysext/backend/Classes/Middleware/OutputCompression.php
index b0345f93067e..7a1570dcbf93 100644
--- a/typo3/sysext/backend/Classes/Middleware/OutputCompression.php
+++ b/typo3/sysext/backend/Classes/Middleware/OutputCompression.php
@@ -46,7 +46,7 @@ class OutputCompression implements MiddlewareInterface
     /**
      * Initialize output compression if configured
      */
-    protected function initializeOutputCompression()
+    protected function initializeOutputCompression(): void
     {
         if (extension_loaded('zlib') && $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel']) {
             if (MathUtility::canBeInterpretedAsInteger($GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'])) {
diff --git a/typo3/sysext/core/Classes/Middleware/BackendUserAuthenticator.php b/typo3/sysext/core/Classes/Middleware/BackendUserAuthenticator.php
index a3eda9407bf3..24289fbdca12 100644
--- a/typo3/sysext/core/Classes/Middleware/BackendUserAuthenticator.php
+++ b/typo3/sysext/core/Classes/Middleware/BackendUserAuthenticator.php
@@ -49,14 +49,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  */
 abstract class BackendUserAuthenticator implements MiddlewareInterface
 {
-    /**
-     * @var Context
-     */
-    protected $context;
-
-    public function __construct(Context $context)
+    public function __construct(protected Context $context)
     {
-        $this->context = $context;
     }
 
     abstract public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
diff --git a/typo3/sysext/frontend/Classes/Middleware/BackendUserAuthenticator.php b/typo3/sysext/frontend/Classes/Middleware/BackendUserAuthenticator.php
index 993b9b38eee9..95fea6fbba9a 100644
--- a/typo3/sysext/frontend/Classes/Middleware/BackendUserAuthenticator.php
+++ b/typo3/sysext/frontend/Classes/Middleware/BackendUserAuthenticator.php
@@ -39,14 +39,11 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  */
 class BackendUserAuthenticator extends \TYPO3\CMS\Core\Middleware\BackendUserAuthenticator
 {
-    private LanguageServiceFactory $languageServiceFactory;
-
     public function __construct(
         Context $context,
-        LanguageServiceFactory $languageServiceFactory
+        protected readonly LanguageServiceFactory $languageServiceFactory
     ) {
         parent::__construct($context);
-        $this->languageServiceFactory = $languageServiceFactory;
     }
 
     /**
@@ -89,12 +86,9 @@ class BackendUserAuthenticator extends \TYPO3\CMS\Core\Middleware\BackendUserAut
     }
 
     /**
-     * Creates the backend user object and returns it.
-     *
-     * @return FrontendBackendUserAuthentication|null the backend user object or null if there was no valid user found
-     * @throws \TYPO3\CMS\Core\Exception
+     * Creates the backend user object and returns it if a valid backend user is found.
      */
-    protected function initializeBackendUser(ServerRequestInterface $request)
+    protected function initializeBackendUser(ServerRequestInterface $request): ?FrontendBackendUserAuthentication
     {
         // New backend user object
         $backendUserObject = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class);
@@ -119,10 +113,9 @@ class BackendUserAuthenticator extends \TYPO3\CMS\Core\Middleware\BackendUserAut
 
     /**
      * Implementing the access checks that the TYPO3 CMS bootstrap script does before a user is ever logged in.
-     *
-     * @return bool Returns TRUE if access is OK
+     * Returns TRUE if access is OK
      */
-    protected function isAuthenticated(FrontendBackendUserAuthentication $user, NormalizedParams $normalizedParams)
+    protected function isAuthenticated(FrontendBackendUserAuthentication $user, NormalizedParams $normalizedParams): bool
     {
         // Check IP
         $ipMask = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['IPmaskList'] ?? '');
diff --git a/typo3/sysext/frontend/Classes/Middleware/EidHandler.php b/typo3/sysext/frontend/Classes/Middleware/EidHandler.php
index f9981f676fb1..15502ba211fd 100644
--- a/typo3/sysext/frontend/Classes/Middleware/EidHandler.php
+++ b/typo3/sysext/frontend/Classes/Middleware/EidHandler.php
@@ -21,7 +21,6 @@ use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\MiddlewareInterface;
 use Psr\Http\Server\RequestHandlerInterface;
-use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Http\DispatcherInterface;
 use TYPO3\CMS\Core\Http\Response;
 
@@ -41,8 +40,6 @@ class EidHandler implements MiddlewareInterface
 
     /**
      * Dispatches the request to the corresponding eID class or eID script
-     *
-     * @throws Exception
      */
     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
     {
diff --git a/typo3/sysext/frontend/Classes/Middleware/FrontendUserAuthenticator.php b/typo3/sysext/frontend/Classes/Middleware/FrontendUserAuthenticator.php
index d8a7a2ed69c6..c30e892916b5 100644
--- a/typo3/sysext/frontend/Classes/Middleware/FrontendUserAuthenticator.php
+++ b/typo3/sysext/frontend/Classes/Middleware/FrontendUserAuthenticator.php
@@ -39,16 +39,10 @@ class FrontendUserAuthenticator implements MiddlewareInterface, LoggerAwareInter
 {
     use LoggerAwareTrait;
 
-    /**
-     * @var Context
-     */
-    protected $context;
-    protected RateLimiterFactory $rateLimiterFactory;
-
-    public function __construct(Context $context, RateLimiterFactory $rateLimiterFactory)
-    {
-        $this->context = $context;
-        $this->rateLimiterFactory = $rateLimiterFactory;
+    public function __construct(
+        protected readonly Context $context,
+        protected readonly RateLimiterFactory $rateLimiterFactory
+    ) {
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Middleware/OutputCompression.php b/typo3/sysext/frontend/Classes/Middleware/OutputCompression.php
index c77c0647315f..deddcf9e98b3 100644
--- a/typo3/sysext/frontend/Classes/Middleware/OutputCompression.php
+++ b/typo3/sysext/frontend/Classes/Middleware/OutputCompression.php
@@ -47,7 +47,7 @@ class OutputCompression implements MiddlewareInterface
     /**
      * Initialize output compression if configured
      */
-    protected function initializeOutputCompression()
+    protected function initializeOutputCompression(): void
     {
         if ($GLOBALS['TYPO3_CONF_VARS']['FE']['compressionLevel'] && extension_loaded('zlib')) {
             if (MathUtility::canBeInterpretedAsInteger($GLOBALS['TYPO3_CONF_VARS']['FE']['compressionLevel'])) {
diff --git a/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php b/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php
index af9db15809b1..cf0c9fcceed2 100644
--- a/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php
+++ b/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php
@@ -40,29 +40,15 @@ class PageArgumentValidator implements MiddlewareInterface, LoggerAwareInterface
 {
     use LoggerAwareTrait;
 
-    /**
-     * The cHash Service class used for cHash related functionality
-     *
-     * @var CacheHashCalculator
-     */
-    protected $cacheHashCalculator;
-
-    /**
-     * @var TimeTracker
-     */
-    protected $timeTracker;
-
     /**
      * @var bool will be used to set $TSFE->no_cache later-on
      */
-    protected $disableCache = false;
+    protected bool $disableCache = false;
 
     public function __construct(
-        CacheHashCalculator $cacheHashCalculator,
-        TimeTracker $timeTracker
+        protected readonly CacheHashCalculator $cacheHashCalculator,
+        protected readonly TimeTracker $timeTracker
     ) {
-        $this->cacheHashCalculator = $cacheHashCalculator;
-        $this->timeTracker = $timeTracker;
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Middleware/PreviewSimulator.php b/typo3/sysext/frontend/Classes/Middleware/PreviewSimulator.php
index 7ff89d89b96c..ffe0d1cb6a02 100644
--- a/typo3/sysext/frontend/Classes/Middleware/PreviewSimulator.php
+++ b/typo3/sysext/frontend/Classes/Middleware/PreviewSimulator.php
@@ -39,11 +39,8 @@ use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
  */
 class PreviewSimulator implements MiddlewareInterface
 {
-    private Context $context;
-
-    public function __construct(Context $context)
+    public function __construct(protected readonly Context $context)
     {
-        $this->context = $context;
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Middleware/SiteBaseRedirectResolver.php b/typo3/sysext/frontend/Classes/Middleware/SiteBaseRedirectResolver.php
index 45b2b259883d..8ef8051b304b 100644
--- a/typo3/sysext/frontend/Classes/Middleware/SiteBaseRedirectResolver.php
+++ b/typo3/sysext/frontend/Classes/Middleware/SiteBaseRedirectResolver.php
@@ -92,10 +92,8 @@ class SiteBaseRedirectResolver implements MiddlewareInterface
 
     /**
      * Checks if the language is allowed in Frontend, if not, check if there is valid BE user
-     *
-     * @param BackendUserAuthentication|null $user
      */
-    protected function isLanguageEnabled(SiteLanguage $language, BackendUserAuthentication $user = null): bool
+    protected function isLanguageEnabled(SiteLanguage $language, ?BackendUserAuthentication $user = null): bool
     {
         // language is hidden, check if a possible backend user is allowed to access the language
         if ($language->enabled() || ($user instanceof BackendUserAuthentication && $user->checkLanguageAccess($language->getLanguageId()))) {
diff --git a/typo3/sysext/frontend/Classes/Middleware/SiteResolver.php b/typo3/sysext/frontend/Classes/Middleware/SiteResolver.php
index 169b9b54ef92..88f0e5f54715 100644
--- a/typo3/sysext/frontend/Classes/Middleware/SiteResolver.php
+++ b/typo3/sysext/frontend/Classes/Middleware/SiteResolver.php
@@ -35,14 +35,8 @@ use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
  */
 class SiteResolver implements MiddlewareInterface
 {
-    /**
-     * @var SiteMatcher
-     */
-    protected $matcher;
-
-    public function __construct(SiteMatcher $matcher)
+    public function __construct(protected readonly SiteMatcher $matcher)
     {
-        $this->matcher = $matcher;
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Middleware/StaticRouteResolver.php b/typo3/sysext/frontend/Classes/Middleware/StaticRouteResolver.php
index ca29d356d938..b37f75fdcb43 100644
--- a/typo3/sysext/frontend/Classes/Middleware/StaticRouteResolver.php
+++ b/typo3/sysext/frontend/Classes/Middleware/StaticRouteResolver.php
@@ -35,22 +35,10 @@ use TYPO3\CMS\Core\Site\Entity\Site;
  */
 class StaticRouteResolver implements MiddlewareInterface
 {
-    /**
-     * @var RequestFactory
-     */
-    protected $requestFactory;
-
-    /**
-     * @var LinkService
-     */
-    protected $linkService;
-
     public function __construct(
-        RequestFactory $requestFactory,
-        LinkService $linkService
+        protected readonly RequestFactory $requestFactory,
+        protected readonly LinkService $linkService
     ) {
-        $this->requestFactory = $requestFactory;
-        $this->linkService = $linkService;
     }
 
     /**
@@ -58,7 +46,7 @@ class StaticRouteResolver implements MiddlewareInterface
      */
     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
     {
-        if (($site = $request->getAttribute('site', null)) instanceof Site &&
+        if (($site = $request->getAttribute('site')) instanceof Site &&
             ($configuration = $site->getConfiguration()['routes'] ?? null)
         ) {
             $path = ltrim($request->getUri()->getPath(), '/');
@@ -127,9 +115,6 @@ class StaticRouteResolver implements MiddlewareInterface
         return [$content, $contentType];
     }
 
-    /**
-     * @throws InvalidRouteArgumentsException
-     */
     protected function getPageUri(ServerRequestInterface $request, Site $site, array $urlParams): string
     {
         $parameters = [];
diff --git a/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php b/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php
index 8915353776d4..f644fc7e6830 100644
--- a/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php
+++ b/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php
@@ -31,14 +31,8 @@ use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
  */
 class TimeTrackerInitialization implements MiddlewareInterface
 {
-    /**
-     * @var TimeTracker
-     */
-    protected $timeTracker;
-
-    public function __construct(TimeTracker $timeTracker)
+    public function __construct(protected readonly TimeTracker $timeTracker)
     {
-        $this->timeTracker = $timeTracker;
     }
 
     /**
-- 
GitLab