summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2021-02-03 10:30:22 +0100
committerChristian Schneider2021-02-03 10:30:22 +0100
commit21ccdefc4aea4c2439f7d41ece7d8216d8ba8b40 (patch)
tree3cb6969d9e34580fb0b2eaa64016850e048afb56
parent039792e13581ebfd0ddc05bb115bc5f6bfa9cf46 (diff)
downloaditools-21ccdefc4aea4c2439f7d41ece7d8216d8ba8b40.tar.gz
itools-21ccdefc4aea4c2439f7d41ece7d8216d8ba8b40.tar.bz2
itools-21ccdefc4aea4c2439f7d41ece7d8216d8ba8b40.zip
Code cleanup: Switch to new style varargs
-rw-r--r--auto_prepend.php19
-rw-r--r--it.class33
-rw-r--r--it_dbi.class18
-rw-r--r--it_html.class9
4 files changed, 29 insertions, 50 deletions
diff --git a/auto_prepend.php b/auto_prepend.php
index 0aa70ef..f05a4c2 100644
--- a/auto_prepend.php
+++ b/auto_prepend.php
@@ -5,18 +5,16 @@
/**
* Return string containing names and values of all arguments
*/
-function D()
+function D(...$args)
{
- $args = func_get_args();
return it_debug::dump($args);
}
/**
* Echo string containing names and values of all arguments
*/
-function ED()
+function ED(...$args)
{
- $args = func_get_args();
if (ob_get_level() == 0 && $_SERVER['REMOTE_ADDR'])
ob_start(); # prevent later 'headers already sent' error
$GLOBALS['debug_noredir'] = 1;
@@ -28,15 +26,12 @@ function ED()
* Same as ED(), but if first argument is foo then $GLOBALS['debug_foo'] must be set for output
* @return boolean indicating whether $GLOBALS['debug_foo'] was set
*/
-function EDC($var)
+function EDC($var, ...$args)
{
$GLOBALS['ULTRADEBUGVARS'][$var] = 1;
if (($result = $GLOBALS["debug_$var"]))
{
- $args = func_get_args();
- array_shift($args); # remove $var
-
if ($args)
{
if (ob_get_level() == 0 && $_SERVER['REMOTE_ADDR'])
@@ -54,9 +49,8 @@ function EDC($var)
/**
* Echo string containing names and values of all arguments, then exit
*/
-function EDX()
+function EDX(...$args)
{
- $args = func_get_args();
if ($_SERVER['REMOTE_ADDR'] && !headers_sent())
header("Content-Type: text/html"); # Not going to be e.g. a valid gif anyway
@@ -155,10 +149,9 @@ function T_exists($label, $language = null)
/**
* Build an url
*/
-function U(/* ... */)
+function U(...$args)
{
- $args = func_get_args();
- return call_user_func_array(array(is_a($o = $GLOBALS['it_html'], 'it_html') ? $o : 'it_html', 'U'), $args);
+ return is_a($obj = $GLOBALS['it_html'], 'it_html') ? $obj->U(...$args) : it_html::U(...$args);
}
diff --git a/it.class b/it.class
index b5343cd..dd62dd7 100644
--- a/it.class
+++ b/it.class
@@ -72,12 +72,11 @@ static function &cloneobj(&$object)
/**
* Append all arguments to a logfile (tab separated). Date will be added to filename and line
* @param $name Name of logfile. Will be in log/ of service unless it starts with /
- * @param $args... Varargs to log, will be tab separated.
+ * @param $args Varargs to log, will be tab separated.
*/
-static function log($name /* ... */)
+static function log($name, ...$args)
{
- $args = func_get_args();
- $line = date("Y-m-d H:i:s") . "\t" . implode("\t", array_slice($args, 1));
+ $line = date("Y-m-d H:i:s") . "\t" . implode("\t", $args);
self::log_line($name, $line);
return $line;
}
@@ -627,13 +626,12 @@ static function filter_keys($array, $keys, $p = array())
* {0} .. {n} quotes and inserts positional arguments
* {-opts} array of opts => {value,true,false,null}: it::exec('ls {-opts}', ['-opts' => ["-l" => true]]);
* @param $cmd Format string with {keywords} a la ET()
- * @param $values varargs, contains key => val arrays for filling in cmd line. val=null expands to nothing
+ * @param $args varargs, contains key => val arrays for filling in cmd line. val=null expands to nothing
* @return output of command. shell errors not detectable, consider it::system or see /www/server/log/error_log
*/
-static function exec(/* $cmd, $values1 = array(), ... */)
+static function exec($cmd, ...$args)
{
- $args = func_get_args();
- $cmd = call_user_func_array('it::shell_command', $args);
+ $cmd = it::shell_command($cmd, ...$args);
$before = gettimeofday(true);
$result = EDC('noexec') ? "" : (string)shell_exec("set +o posix\n" . $cmd);
@@ -649,10 +647,9 @@ static function exec(/* $cmd, $values1 = array(), ... */)
* See ::exec above for usage
* @return exit code of last command: zero for success, nonzero for failure
*/
-static function system(/* $cmd, $values1 = array(), ... */)
+static function system($cmd, ...$args)
{
- $args = func_get_args();
- $cmd = call_user_func_array('it::shell_command', $args);
+ $cmd = it::shell_command($cmd, ...$args);
$before = gettimeofday(true);
if (!EDC('noexec'))
@@ -670,13 +667,11 @@ static function system(/* $cmd, $values1 = array(), ... */)
* Keywords {keyword} are replace a la ET(),
* {-opts} array of opts => {value,true,false,null}: it::exec('ls {-opts}', ['-opts' => ["-l" => true]]);
* @param $cmd Format string with {keywords} replace a la ET()
- * @param $values (zero, one or more arrays can be passed)
+ * @param $args (zero, one or more arrays can be passed)
* @return output of command. shell errors not detectable, see error_log in /www/server/log
*/
-static function shell_command(/* $cmd, $values1 = array(), ... */)
+static function shell_command($cmd, ...$args)
{
- $args = func_get_args();
- $cmd = array_shift($args);
$values = array();
# Merge values into one array
@@ -686,10 +681,10 @@ static function shell_command(/* $cmd, $values1 = array(), ... */)
# for escapeshellarg in it::_exec_quotevalue
$oldlocale = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'de_CH');
- foreach (it::match('({(-?)([a-z0-9]\w*)})', $cmd, array('all' => true)) as $tags)
+ foreach (it::match('({(-?)([a-z0-9]\w*)})', $cmd, ['all' => true]) as $tags)
{
list($tag, $option, $key) = $tags;
- $parts = array();
+ $parts = [];
if ($option)
{
@@ -734,9 +729,9 @@ static function _exec_quotevalue($value, $errmsg = "")
* @params see it::exec()
* @see it::exec, /opt/ultra/bin/cdist
*/
-static function cexec(/* $cmd, $values1 = array(), ... */)
+static function cexec($cmd, ...$args)
{
- return it::exec('cdist -c {CMD}', array('CMD' => call_user_func_array('it::shell_command', func_get_args())));
+ return it::exec('cdist -c {cmd}', ['cmd' => it::shell_command($cmd, ...$args)]);
}
diff --git a/it_dbi.class b/it_dbi.class
index 65c083e..d8a3393 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -61,7 +61,7 @@ class it_dbi
* @param $p optional array(key => value) of configuration data
* @param $query Optional initial query to run
*/
-function __construct($p = array(), $query = null)
+function __construct($p = array(), $query = null, ...$args)
{
# Shortcut: String config means use this table with default values
if (!is_array($p))
@@ -90,12 +90,7 @@ function __construct($p = array(), $query = null)
$this->_p += $this->_get_field_info(); # Get $this->_fields and p[keyfield, autoincrement, randomid]
if (is_array($query))
- {
- # Call with all arguments except first one
- $args = func_get_args();
- array_shift($args);
- call_user_func_array(array($this, "select"), $args);
- }
+ $this->select($query, ...$args); # Call with all arguments except first one
else if (isset($query))
$this->read($query);
}
@@ -183,11 +178,8 @@ static function createclass($p)
$parentname = static::$_global_key;
$code = "class $classname extends $parentname $interface
{
- function __construct(/* \$query ... */)
+ function __construct(\$query = null, ...\$args)
{
- \$args = func_get_args();
- \$query = array_shift(\$args); # Preserve type (scalar/array) in single parameter case
-
foreach (\$args as \$arg)
\$query = array_merge((array)\$query, (array)\$arg);
@@ -651,10 +643,10 @@ function read($id=null)
* @see iterate()
* @see _where()
*/
-function select(/* $query = array|string, ... */)
+function select(...$args)
{
$query = array();
- foreach (func_get_args() as $arg)
+ foreach ($args as $arg)
$query = array_merge($query, (array)$arg);
$this->_connect();
diff --git a/it_html.class b/it_html.class
index 1ce8b86..057c410 100644
--- a/it_html.class
+++ b/it_html.class
@@ -113,7 +113,7 @@ function __construct($p = array())
foreach (array_keys($this->alltags) as $func)
{
if (!function_exists($func) && $func)
- $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['{$this->p['name']}']->_tag('$func', \$args); }";
+ $code[$func] = "function $func(...\$args) { return \$GLOBALS['{$this->p['name']}']->_tag('$func', \$args); }";
else if ($this->p['error_on_redefine'])
it::error("Trying to redefine existing function '$func' in it_html");
}
@@ -122,14 +122,14 @@ function __construct($p = array())
foreach (get_class_methods(get_class($this)) as $func)
{
if (!preg_match('/^_/', $func) && !is_a($this, $func) && $func && !function_exists($func) && !$notexported[$func])
- $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['{$this->p['name']}']->$func(\$args); }";
+ $code[$func] = "function $func(...\$args) { return \$GLOBALS['{$this->p['name']}']->$func(\$args); }";
}
# Create global functions for methods that are statically callable (have same arguments as global stubs)
foreach (explode(',', $this->p['staticallycallable']) as $func)
{
if ($func && !function_exists($func))
- $code[$func] = "function $func() { \$args = func_get_args(); return call_user_func_array(array(&\$GLOBALS['{$this->p['name']}'], '$func'), \$args); }";
+ $code[$func] = "function $func(...\$args) { return call_user_func_array(array(&\$GLOBALS['{$this->p['name']}'], '$func'), \$args); }";
}
eval(implode('', (array)$code));
@@ -492,9 +492,8 @@ static function Q($string)
* Build a complete url from base-url and params
* @param ... scalar args and numeric indices build base-url, rest as params
*/
-static function U(/* ... */)
+static function U(...$args)
{
- $args = func_get_args();
list($base, $params) = it_parse_args($args);
if (!isset($base))