Commit 97b3bbc5 authored by Alexander Schnitzler's avatar Alexander Schnitzler Committed by Anja Leichsenring
Browse files

[TASK] Harden \TYPO3\CMS\Extbase\Utility\LocalizationUtility

- 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's avatarOliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: default avatarJörg Bösche <typo3@joergboesche.de>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Tested-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
parent 66365a9f
<?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'];
}
......
......@@ -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');
......
......@@ -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);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment