From 006b5427d3a9277e69e32c96a263fec954377ffd Mon Sep 17 00:00:00 2001
From: Alexander Schnitzler <git@alexanderschnitzler.de>
Date: Mon, 11 May 2020 17:33:41 +0200
Subject: [PATCH] [TASK] Fix phpstan checkFunctionArgumentTypes errors in
 ext:core Routing

This patch fixes incompatible type usage in function arguments
and is preparatory work for introducing native type hints and
strict mode in all core files.

Releases: master, 10.4
Resolves: #92278
Change-Id: I242553c64f934fbce2dc7762c4fcce4d01b5a0c9
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65676
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
---
 typo3/sysext/core/Classes/Routing/PageRouter.php            | 6 +++---
 .../core/Classes/Routing/PageSlugCandidateProvider.php      | 3 ++-
 typo3/sysext/core/Classes/Routing/PageUriMatcher.php        | 4 ++--
 typo3/sysext/core/Classes/Routing/SiteMatcher.php           | 6 +++---
 4 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/typo3/sysext/core/Classes/Routing/PageRouter.php b/typo3/sysext/core/Classes/Routing/PageRouter.php
index aff9edca1f31..1190dbd573fb 100644
--- a/typo3/sysext/core/Classes/Routing/PageRouter.php
+++ b/typo3/sysext/core/Classes/Routing/PageRouter.php
@@ -90,7 +90,7 @@ class PageRouter implements RouterInterface
     protected $cacheHashCalculator;
 
     /**
-     * @var \TYPO3\CMS\Core\Context\Context|null
+     * @var \TYPO3\CMS\Core\Context\Context
      */
     protected $context;
 
@@ -130,7 +130,7 @@ class PageRouter implements RouterInterface
         if ($requestId > 0) {
             if (!empty($pageId = $candidateProvider->getRealPageIdForPageIdAsPossibleCandidate($requestId))) {
                 return new PageArguments(
-                    $pageId,
+                    (int)$pageId,
                     (string)($request->getQueryParams()['type'] ?? '0'),
                     [],
                     [],
@@ -260,7 +260,7 @@ class PageRouter implements RouterInterface
             // with the base of the MountPoint page, this is especially relevant for cross-domain linking
             // Because the language contains the full base, it is retrieved in this case.
             try {
-                [, $mountPointPage] = explode('-', reset($mountPointPairs));
+                [, $mountPointPage] = explode('-', (string)reset($mountPointPairs));
                 $site = GeneralUtility::makeInstance(SiteMatcher::class)
                     ->matchByPageId((int)$mountPointPage);
                 $language = $site->getLanguageById($language->getLanguageId());
diff --git a/typo3/sysext/core/Classes/Routing/PageSlugCandidateProvider.php b/typo3/sysext/core/Classes/Routing/PageSlugCandidateProvider.php
index a0cf77a67406..c87c00372765 100644
--- a/typo3/sysext/core/Classes/Routing/PageSlugCandidateProvider.php
+++ b/typo3/sysext/core/Classes/Routing/PageSlugCandidateProvider.php
@@ -304,6 +304,7 @@ class PageSlugCandidateProvider
 
             // Add possible sub-pages prepended with the MountPoint page slug
             if ($mountPageInformation) {
+                /** @var array $mountedPage */
                 $siteOfMountedPage = $siteFinder->getSiteByPageId((int)$mountedPage['uid']);
                 $morePageCandidates = $this->findPageCandidatesOfMountPoint(
                     $row,
@@ -447,7 +448,7 @@ class PageSlugCandidateProvider
         if (!empty($redecorationPattern) && preg_match('#' . $redecorationPattern . '#', $routePath, $matches)) {
             $decoration = $matches['decoration'];
             $decorationPattern = preg_quote($decoration, '#');
-            $routePath = preg_replace('#' . $decorationPattern . '$#', '', $routePath);
+            $routePath = preg_replace('#' . $decorationPattern . '$#', '', $routePath) ?? '';
         }
 
         $candidatePathParts = [];
diff --git a/typo3/sysext/core/Classes/Routing/PageUriMatcher.php b/typo3/sysext/core/Classes/Routing/PageUriMatcher.php
index 3ec665d055e8..c961d31e0eda 100644
--- a/typo3/sysext/core/Classes/Routing/PageUriMatcher.php
+++ b/typo3/sysext/core/Classes/Routing/PageUriMatcher.php
@@ -32,7 +32,7 @@ use TYPO3\CMS\Core\Routing\Aspect\MappableProcessor;
 class PageUriMatcher
 {
     /**
-     * @var RouteCollection
+     * @var RouteCollection<string, Route>
      */
     protected $routes;
 
@@ -69,7 +69,7 @@ class PageUriMatcher
      * Tries to match a URL with a set of routes.
      *
      * @param string $urlPath The path info to be parsed
-     * @param RouteCollection $routes The set of routes
+     * @param RouteCollection<string,Route> $routes The set of routes
      * @return array An array of parameters
      */
     protected function matchCollection(string $urlPath, RouteCollection $routes): ?array
diff --git a/typo3/sysext/core/Classes/Routing/SiteMatcher.php b/typo3/sysext/core/Classes/Routing/SiteMatcher.php
index b2c1a0bd93cc..7d4957baf99c 100644
--- a/typo3/sysext/core/Classes/Routing/SiteMatcher.php
+++ b/typo3/sysext/core/Classes/Routing/SiteMatcher.php
@@ -90,7 +90,7 @@ class SiteMatcher implements SingletonInterface
      */
     public function matchRequest(ServerRequestInterface $request): RouteResultInterface
     {
-        $site = null;
+        $site = new NullSite();
         $language = null;
         $defaultLanguage = null;
 
@@ -125,7 +125,7 @@ class SiteMatcher implements SingletonInterface
             $context = new RequestContext(
                 '',
                 $request->getMethod(),
-                HttpUtility::idn_to_ascii($request->getUri()->getHost()),
+                (string)HttpUtility::idn_to_ascii($request->getUri()->getHost()),
                 $request->getUri()->getScheme(),
                 // Ports are only necessary for URL generation in Symfony which is not used by TYPO3
                 80,
@@ -202,7 +202,7 @@ class SiteMatcher implements SingletonInterface
                     ['site' => $site, 'language' => $siteLanguage, 'tail' => ''],
                     array_filter(['tail' => '.*', 'port' => (string)$uri->getPort()]),
                     ['utf8' => true],
-                    HttpUtility::idn_to_ascii($uri->getHost()) ?: '',
+                    (string)(HttpUtility::idn_to_ascii($uri->getHost()) ?: ''),
                     $uri->getScheme()
                 );
                 $identifier = 'site_' . $site->getIdentifier() . '_' . $siteLanguage->getLanguageId();
-- 
GitLab