Skip to content

Commit

Permalink
Merge pull request #47 from MauricioFauth/strict-types
Browse files Browse the repository at this point in the history
Refactor Loader::listLocales() to make it stricter
  • Loading branch information
MauricioFauth committed Jan 24, 2024
2 parents 39b6055 + e12a399 commit 29ab5aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 51 deletions.
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Expand Up @@ -15,11 +15,6 @@ parameters:
count: 1
path: src/Cache/ApcuCache.php

-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
count: 1
path: src/Loader.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
Expand All @@ -30,26 +25,6 @@ parameters:
count: 1
path: src/Loader.php

-
message: "#^Only booleans are allowed in an elseif condition, string\\|null given\\.$#"
count: 2
path: src/Loader.php

-
message: "#^Only booleans are allowed in an if condition, int\\|false given\\.$#"
count: 1
path: src/Loader.php

-
message: "#^Only booleans are allowed in an if condition, string given\\.$#"
count: 1
path: src/Loader.php

-
message: "#^Only booleans are allowed in an if condition, string\\|null given\\.$#"
count: 5
path: src/Loader.php

-
message: "#^Casting to int something that's already int\\.$#"
count: 1
Expand Down
11 changes: 1 addition & 10 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
<file src="src/Cache/ApcuCache.php">
<MixedAssignment>
<code>$cached</code>
Expand All @@ -18,15 +18,6 @@
<MixedReturnStatement>
<code><![CDATA[$GLOBALS['lang']]]></code>
</MixedReturnStatement>
<PossiblyNullArgument>
<code>$lang</code>
<code>$lang</code>
<code>$lang</code>
<code>$lang</code>
<code>$lang</code>
<code>$lang</code>
<code>$lang</code>
</PossiblyNullArgument>
</file>
<file src="src/StringReader.php">
<MixedAssignment>
Expand Down
33 changes: 17 additions & 16 deletions src/Loader.php
Expand Up @@ -105,12 +105,13 @@ public static function loadFunctions(): void
* @param string $locale Locale code
*
* @return string[] list of locales to try for any POSIX-style locale specification
* @psalm-return list<string>
*/
public static function listLocales(string $locale): array
{
$localeNames = [];

if ($locale) {
if ($locale !== '') {
if (
preg_match(
'/^(?P<lang>[a-z]{2,3})' // language code
Expand All @@ -119,44 +120,44 @@ public static function listLocales(string $locale): array
. '(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/', // @ modifier
$locale,
$matches,
)
) === 1
) {
$lang = $matches['lang'] ?? null;
$country = $matches['country'] ?? null;
$charset = $matches['charset'] ?? null;
$modifier = $matches['modifier'] ?? null;

if ($modifier) {
if ($country) {
if ($charset) {
$lang = $matches['lang'] ?? '';
$country = $matches['country'] ?? '';
$charset = $matches['charset'] ?? '';
$modifier = $matches['modifier'] ?? '';

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

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

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

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

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

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

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

0 comments on commit 29ab5aa

Please sign in to comment.