diff options
| author | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
|---|---|---|
| committer | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
| commit | 2c69095a1edf7474073451285238659d0f62ce3f (patch) | |
| tree | 5dee476605402c66dc2853d0836b29f6aedae62b /tools/inc/lib/jsgettext | |
| parent | e9a61e2fbbd930eaa80d6a5f33420cedc59d748c (diff) | |
PHP8.1 fixes from Rector
Diffstat (limited to 'tools/inc/lib/jsgettext')
| -rw-r--r-- | tools/inc/lib/jsgettext/JSParser.php | 31 | ||||
| -rw-r--r-- | tools/inc/lib/jsgettext/PoeditParser.php | 25 | ||||
| -rw-r--r-- | tools/inc/lib/jsgettext/PoeditString.php | 16 | ||||
| -rw-r--r-- | tools/inc/lib/jsgettext/jsgettext.php | 18 | ||||
| -rw-r--r-- | tools/inc/lib/jsgettext/po2json.php | 16 |
5 files changed, 41 insertions, 65 deletions
diff --git a/tools/inc/lib/jsgettext/JSParser.php b/tools/inc/lib/jsgettext/JSParser.php index adba59bc..739d1195 100644 --- a/tools/inc/lib/jsgettext/JSParser.php +++ b/tools/inc/lib/jsgettext/JSParser.php @@ -4,9 +4,9 @@ class JSParser { protected $content; protected $keywords; - protected $regs = array(); + protected $regs = []; protected $regsCounter = 0; - protected $strings = array(); + protected $strings = []; protected $stringsCounter = 0; protected function _extractRegs($match) { @@ -23,16 +23,12 @@ class JSParser { } protected function importRegExps($input) { $regs = $this->regs; - return preg_replace_callback("#<<reg(\d+)>>#", function ($match) use($regs) { - return $regs[$match[1]]; - }, $input); + return preg_replace_callback("#<<reg(\d+)>>#", fn($match) => $regs[$match[1]], $input); } protected function importStrings($input) { $strings = $this->strings; - return preg_replace_callback("#<<s(\d+)>>#", function ($match) use($strings) { - return $strings[$match[1]]; - }, $input); + return preg_replace_callback("#<<s(\d+)>>#", fn($match) => $strings[$match[1]], $input); } public function __construct($file, $keywords = '_') { @@ -46,15 +42,12 @@ class JSParser { // extract reg exps $output = preg_replace_callback( '# ( / (?: (?>[^/\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\/ )+ (?<!\\\\)/ ) [a-z]* \b #ix', - array($this, '_extractRegs'), $output + $this->_extractRegs(...), $output ); // extract strings $output = preg_replace_callback( - array( - '# " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" #ix', - "# ' ( (?: (?>[^'\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\' )* ) (?<!\\\\)' #ix" - ), array($this, '_extractStrings'), $output + ['# " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" #ix', "# ' ( (?: (?>[^'\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\' )* ) (?<!\\\\)' #ix"], $this->_extractStrings(...), $output ); // delete line comments @@ -64,13 +57,11 @@ class JSParser { $output = preg_replace('#/\*(.*?)\*/#is', '', $output); $strings = $this->strings; - $output = preg_replace_callback("#<<s(\d+)>>#", function($match) use($strings) { - return $strings[$match[1]]; - }, $output); + $output = preg_replace_callback("#<<s(\d+)>>#", fn($match) => $strings[$match[1]], $output); $keywords = implode('|', $this->keywords); - $strings = array(); + $strings = []; // extract func calls preg_match_all( @@ -78,15 +69,15 @@ class JSParser { $output, $matches, PREG_SET_ORDER ); - foreach ($matches as $m) $strings[] = stripslashes($m[1]); + foreach ($matches as $m) $strings[] = stripslashes((string) $m[1]); - $matches = array(); + $matches = []; preg_match_all( "# (?:$keywords) \(\\ *' ( (?: (?>[^'\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\' )* ) (?<!\\\\)'\\ *\) #ix", $output, $matches, PREG_SET_ORDER ); - foreach ($matches as $m) $strings[] = stripslashes($m[1]); + foreach ($matches as $m) $strings[] = stripslashes((string) $m[1]); return $strings; } diff --git a/tools/inc/lib/jsgettext/PoeditParser.php b/tools/inc/lib/jsgettext/PoeditParser.php index eebd816e..0fbe04e6 100644 --- a/tools/inc/lib/jsgettext/PoeditParser.php +++ b/tools/inc/lib/jsgettext/PoeditParser.php @@ -4,17 +4,16 @@ require_once 'PoeditString.php'; class PoeditParser { - protected $file; protected $header = ''; - protected $strings = array(); + protected $strings = []; protected function _fixQuotes($str) { - return stripslashes($str); + return stripslashes((string) $str); } - public function __construct($file) { - $this->file = $file; - } + public function __construct(protected $file) + { + } public function parse() { $contents = file_get_contents($this->file); @@ -26,13 +25,13 @@ class PoeditParser { foreach ($parts as $part) { // parse comments - $comments = array(); - preg_match_all('#^\\#: (.*?)$#m', $part, $matches, PREG_SET_ORDER); + $comments = []; + preg_match_all('#^\\#: (.*?)$#m', (string) $part, $matches, PREG_SET_ORDER); foreach ($matches as $m) $comments[] = $m[1]; - $isFuzzy = preg_match('#^\\#, fuzzy$#im', $part) ? true : false; + $isFuzzy = preg_match('#^\\#, fuzzy$#im', (string) $part) ? true : false; - preg_match_all('# ^ (msgid|msgstr)\ " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" $ #ixm', $part, $matches2, PREG_SET_ORDER); + preg_match_all('# ^ (msgid|msgstr)\ " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?<!\\\\)\\\\(?!\\\\) | \\\\" )* ) (?<!\\\\)" $ #ixm', (string) $part, $matches2, PREG_SET_ORDER); $k = $this->_fixQuotes($matches2[0][2]); $v = !empty($matches2[1][2]) ? $this->_fixQuotes($matches2[1][2]) : ''; @@ -58,11 +57,11 @@ class PoeditParser { } public function getJSON() { - $str = array(); + $str = []; foreach ($this->strings as $s) { if ($s->value) $str[$s->key] = $s->value; } - return json_encode($str); + return json_encode($str, JSON_THROW_ON_ERROR); } public function toJSON($outputFilename, $varName = 'l10n') { @@ -75,7 +74,7 @@ class PoeditParser { foreach ($this->strings as $str) { $data .= $str; } - return file_put_contents($filename ? $filename : $this->file, $data) !== false; + return file_put_contents($filename ?: $this->file, $data) !== false; } } diff --git a/tools/inc/lib/jsgettext/PoeditString.php b/tools/inc/lib/jsgettext/PoeditString.php index 3d59d004..5bffeea0 100644 --- a/tools/inc/lib/jsgettext/PoeditString.php +++ b/tools/inc/lib/jsgettext/PoeditString.php @@ -1,26 +1,20 @@ <?php -class PoeditString { - public $key; - public $value; - public $fuzzy; +class PoeditString implements \Stringable { public $comments; - function __construct($key, $value = '', $fuzzy = false, $comments = array()) { - $this->key = $key; - $this->value = $value; - $this->fuzzy = $fuzzy; + function __construct(public $key, public $value = '', public $fuzzy = false, $comments = []) { $this->comments = (array)$comments; } - public function __toString() { + public function __toString(): string { $str =''; foreach ($this->comments as $c) { $str .= "#: $c\n"; } if ($this->fuzzy) $str .= "#, fuzzy\n"; - $str .= 'msgid "'.str_replace('"', '\\"', $this->key).'"' . "\n"; - $str .= 'msgstr "'.str_replace('"', '\\"', $this->value).'"' . "\n"; + $str .= 'msgid "'.str_replace('"', '\\"', (string) $this->key).'"' . "\n"; + $str .= 'msgstr "'.str_replace('"', '\\"', (string) $this->value).'"' . "\n"; $str .= "\n"; return $str; } diff --git a/tools/inc/lib/jsgettext/jsgettext.php b/tools/inc/lib/jsgettext/jsgettext.php index 6c70f0b4..75ea6566 100644 --- a/tools/inc/lib/jsgettext/jsgettext.php +++ b/tools/inc/lib/jsgettext/jsgettext.php @@ -4,16 +4,12 @@ require_once 'PoeditParser.php'; function buildOptions($args) { - $options = array( - 'files' => array(), - '-o' => null, - '-k' => '_' - ); - $len = count($args); + $options = ['files' => [], '-o' => null, '-k' => '_']; + $len = is_countable($args) ? count($args) : 0; $i = 1; while ($i < $len) { - if (preg_match('#^-[a-z]$#i', $args[$i])) { - $options[$args[$i]] = isset($args[$i+1]) ? trim($args[$i+1]) : true; + if (preg_match('#^-[a-z]$#i', (string) $args[$i])) { + $options[$args[$i]] = isset($args[$i+1]) ? trim((string) $args[$i+1]) : true; $i += 2; } else { @@ -43,14 +39,14 @@ $poeditParser = new PoeditParser($options['-o']); $poeditParser->parse(); - $errors = array(); + $errors = []; foreach ($inputFiles as $f) { - if (!is_readable($f) || !preg_match('#\.js$#', $f)) { + if (!is_readable($f) || !preg_match('#\.js$#', (string) $f)) { $errors[] = ("$f is not a valid javascript file."); continue; } - $jsparser = new JSParser($f, explode(' ', $options['-k'])); + $jsparser = new JSParser($f, explode(' ', (string) $options['-k'])); $jsStrings = $jsparser->parse(); $poeditParser->merge($jsStrings); } diff --git a/tools/inc/lib/jsgettext/po2json.php b/tools/inc/lib/jsgettext/po2json.php index 847b0899..8fb6f69f 100644 --- a/tools/inc/lib/jsgettext/po2json.php +++ b/tools/inc/lib/jsgettext/po2json.php @@ -3,16 +3,12 @@ require_once 'PoeditParser.php'; function buildOptions($args) { - $options = array( - '-o' => null, - '-i' => null, - '-n' => 'l10n' - ); - $len = count($args); + $options = ['-o' => null, '-i' => null, '-n' => 'l10n']; + $len = is_countable($args) ? count($args) : 0; $i = 0; while ($i < $len) { - if (preg_match('#^-[a-z]$#i', $args[$i])) { - $options[$args[$i]] = isset($args[$i+1]) ? trim($args[$i+1]) : true; + if (preg_match('#^-[a-z]$#i', (string) $args[$i])) { + $options[$args[$i]] = isset($args[$i+1]) ? trim((string) $args[$i+1]) : true; $i += 2; } else { @@ -33,8 +29,8 @@ $poeditParser->parse(); if ($poeditParser->toJSON($options['-o'], $options['-n'])) { - $strings = count($poeditParser->getStrings()); - echo "Successfully exported " . count($strings) . " strings.\n"; + $strings = is_countable($poeditParser->getStrings()) ? count($poeditParser->getStrings()) : 0; + echo "Successfully exported " . (is_countable($strings) ? count($strings) : 0) . " strings.\n"; } else { echo "Cannor write to file '{$options['-o']}'.\n"; |
