From c68090f2e802e363869dc3d2f79f2b51f3e5f8be Mon Sep 17 00:00:00 2001
From: Simon Praetorius <simon@praetorius.me>
Date: Mon, 9 Sep 2024 15:28:27 +0200
Subject: [PATCH] [TASK] Avoid constructor argument of TemplatePaths
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

A previous patch already avoided calls to the constructor
of `TemplatePaths`, which is deprecated if called with an
argument since Fluid v2.15. This follow-up covers remaining
places.

Furthermore, both usages of `GeneralUtility::makeInstance()`
are converted to `new` to harmonize the creation of those
objects in the Core. Now every occurrences use `new`.

`ksort()` is removed because this is done internally in
`TemplatePaths` setters. It was necessary before because
of the merging behavior of `extractPathArrays()`, which
is no longer used now (and is also deprecated in Fluid).

Resolves: #104860
Related: #104823
Releases: main
Change-Id: Ic16638e8512f6272dbd52bbf0f65fb405b8483cf
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/85926
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Simon Praetorius <simon@praetorius.me>
Reviewed-by: Simon Praetorius <simon@praetorius.me>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
---
 .../Domain/Finishers/EmailFinisher.php        | 39 ++++++---------
 .../Domain/Finishers/EmailFinisherTest.php    | 50 +++++++++----------
 .../Classes/Task/ValidatorTask.php            | 15 +++---
 3 files changed, 46 insertions(+), 58 deletions(-)

diff --git a/typo3/sysext/form/Classes/Domain/Finishers/EmailFinisher.php b/typo3/sysext/form/Classes/Domain/Finishers/EmailFinisher.php
index 811cc02aa36f..371fc9bafbe0 100644
--- a/typo3/sysext/form/Classes/Domain/Finishers/EmailFinisher.php
+++ b/typo3/sysext/form/Classes/Domain/Finishers/EmailFinisher.php
@@ -162,31 +162,20 @@ class EmailFinisher extends AbstractFinisher
 
     protected function initializeTemplatePaths(array $globalConfig, array $localConfig): TemplatePaths
     {
-        if (is_array($localConfig['templateRootPaths'] ?? null)) {
-            $globalConfig['templateRootPaths'] = array_replace_recursive(
-                $globalConfig['templateRootPaths'],
-                $localConfig['templateRootPaths']
-            );
-            ksort($globalConfig['templateRootPaths']);
-        }
-
-        if (is_array($localConfig['partialRootPaths'] ?? null)) {
-            $globalConfig['partialRootPaths'] = array_replace_recursive(
-                $globalConfig['partialRootPaths'],
-                $localConfig['partialRootPaths']
-            );
-            ksort($globalConfig['partialRootPaths']);
-        }
-
-        if (is_array($localConfig['layoutRootPaths'] ?? null)) {
-            $globalConfig['layoutRootPaths'] = array_replace_recursive(
-                $globalConfig['layoutRootPaths'],
-                $localConfig['layoutRootPaths']
-            );
-            ksort($globalConfig['layoutRootPaths']);
-        }
-
-        return GeneralUtility::makeInstance(TemplatePaths::class, $globalConfig);
+        $templatePaths = new TemplatePaths();
+        $templatePaths->setTemplateRootPaths(array_replace(
+            $globalConfig['templateRootPaths'] ?? [],
+            $localConfig['templateRootPaths'] ?? [],
+        ));
+        $templatePaths->setLayoutRootPaths(array_replace(
+            $globalConfig['layoutRootPaths'] ?? [],
+            $localConfig['layoutRootPaths'] ?? [],
+        ));
+        $templatePaths->setPartialRootPaths(array_replace(
+            $globalConfig['partialRootPaths'] ?? [],
+            $localConfig['partialRootPaths'] ?? [],
+        ));
+        return $templatePaths;
     }
 
     protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
diff --git a/typo3/sysext/form/Tests/Unit/Domain/Finishers/EmailFinisherTest.php b/typo3/sysext/form/Tests/Unit/Domain/Finishers/EmailFinisherTest.php
index d6256382c6ed..f8d1c77c5929 100644
--- a/typo3/sysext/form/Tests/Unit/Domain/Finishers/EmailFinisherTest.php
+++ b/typo3/sysext/form/Tests/Unit/Domain/Finishers/EmailFinisherTest.php
@@ -52,16 +52,16 @@ final class EmailFinisherTest extends UnitTestCase
                 ],
                 'expected' => [
                     'templateRootPaths' => [
-                        'EXT:core/Resources/Private/Templates/Email/',
-                        'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
+                        0 => 'EXT:core/Resources/Private/Templates/Email/',
+                        10 => 'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
                     ],
                     'layoutRootPaths' => [
-                        'EXT:core/Resources/Private/Layouts/',
-                        'EXT:backend/Resources/Private/Layouts/',
+                        0 => 'EXT:core/Resources/Private/Layouts/',
+                        10 => 'EXT:backend/Resources/Private/Layouts/',
                     ],
                     'partialRootPaths' => [
-                        'EXT:core/Resources/Private/Partials/',
-                        'EXT:backend/Resources/Private/Partials/',
+                        0 => 'EXT:core/Resources/Private/Partials/',
+                        10 => 'EXT:backend/Resources/Private/Partials/',
                     ],
                 ],
             ],
@@ -91,17 +91,17 @@ final class EmailFinisherTest extends UnitTestCase
                 ],
                 'expected' => [
                     'templateRootPaths' => [
-                        'EXT:core/Resources/Private/Templates/Email/',
-                        'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
-                        'EXT:myextension/Resources/Private/Templates/Form/Email/',
+                        0 => 'EXT:core/Resources/Private/Templates/Email/',
+                        10 => 'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
+                        20 => 'EXT:myextension/Resources/Private/Templates/Form/Email/',
                     ],
                     'layoutRootPaths' => [
-                        'EXT:core/Resources/Private/Layouts/',
-                        'EXT:backend/Resources/Private/Layouts/',
+                        0 => 'EXT:core/Resources/Private/Layouts/',
+                        10 => 'EXT:backend/Resources/Private/Layouts/',
                     ],
                     'partialRootPaths' => [
-                        'EXT:core/Resources/Private/Partials/',
-                        'EXT:backend/Resources/Private/Partials/',
+                        0 => 'EXT:core/Resources/Private/Partials/',
+                        10 => 'EXT:backend/Resources/Private/Partials/',
                     ],
                 ],
             ],
@@ -138,22 +138,22 @@ final class EmailFinisherTest extends UnitTestCase
                 ],
                 'expected' => [
                     'templateRootPaths' => [
-                        'EXT:core/Resources/Private/Templates/Email/',
-                        'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
-                        'path/to/myextension/Resources/Private/Templates/Email/',
-                        'path/to/myextension/Resources/Private/Templates/Form/Email/',
+                        0 => 'EXT:core/Resources/Private/Templates/Email/',
+                        10 => 'EXT:form/Resources/Private/Frontend/Templates/Finishers/Email/',
+                        20 => 'path/to/myextension/Resources/Private/Templates/Email/',
+                        100 => 'path/to/myextension/Resources/Private/Templates/Form/Email/',
                     ],
                     'layoutRootPaths' => [
-                        'EXT:core/Resources/Private/Layouts/',
-                        'EXT:backend/Resources/Private/Layouts/',
-                        'path/to/myextension/Resources/Private/Layouts/Email/',
-                        'path/to/myextension/Resources/Private/Layouts/Form/Email/',
+                        0 => 'EXT:core/Resources/Private/Layouts/',
+                        10 => 'EXT:backend/Resources/Private/Layouts/',
+                        20 => 'path/to/myextension/Resources/Private/Layouts/Email/',
+                        100 => 'path/to/myextension/Resources/Private/Layouts/Form/Email/',
                     ],
                     'partialRootPaths' => [
-                        'EXT:core/Resources/Private/Partials/',
-                        'EXT:backend/Resources/Private/Partials/',
-                        'path/to/myextension/Resources/Private/Partials/Email/',
-                        'path/to/myextension/Resources/Private/Partials/Form/Email/',
+                        0 => 'EXT:core/Resources/Private/Partials/',
+                        10 => 'EXT:backend/Resources/Private/Partials/',
+                        20 => 'path/to/myextension/Resources/Private/Partials/Email/',
+                        100 => 'path/to/myextension/Resources/Private/Partials/Form/Email/',
                     ],
                 ],
             ],
diff --git a/typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php b/typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php
index 1d28ee28d92c..8c2ab76e8998 100644
--- a/typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php
+++ b/typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php
@@ -506,14 +506,13 @@ class ValidatorTask extends AbstractTask
      */
     protected function getFluidEmail(): FluidEmail
     {
-        $templateConfiguration = array_replace_recursive(
-            $GLOBALS['TYPO3_CONF_VARS']['MAIL'],
-            ['templateRootPaths' => [20 => 'EXT:linkvalidator/Resources/Private/Templates/Email/']]
-        );
-
-        // must be sorted after adding the default path to ensure already registered custom paths are called first
-        ksort($templateConfiguration['templateRootPaths']);
-        $templatePaths = GeneralUtility::makeInstance(TemplatePaths::class, $templateConfiguration);
+        $templatePaths = new TemplatePaths();
+        $templatePaths->setTemplateRootPaths(array_replace(
+            $GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] ?? [],
+            [20 => 'EXT:linkvalidator/Resources/Private/Templates/Email/'],
+        ));
+        $templatePaths->setLayoutRootPaths($GLOBALS['TYPO3_CONF_VARS']['MAIL']['layoutRootPaths'] ?? []);
+        $templatePaths->setPartialRootPaths($GLOBALS['TYPO3_CONF_VARS']['MAIL']['partialRootPaths'] ?? []);
 
         if ($this->emailTemplateName === '' || !$this->templateFilesExist($templatePaths->getTemplateRootPaths())) {
             // Add default template name to task if empty or given template name does not exist
-- 
GitLab