Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19356 from MauricioFauth/language-refactor
Browse files Browse the repository at this point in the history
Refactor LanguageManager class
  • Loading branch information
MauricioFauth authored Nov 5, 2024
2 parents 75cbd0d + 5258f2a commit e328ec4
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 201 deletions.
47 changes: 7 additions & 40 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7854,26 +7854,6 @@ parameters:
count: 2
path: src/Http/ServerRequest.php

-
message: "#^Cannot access offset 'a_meta_dir' on mixed\\.$#"
count: 1
path: src/I18n/Language.php

-
message: "#^Cannot access offset 'a_meta_language' on mixed\\.$#"
count: 1
path: src/I18n/Language.php

-
message: "#^Cannot access offset 'w_page' on mixed\\.$#"
count: 1
path: src/I18n/Language.php

-
message: "#^Parameter \\#2 \\$path of function _bindtextdomain expects string, mixed given\\.$#"
count: 1
path: src/I18n/Language.php

-
message: """
#^Call to deprecated method getInstance\\(\\) of class PhpMyAdmin\\\\Config\\:
Expand All @@ -7883,20 +7863,10 @@ parameters:
path: src/I18n/LanguageManager.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 5
path: src/I18n/LanguageManager.php

-
message: "#^Method PhpMyAdmin\\\\I18n\\\\LanguageManager\\:\\:availableLocales\\(\\) should return array\\<string\\> but returns array\\|false\\.$#"
message: "#^Method PhpMyAdmin\\\\I18n\\\\LanguageManager\\:\\:availableLocales\\(\\) should return array\\<string\\> but returns array\\.$#"
count: 1
path: src/I18n/LanguageManager.php

-
message: "#^Parameter \\#1 \\$code of method PhpMyAdmin\\\\I18n\\\\LanguageManager\\:\\:getLanguage\\(\\) expects string, mixed given\\.$#"
count: 4
path: src/I18n/LanguageManager.php

-
message: "#^Parameter \\#1 \\$directory of function opendir expects string, mixed given\\.$#"
count: 1
Expand All @@ -7913,7 +7883,12 @@ parameters:
path: src/I18n/LanguageManager.php

-
message: "#^Property PhpMyAdmin\\\\I18n\\\\LanguageManager\\:\\:\\$availableLocales \\(array\\<string\\>\\) does not accept array\\|false\\.$#"
message: "#^Parameter \\#2 \\$path of function _bindtextdomain expects string, mixed given\\.$#"
count: 1
path: src/I18n/LanguageManager.php

-
message: "#^Property PhpMyAdmin\\\\I18n\\\\LanguageManager\\:\\:\\$availableLocales \\(array\\<string\\>\\) does not accept array\\.$#"
count: 1
path: src/I18n/LanguageManager.php

Expand Down Expand Up @@ -16670,14 +16645,6 @@ parameters:
count: 1
path: tests/unit/Html/MySQLDocumentationTest.php

-
message: """
#^Call to deprecated method getInstance\\(\\) of class PhpMyAdmin\\\\Config\\:
Use dependency injection instead\\.$#
"""
count: 6
path: tests/unit/I18n/LanguageTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with true will always evaluate to true\\.$#"
count: 1
Expand Down
48 changes: 12 additions & 36 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6156,41 +6156,25 @@
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$this->config->get('Lang')]]></code>
<code><![CDATA[$this->config->getCookie('pma_lang')]]></code>
</MixedArgument>
<DocblockTypeContradiction>
<code><![CDATA[$locales]]></code>
</DocblockTypeContradiction>
<MixedArrayOffset>
<code><![CDATA[$langs[$this->config->get('DefaultLang')]]]></code>
</MixedArrayOffset>
<MixedArrayTypeCoercion>
<code><![CDATA[$langs[$this->config->get('DefaultLang')]]]></code>
</MixedArrayTypeCoercion>
<MixedOperand>
<code><![CDATA[$this->config->get('FilterLanguages')]]></code>
</MixedOperand>
<PossiblyInvalidArgument>
<code><![CDATA[$_GET['lang']]]></code>
<code><![CDATA[$_POST['lang']]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$_GET['lang']]]></code>
<code><![CDATA[$_POST['lang']]]></code>
</PossiblyInvalidCast>
<PossiblyNullArgument>
<code><![CDATA[$this->config->get('Lang')]]></code>
<code><![CDATA[$this->config->getCookie('pma_lang')]]></code>
</PossiblyNullArgument>
<MixedAssignment>
<code><![CDATA[$languageFromConfig]]></code>
<code><![CDATA[$languageFromCookie]]></code>
</MixedAssignment>
<PossiblyNullArrayOffset>
<code><![CDATA[$langs]]></code>
</PossiblyNullArrayOffset>
<RiskyTruthyFalsyComparison>
<code><![CDATA[empty($_GET['lang'])]]></code>
<code><![CDATA[empty($_POST['lang'])]]></code>
<code><![CDATA[empty($this->config->get('FilterLanguages'))]]></code>
<code><![CDATA[empty($this->config->get('Lang'))]]></code>
<code><![CDATA[empty($this->config->getCookie('pma_lang'))]]></code>
</RiskyTruthyFalsyComparison>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$availableLocales !== false]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Image/ImageWrapper.php">
<PossiblyUnusedReturnValue>
Expand Down Expand Up @@ -13409,17 +13393,9 @@
<code><![CDATA[providerForTestGetRoute]]></code>
</PossiblyUnusedMethod>
</file>
<file src="tests/unit/I18n/LanguageTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<file src="tests/unit/I18n/LanguageManagerTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[listLocales]]></code>
<code><![CDATA[availableLocalesProvider]]></code>
<code><![CDATA[selectDataProvider]]></code>
</PossiblyUnusedMethod>
</file>
Expand Down
5 changes: 3 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,11 @@ public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumC
$this->setUserValue(null, 'lang', $GLOBALS['lang'], 'en');
}
} elseif (isset($configData['lang'])) {
$languageManager = LanguageManager::getInstance();
// read language from settings
$language = LanguageManager::getInstance()->getLanguage($configData['lang']);
$language = $languageManager->getLanguage($configData['lang']);
if ($language !== false) {
$language->activate();
$languageManager->activate($language);
$this->setCookie('pma_lang', $language->getCode());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/LanguageLoading.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ final class LanguageLoading implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$language = LanguageManager::getInstance()->selectLanguage();
$language->activate();
$languageManager = LanguageManager::getInstance();
$languageManager->activate($languageManager->selectLanguage());

return $handler->handle($request);
}
Expand Down
39 changes: 0 additions & 39 deletions src/I18n/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace PhpMyAdmin\I18n;

use function __;
use function _bindtextdomain;
use function _setlocale;
use function _textdomain;
use function addcslashes;
use function function_exists;
use function in_array;
use function preg_match;
use function setlocale;
use function str_contains;
use function str_replace;
use function strcmp;
Expand Down Expand Up @@ -146,37 +140,4 @@ public function isRTL(): bool
{
return in_array($this->code, ['ar', 'fa', 'he', 'ur'], true);
}

/**
* Activates given translation
*/
public function activate(): void
{
$GLOBALS['lang'] = $this->code;

// Set locale
_setlocale(0, $this->code);
_bindtextdomain('phpmyadmin', LOCALE_PATH);
_textdomain('phpmyadmin');
// Set PHP locale as well
if (function_exists('setlocale')) {
setlocale(0, $this->code);
}

LanguageManager::$textDirection = $this->isRTL() ? TextDirection::RightToLeft : TextDirection::LeftToRight;

/* TCPDF */
$GLOBALS['l'] = [];

/* TCPDF settings */
$GLOBALS['l']['a_meta_charset'] = 'UTF-8';
$GLOBALS['l']['a_meta_dir'] = LanguageManager::$textDirection->value;
$GLOBALS['l']['a_meta_language'] = $this->code;

/* TCPDF translations */
$GLOBALS['l']['w_page'] = __('Page number:');

/* Show possible warnings from language selection */
LanguageManager::getInstance()->showWarnings();
}
}
Loading

0 comments on commit e328ec4

Please sign in to comment.