[TASK] Harden \TYPO3\CMS\Extbase\Utility\LocalizationUtility 95/59595/5
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Thu, 31 Jan 2019 18:30:51 +0000 (19:30 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 17 Feb 2019 09:55:34 +0000 (10:55 +0100)
- Use strict type mode
- Use type hints whereever possible

Releases: master
Resolves: #87600
Change-Id: Ie57d5515f97909191b61927d820713615c6a8b69
Reviewed-on: https://review.typo3.org/c/59595
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/extbase/Classes/Utility/LocalizationUtility.php
typo3/sysext/extbase/Tests/Functional/Mvc/Validation/ActionControllerValidationTest.php
typo3/sysext/extbase/Tests/Unit/Utility/LocalizationUtilityTest.php

index 3a7027e..13d5b9e 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+declare(strict_types = 1);
+
 namespace TYPO3\CMS\Extbase\Utility;
 
 /*
@@ -64,9 +66,9 @@ class LocalizationUtility
      * @param string[] $alternativeLanguageKeys The alternative language keys if no translation was found. If null and we are in the frontend, then the language_alt from TypoScript setup will be used
      * @return string|null The value from LOCAL_LANG or null if no translation was found.
      */
-    public static function translate($key, $extensionName = null, $arguments = null, string $languageKey = null, array $alternativeLanguageKeys = null)
+    public static function translate(string $key, ?string $extensionName = null, array $arguments = null, string $languageKey = null, array $alternativeLanguageKeys = null): ?string
     {
-        if ((string)$key === '') {
+        if ($key === '') {
             // Early return guard: returns null if the key was empty, because the key may be a dynamic value
             // (from for example Fluid). Returning null allows null coalescing to a default value when that happens.
             return null;
@@ -139,7 +141,7 @@ class LocalizationUtility
      * @param string[] $alternativeLanguageKeys
      * @param string $extensionName
      */
-    protected static function initializeLocalization(string $languageFilePath, string $languageKey, array $alternativeLanguageKeys, string $extensionName = null)
+    protected static function initializeLocalization(string $languageFilePath, string $languageKey, array $alternativeLanguageKeys, string $extensionName = null): void
     {
         $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
 
@@ -227,7 +229,7 @@ class LocalizationUtility
      * @param string $extensionName
      * @param string $languageFilePath
      */
-    protected static function loadTypoScriptLabels($extensionName, $languageFilePath)
+    protected static function loadTypoScriptLabels(string $extensionName, string $languageFilePath): void
     {
         $configurationManager = static::getConfigurationManager();
         $frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
@@ -269,7 +271,7 @@ class LocalizationUtility
      * @param string $parentKey the name of the parent key in the recursion; is only needed for recursion.
      * @return array flattened array of labels.
      */
-    protected static function flattenTypoScriptLabelArray(array $labelValues, $parentKey = '')
+    protected static function flattenTypoScriptLabelArray(array $labelValues, string $parentKey = ''): array
     {
         $result = [];
         foreach ($labelValues as $key => $labelValue) {
@@ -295,7 +297,7 @@ class LocalizationUtility
      *
      * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
      */
-    protected static function getConfigurationManager()
+    protected static function getConfigurationManager(): ConfigurationManagerInterface
     {
         if (static::$configurationManager !== null) {
             return static::$configurationManager;
@@ -323,7 +325,7 @@ class LocalizationUtility
     /**
      * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
      */
-    protected static function getTypoScriptFrontendController()
+    protected static function getTypoScriptFrontendController(): \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
     {
         return $GLOBALS['TSFE'];
     }
@@ -331,7 +333,7 @@ class LocalizationUtility
     /**
      * @return \TYPO3\CMS\Core\Localization\LanguageService
      */
-    protected static function getLanguageService()
+    protected static function getLanguageService(): \TYPO3\CMS\Core\Localization\LanguageService
     {
         return $GLOBALS['LANG'];
     }
index 424a453..3e6226f 100644 (file)
@@ -67,6 +67,7 @@ class ActionControllerValidationTest extends FunctionalTestCase
      */
     public function forwardedActionValidatesPreviouslyIgnoredArgument(array $blogPostArgument, array $trustedProperties, array $expectedErrorCodes)
     {
+        $GLOBALS['LANG'] = new \TYPO3\CMS\Core\Localization\LanguageService();
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
 
         $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
@@ -113,6 +114,7 @@ class ActionControllerValidationTest extends FunctionalTestCase
      */
     public function validationResultsAreProvidedForTheSameObjectInDifferentArguments()
     {
+        $GLOBALS['LANG'] = new \TYPO3\CMS\Core\Localization\LanguageService();
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
 
         $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
index d5eb578..5da2c03 100644 (file)
@@ -332,7 +332,7 @@ class LocalizationUtilityTest extends UnitTestCase
         $property = $reflectionClass->getProperty('LOCAL_LANG');
         $property->setAccessible(true);
         $property->setValue($this->LOCAL_LANG);
-        $GLOBALS['LANG'] = $this->LOCAL_LANG;
+        $GLOBALS['LANG'] = new \TYPO3\CMS\Core\Localization\LanguageService();
         $this->assertEquals($expected, LocalizationUtility::translate($key, 'core', $arguments, $languageKey, $altLanguageKeys));
     }
 
@@ -492,7 +492,7 @@ class LocalizationUtilityTest extends UnitTestCase
         $method->setAccessible(true);
         $method->invoke(null, 'core', $this->languageFilePath);
 
-        $GLOBALS['LANG'] = $this->LOCAL_LANG;
+        $GLOBALS['LANG'] = new \TYPO3\CMS\Core\Localization\LanguageService();
 
         $result = LocalizationUtility::translate('key1', 'core', null, 'dk');
         $this->assertNotNull($result);
@@ -534,7 +534,7 @@ class LocalizationUtilityTest extends UnitTestCase
         $method->setAccessible(true);
         $method->invoke(null, 'core', ''); // setting the language file path to an empty string here
 
-        $GLOBALS['LANG'] = $this->LOCAL_LANG;
+        $GLOBALS['LANG'] = new \TYPO3\CMS\Core\Localization\LanguageService();
 
         $result = LocalizationUtility::translate('key1', 'core', null, 'dk');
         $this->assertNotNull($result);