diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d0948d1..3364094 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -22,7 +22,7 @@ parameters: - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 2 + count: 1 path: src/Loader.php - diff --git a/src/Cache/InMemoryCache.php b/src/Cache/InMemoryCache.php index 7db7d9f..5eea961 100644 --- a/src/Cache/InMemoryCache.php +++ b/src/Cache/InMemoryCache.php @@ -11,11 +11,10 @@ final class InMemoryCache implements CacheInterface, GetAllInterface { /** @var array */ - private array $cache; + private array $cache = []; public function __construct(MoParser $parser) { - $this->cache = []; $parser->parseIntoCache($this); } diff --git a/src/Loader.php b/src/Loader.php index 62d46fa..6a2935a 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -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; @@ -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; } } @@ -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); diff --git a/src/MoParser.php b/src/MoParser.php index f7529b5..7d9113b 100644 --- a/src/MoParser.php +++ b/src/MoParser.php @@ -7,7 +7,6 @@ use PhpMyAdmin\MoTranslator\Cache\CacheInterface; use function is_readable; -use function strcmp; final class MoParser { @@ -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; diff --git a/src/Translator.php b/src/Translator.php index 7bfbbc1..9e0879a 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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; @@ -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)); } @@ -195,7 +189,7 @@ public static function extractPluralCount(string $expr): int return 1; } - return intval($nplurals[1]); + return (int) $nplurals[1]; } /** @@ -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; } @@ -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]; } /** @@ -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; } @@ -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}")) { return $msgid; } @@ -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, )); } } diff --git a/tests/MoFilesTest.php b/tests/MoFilesTest.php index 91acee9..3706d2b 100644 --- a/tests/MoFilesTest.php +++ b/tests/MoFilesTest.php @@ -11,7 +11,7 @@ use function basename; use function glob; -use function strpos; +use function str_contains; /** * Test for MO files parsing. @@ -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';