Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations & refactoring #46

Merged
merged 6 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ parameters:

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

-
Expand Down
3 changes: 1 addition & 2 deletions src/Cache/InMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
final class InMemoryCache implements CacheInterface, GetAllInterface
{
/** @var array<string, string> */
private array $cache;
private array $cache = [];

public function __construct(MoParser $parser)
{
$this->cache = [];
$parser->parseIntoCache($this);
}

Expand Down
52 changes: 12 additions & 40 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PhpMyAdmin\MoTranslator\Cache\CacheFactoryInterface;
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;

use function array_push;
use function file_exists;
use function getenv;
use function in_array;
Expand Down Expand Up @@ -130,56 +129,35 @@ public static function listLocales(string $locale): array
if ($modifier) {
if ($country) {
if ($charset) {
array_push(
$localeNames,
sprintf('%s_%s.%s@%s', $lang, $country, $charset, $modifier),
);
$localeNames[] = sprintf('%s_%s.%s@%s', $lang, $country, $charset, $modifier);
}

array_push(
$localeNames,
sprintf('%s_%s@%s', $lang, $country, $modifier),
);
$localeNames[] = sprintf('%s_%s@%s', $lang, $country, $modifier);
} elseif ($charset) {
array_push(
$localeNames,
sprintf('%s.%s@%s', $lang, $charset, $modifier),
);
$localeNames[] = sprintf('%s.%s@%s', $lang, $charset, $modifier);
}

array_push(
$localeNames,
sprintf('%s@%s', $lang, $modifier),
);
$localeNames[] = sprintf('%s@%s', $lang, $modifier);
}

if ($country) {
if ($charset) {
array_push(
$localeNames,
sprintf('%s_%s.%s', $lang, $country, $charset),
);
$localeNames[] = sprintf('%s_%s.%s', $lang, $country, $charset);
}

array_push(
$localeNames,
sprintf('%s_%s', $lang, $country),
);
$localeNames[] = sprintf('%s_%s', $lang, $country);
} elseif ($charset) {
array_push(
$localeNames,
sprintf('%s.%s', $lang, $charset),
);
$localeNames[] = sprintf('%s.%s', $lang, $charset);
}

if ($lang !== null) {
array_push($localeNames, $lang);
$localeNames[] = $lang;
}
}

// If the locale name doesn't match POSIX style, just include it as-is.
if (! in_array($locale, $localeNames)) {
array_push($localeNames, $locale);
$localeNames[] = $locale;
}
}

Expand All @@ -201,20 +179,14 @@ public static function setCacheFactory(CacheFactoryInterface|null $cacheFactory)
*/
public function getTranslator(string $domain = ''): Translator
{
if (empty($domain)) {
if ($domain === '') {
$domain = $this->defaultDomain;
}

if (! isset($this->domains[$this->locale])) {
$this->domains[$this->locale] = [];
}
$this->domains[$this->locale] ??= [];

if (! isset($this->domains[$this->locale][$domain])) {
if (isset($this->paths[$domain])) {
$base = $this->paths[$domain];
} else {
$base = './';
}
$base = $this->paths[$domain] ?? './';

$localeNames = self::listLocales($this->locale);

Expand Down
5 changes: 2 additions & 3 deletions src/MoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpMyAdmin\MoTranslator\Cache\CacheInterface;

use function is_readable;
use function strcmp;

final class MoParser
{
Expand Down Expand Up @@ -69,9 +68,9 @@ public function parseIntoCache(CacheInterface $cache): void

try {
$magic = $stream->read(0, 4);
if (strcmp($magic, self::MAGIC_LE) === 0) {
if ($magic === self::MAGIC_LE) {
$unpack = 'V';
} elseif (strcmp($magic, self::MAGIC_BE) === 0) {
} elseif ($magic === self::MAGIC_BE) {
$unpack = 'N';
} else {
$this->error = self::ERROR_BAD_MAGIC;
Expand Down
40 changes: 17 additions & 23 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Throwable;

use function chr;
use function array_key_exists;
use function count;
use function explode;
use function get_class;
use function implode;
use function intval;
use function is_numeric;
use function ltrim;
use function preg_replace;
use function rtrim;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function stripos;
use function strpos;
use function strtolower;
use function substr;
use function trim;
Expand Down Expand Up @@ -153,20 +151,16 @@ public static function sanitizePluralExpression(string $expr): string
{
// Parse equation
$expr = explode(';', $expr);
if (count($expr) >= 2) {
$expr = $expr[1];
} else {
$expr = $expr[0];
}
$expr = count($expr) >= 2 ? $expr[1] : $expr[0];

$expr = trim(strtolower($expr));
// Strip plural prefix
if (substr($expr, 0, 6) === 'plural') {
if (str_starts_with($expr, 'plural')) {
$expr = ltrim(substr($expr, 6));
}

// Strip equals
if (substr($expr, 0, 1) === '=') {
if (str_starts_with($expr, '=')) {
$expr = ltrim(substr($expr, 1));
}

Expand Down Expand Up @@ -195,7 +189,7 @@ public static function extractPluralCount(string $expr): int
return 1;
}

return intval($nplurals[1]);
return (int) $nplurals[1];
}

/**
Expand Down Expand Up @@ -281,7 +275,7 @@ private function selectString(int $n): int
public function ngettext(string $msgid, string $msgidPlural, int $number): string
{
// this should contains all strings separated by NULLs
$key = implode(chr(0), [$msgid, $msgidPlural]);
$key = $msgid . "\u{0}" . $msgidPlural;
if (! $this->cache->has($key)) {
return $number !== 1 ? $msgidPlural : $msgid;
}
Expand All @@ -291,13 +285,13 @@ public function ngettext(string $msgid, string $msgidPlural, int $number): strin
// find out the appropriate form
$select = $this->selectString($number);

$list = explode(chr(0), $result);
$list = explode("\u{0}", $result);

if (! isset($list[$select])) {
return $list[0];
if (array_key_exists($select, $list)) {
return $list[$select];
}

return $list[$select];
return $list[0];
}

/**
Expand All @@ -310,9 +304,9 @@ public function ngettext(string $msgid, string $msgidPlural, int $number): strin
*/
public function pgettext(string $msgctxt, string $msgid): string
{
$key = implode(chr(4), [$msgctxt, $msgid]);
$key = $msgctxt . "\u{4}" . $msgid;
$ret = $this->gettext($key);
if (strpos($ret, chr(4)) !== false) {
if ($ret === $key) {
return $msgid;
}

Expand All @@ -331,9 +325,9 @@ public function pgettext(string $msgctxt, string $msgid): string
*/
public function npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
$key = implode(chr(4), [$msgctxt, $msgid]);
$key = $msgctxt . "\u{4}" . $msgid;
$ret = $this->ngettext($key, $msgidPlural, $number);
if (strpos($ret, chr(4)) !== false) {
if (str_contains($ret, "\u{4}")) {
williamdes marked this conversation as resolved.
Show resolved Hide resolved
return $msgid;
}

Expand Down Expand Up @@ -374,7 +368,7 @@ public function getTranslations(): array

throw new CacheException(sprintf(
"Cache '%s' does not support getting translations",
get_class($this->cache),
$this->cache::class,
));
}
}
6 changes: 3 additions & 3 deletions tests/MoFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use function basename;
use function glob;
use function strpos;
use function str_contains;

/**
* Test for MO files parsing.
Expand All @@ -38,10 +38,10 @@ public function testMoFilePlurals(string $filename): void
{
$parser = $this->getTranslator($filename);
$expected2 = '%d sekundy';
if (strpos($filename, 'invalid-formula.mo') !== false || strpos($filename, 'lessplurals.mo') !== false) {
if (str_contains($filename, 'invalid-formula.mo') || str_contains($filename, 'lessplurals.mo')) {
$expected0 = '%d sekunda';
$expected2 = '%d sekunda';
} elseif (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) {
} elseif (str_contains($filename, 'plurals.mo') || str_contains($filename, 'noheader.mo')) {
$expected0 = '%d sekundy';
} else {
$expected0 = '%d sekund';
Expand Down