From 2c69095a1edf7474073451285238659d0f62ce3f Mon Sep 17 00:00:00 2001 From: Appleman1234 Date: Sun, 25 Sep 2022 13:30:13 +1000 Subject: PHP8.1 fixes from Rector --- tools/inc/cli.php | 7 +------ tools/inc/lib/jsgettext/JSParser.php | 31 +++++++++++-------------------- tools/inc/lib/jsgettext/PoeditParser.php | 25 ++++++++++++------------- tools/inc/lib/jsgettext/PoeditString.php | 16 +++++----------- tools/inc/lib/jsgettext/jsgettext.php | 18 +++++++----------- tools/inc/lib/jsgettext/po2json.php | 16 ++++++---------- 6 files changed, 42 insertions(+), 71 deletions(-) (limited to 'tools/inc') diff --git a/tools/inc/cli.php b/tools/inc/cli.php index b04d042a..4b8e3793 100644 --- a/tools/inc/cli.php +++ b/tools/inc/cli.php @@ -40,11 +40,6 @@ putenv('TINYBOARD_PATH=' . getcwd()); require 'inc/bootstrap.php'; -$mod = Array( - 'id' => -1, - 'type' => ADMIN, - 'username' => '?', - 'boards' => Array('*') -); +$mod = ['id' => -1, 'type' => ADMIN, 'username' => '?', 'boards' => ['*']]; 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("#<>#", function ($match) use($regs) { - return $regs[$match[1]]; - }, $input); + return preg_replace_callback("#<>#", fn($match) => $regs[$match[1]], $input); } protected function importStrings($input) { $strings = $this->strings; - return preg_replace_callback("#<>#", function ($match) use($strings) { - return $strings[$match[1]]; - }, $input); + return preg_replace_callback("#<>#", fn($match) => $strings[$match[1]], $input); } public function __construct($file, $keywords = '_') { @@ -46,15 +42,12 @@ class JSParser { // extract reg exps $output = preg_replace_callback( '# ( / (?: (?>[^/\\\\]++) | \\\\\\\\ | (?_extractRegs(...), $output ); // extract strings $output = preg_replace_callback( - array( - '# " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?[^'\\\\]++) | \\\\\\\\ | (?[^"\\\\]++) | \\\\\\\\ | (?[^'\\\\]++) | \\\\\\\\ | (?_extractStrings(...), $output ); // delete line comments @@ -64,13 +57,11 @@ class JSParser { $output = preg_replace('#/\*(.*?)\*/#is', '', $output); $strings = $this->strings; - $output = preg_replace_callback("#<>#", function($match) use($strings) { - return $strings[$match[1]]; - }, $output); + $output = preg_replace_callback("#<>#", 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) \(\\ *' ( (?: (?>[^'\\\\]++) | \\\\\\\\ | (?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)\ " ( (?: (?>[^"\\\\]++) | \\\\\\\\ | (?[^"\\\\]++) | \\\\\\\\ | (?_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 @@ 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"; -- cgit v1.3