Skip to content

Commit

Permalink
[*]: use "@psalm-pure"
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Dec 19, 2019
1 parent 1551979 commit ffc97ed
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
@@ -1,8 +1,12 @@
# Changelog

### 1.4.4 (2019-12-19)

- use "@psalm-pure"

### 1.4.3 (2019-12-19)

- use "@psalm-immutable" and "@psalm-pure"
- use "@psalm-immutable"

### 1.4.2 (2019-12-13)

Expand Down
93 changes: 81 additions & 12 deletions src/voku/helper/ASCII.php
Expand Up @@ -219,6 +219,11 @@ public static function charsArray(bool $replace_extra_symbols = false): array
*/
public static function charsArrayWithMultiLanguageValues(bool $replace_extra_symbols = false): array
{
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<string,array>
*/
static $CHARS_ARRAY = [];
$cacheKey = '' . $replace_extra_symbols;

Expand Down Expand Up @@ -264,6 +269,7 @@ public static function charsArrayWithMultiLanguageValues(bool $replace_extra_sym
* @param bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "拢" with " pound ".</p>
* @param bool $asOrigReplaceArray [optional] <p>TRUE === return thr {orig: string[], replace: string[]}
* array</p>
* @psalm-pure
*
* @return array{orig: string[], replace: string[]}|array<string, string>
* <p>An array of replacements.</p>
Expand All @@ -276,6 +282,11 @@ public static function charsArrayWithOneLanguage(
$language = self::get_language($language);

// init
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<string,array>
*/
static $CHARS_ARRAY = [];
$cacheKey = '' . $replace_extra_symbols . '-' . $asOrigReplaceArray;

Expand Down Expand Up @@ -345,6 +356,7 @@ public static function charsArrayWithOneLanguage(
* @param bool $replace_extra_symbols [optional] <p>Add some more replacements e.g. "拢" with " pound ".</p>
* @param bool $asOrigReplaceArray [optional] <p>TRUE === return thr {orig: string[], replace: string[]}
* array</p>
* @psalm-pure
*
* @return array{orig: string[], replace: string[]}|array<string, string>
* <p>An array of replacements.</p>
Expand All @@ -354,6 +366,11 @@ public static function charsArrayWithSingleLanguageValues(
bool $asOrigReplaceArray = true
): array {
// init
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<string,array>
*/
static $CHARS_ARRAY = [];
$cacheKey = '' . $replace_extra_symbols . '-' . $asOrigReplaceArray;

Expand Down Expand Up @@ -406,6 +423,8 @@ public static function charsArrayWithSingleLanguageValues(
* @param bool $remove_invisible_characters [optional] <p>Set to false, if you not want to remove invisible
* characters e.g.: "\0"</p>
*
* @psalm-pure
*
* @return string
* <p>A clean UTF-8 string.</p>
*/
Expand Down Expand Up @@ -476,6 +495,8 @@ public static function is_ascii(string $str): bool
*
* @param string $str <p>The string to be normalized.</p>
*
* @psalm-pure
*
* @return string
* <p>A string with normalized characters for commonly used chars in Word documents.</p>
*/
Expand All @@ -486,9 +507,14 @@ public static function normalize_msword(string $str): string
}

// init
static $MSWORD_CACHE = [];

if (!isset($MSWORD_CACHE['orig'])) {
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array{orig: string[], replace: string[]}
*/
static $MSWORD_CACHE = ['orig' => [], 'replace' => []];

if (empty($MSWORD_CACHE['orig'])) {
self::prepareAsciiMaps();

/**
Expand All @@ -515,6 +541,8 @@ public static function normalize_msword(string $str): string
* @param bool $keepBidiUnicodeControls [optional] <p>Set to true, to keep non-printable (for the web)
* bidirectional text chars.</p>
*
* @psalm-pure
*
* @return string
* <p>A string with normalized whitespace.</p>
*/
Expand All @@ -527,6 +555,11 @@ public static function normalize_whitespace(
return '';
}

/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<int,array<string,string>>
*/
static $WHITESPACE_CACHE = [];
$cacheKey = (int) $keepNonBreakingSpace;

Expand All @@ -543,10 +576,15 @@ public static function normalize_whitespace(
}

if ($keepBidiUnicodeControls === false) {
/**
* @var array<int,string>|null
*
* @psalm-suppress ImpureStaticVariable
*/
static $BIDI_UNICODE_CONTROLS_CACHE = null;

if ($BIDI_UNICODE_CONTROLS_CACHE === null) {
$BIDI_UNICODE_CONTROLS_CACHE = \array_values(self::$BIDI_UNI_CODE_CONTROLS_TABLE);
$BIDI_UNICODE_CONTROLS_CACHE = self::$BIDI_UNI_CODE_CONTROLS_TABLE;
}

$str = \str_replace($BIDI_UNICODE_CONTROLS_CACHE, '', $str);
Expand Down Expand Up @@ -615,6 +653,8 @@ public static function remove_invisible_characters(
* at the same time. | NULL === auto-setting, depended on the
* language</p>
*
* @psalm-pure
*
* @return string
* <p>A string that contains only ASCII characters.</p>
*/
Expand Down Expand Up @@ -655,6 +695,11 @@ public static function to_ascii(
$language_all_chars = self::charsArrayWithSingleLanguageValues($replace_extra_symbols);
$str = \str_replace($language_all_chars['orig'], $language_all_chars['replace'], $str);
} else {
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<string,array{orig: string[], replace: string[]}>
*/
static $REPLACE_HELPER_CACHE = [];
$cacheKey = $language . '-' . $replace_single_chars_only . '-' . $replace_extra_symbols;

Expand All @@ -665,17 +710,17 @@ public static function to_ascii(

$langAll = self::charsArrayWithSingleLanguageValues($replace_extra_symbols);
if (!empty($langAll)) {
assert(is_array($langAll['orig']));
assert(is_array($langAll['replace']));
\assert(\is_array($langAll['orig']));
\assert(\is_array($langAll['replace']));

$helperTmp['orig'][] = $langAll['orig'];
$helperTmp['replace'][] = $langAll['replace'];
}

$langSpecific = self::charsArrayWithOneLanguage($language, $replace_extra_symbols);
if (!empty($langSpecific)) {
assert(is_array($langSpecific['orig']));
assert(is_array($langSpecific['replace']));
\assert(\is_array($langSpecific['orig']));
\assert(\is_array($langSpecific['replace']));

$helperTmp['orig'][] = $langSpecific['orig'];
$helperTmp['replace'][] = $langSpecific['replace'];
Expand All @@ -688,6 +733,8 @@ public static function to_ascii(
$helperTmp['orig'],
$helperTmp['replace']
);

\assert(\is_array($REPLACE_HELPER_CACHE[$cacheKey]));
}

$charDone = [];
Expand Down Expand Up @@ -730,6 +777,8 @@ public static function to_ascii(
* simply replaced with hyphen otherwise.</p>
* @param string $fallback_char
*
* @psalm-pure
*
* @return string
* <p>A string that contains only safe characters for a filename.</p>
*/
Expand Down Expand Up @@ -779,6 +828,7 @@ public static function to_filename(
* @param bool $use_str_to_lower [optional] <p>Use "string to lower" for the input.</p>
* @param bool $use_transliterate [optional] <p>Use ASCII::to_transliterate() for unknown
* chars.</p>
* @psalm-pure
*
* @return string
* <p>A string that has been converted to an URL slug.</p>
Expand Down Expand Up @@ -846,6 +896,8 @@ public static function to_slugify(
* But you can also use NULL to keep the unknown chars.</p>
* @param bool $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl
*
* @psalm-pure
*
* @return string
* <p>A String that contains only ASCII characters.</p>
*/
Expand All @@ -854,16 +906,33 @@ public static function to_transliterate(
$unknown = '?',
bool $strict = false
): string {
/**
* @psalm-suppress ImpureStaticVariable
*
* @var array<int,string>|null
*/
static $UTF8_TO_TRANSLIT = null;

/**
* @psalm-suppress ImpureStaticVariable
*
* null|\Transliterator
*/
static $TRANSLITERATOR = null;
static $SUPPORT = [];

/**
* @psalm-suppress ImpureStaticVariable
*
* @var bool|null
*/
static $SUPPORT_INTL = null;

if ($str === '') {
return '';
}

if (!isset($SUPPORT['intl'])) {
$SUPPORT['intl'] = \extension_loaded('intl');
if ($SUPPORT_INTL === null) {
$SUPPORT_INTL = \extension_loaded('intl');
}

// check if we only have ASCII, first (better performance)
Expand All @@ -886,7 +955,7 @@ public static function to_transliterate(
if (
$strict === true
&&
$SUPPORT['intl'] === true
$SUPPORT_INTL === true
) {
if (!isset($TRANSLITERATOR)) {
// INFO: see "*-Latin" rules via "transliterator_list_ids()"
Expand Down

0 comments on commit ffc97ed

Please sign in to comment.