diff options
| author | 2022-01-01 13:54:36 +0100 | |
|---|---|---|
| committer | 2022-01-01 13:54:36 +0100 | |
| commit | a791388ce4b7b5c03be109418336d41992d93b88 (patch) | |
| tree | e4d0b697887203c1cbd2f19345f52c711354c299 | |
| parent | 3f6aa42b817145a3b00f4d615f87728b55c4413a (diff) | |
Avoid func_get_args (#4108)
Use variadic syntax instead https://php.net/manual/functions.arguments#functions.variable-arg-list
And avoid dynamic functions names when possible to more easily identify calls and unused functions.
Contributes to https://github.com/FreshRSS/FreshRSS/issues/4103
| -rw-r--r-- | lib/Minz/ExtensionManager.php | 18 | ||||
| -rw-r--r-- | lib/Minz/Translate.php | 17 | ||||
| -rw-r--r-- | lib/Minz/Url.php | 14 | ||||
| -rw-r--r-- | lib/lib_rss.php | 5 |
4 files changed, 27 insertions, 27 deletions
diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php index 0139c326f..576064b35 100644 --- a/lib/Minz/ExtensionManager.php +++ b/lib/Minz/ExtensionManager.php @@ -295,23 +295,25 @@ class Minz_ExtensionManager { * array keys. * * @param string $hook_name the hook to call. - * @param additional parameters (for signature, please see self::$hook_list). + * @param array<mixed> $args additional parameters (for signature, please see self::$hook_list). * @return mixed final result of the called hook. */ - public static function callHook($hook_name) { + public static function callHook($hook_name, ...$args) { if (!isset(self::$hook_list[$hook_name])) { return; } $signature = self::$hook_list[$hook_name]['signature']; - $args = func_get_args(); - if ($signature === 'PassArguments') { - array_shift($args); + if ($signature === 'OneToOne') { + return self::callOneToOne($hook_name, $args[0] ?? null); + } elseif ($signature === 'PassArguments') { foreach (self::$hook_list[$hook_name]['list'] as $function) { - call_user_func_array($function, $args); + call_user_func($function, ...$args); } - } else { - return call_user_func_array('self::call' . $signature, $args); + } elseif ($signature === 'NoneToString') { + return self::callNoneToString($hook_name); + } elseif ($signature === 'NoneToNone') { + return self::callNoneToNone($hook_name); } } diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index 90e678d58..0659b0de2 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -179,11 +179,11 @@ class Minz_Translate { /** * Translate a key into its corresponding value based on selected language. * @param string $key the key to translate. - * @param additional parameters for variable keys. + * @param string $args additional parameters for variable keys. * @return string value corresponding to the key. * If no value is found, return the key itself. */ - public static function t($key) { + public static function t($key, ...$args) { $group = explode('.', $key); if (count($group) < 2) { @@ -232,9 +232,6 @@ class Minz_Translate { } // Get the facultative arguments to replace i18n variables. - $args = func_get_args(); - unset($args[0]); - return vsprintf($translation_value, $args); } @@ -249,11 +246,9 @@ class Minz_Translate { /** * Alias for Minz_Translate::t() + * @param string $key + * @param array<string> $args */ -function _t($key) { - $args = func_get_args(); - unset($args[0]); - array_unshift($args, $key); - - return call_user_func_array('Minz_Translate::t', $args); +function _t($key, ...$args) { + return Minz_Translate::t($key, ...$args); } diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php index a1019df50..59afff557 100644 --- a/lib/Minz/Url.php +++ b/lib/Minz/Url.php @@ -118,16 +118,20 @@ class Minz_Url { } } -function _url ($controller, $action) { - $nb_args = func_num_args (); +/** + * @param string $controller + * @param string $action + * @param array<string,string> $args + */ +function _url ($controller, $action, ...$args) { + $nb_args = count($args); - if($nb_args < 2 || $nb_args % 2 != 0) { + if ($nb_args % 2 !== 0) { return false; } - $args = func_get_args (); $params = array (); - for($i = 2; $i < $nb_args; $i = $i + 2) { + for ($i = 0; $i < $nb_args; $i += 2) { $params[$args[$i]] = $args[$i + 1]; } diff --git a/lib/lib_rss.php b/lib/lib_rss.php index 93463bace..2627773b6 100644 --- a/lib/lib_rss.php +++ b/lib/lib_rss.php @@ -18,11 +18,10 @@ if (COPY_SYSLOG_TO_STDERR) { /** * Build a directory path by concatenating a list of directory names. * - * @param $path_parts a list of directory names + * @param array<string> $path_parts a list of directory names * @return string corresponding to the final pathname */ -function join_path() { - $path_parts = func_get_args(); +function join_path(...$path_parts) { return join(DIRECTORY_SEPARATOR, $path_parts); } |
