From 0b71dd27567f8ea386ac306340c31c9b2e083aa1 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Sun, 10 Jul 2016 11:09:21 +0200 Subject: [PATCH] replace (slower and ugly) create_function with closures --- api/src/Storage/Merge.php | 6 ++- api/src/Vfs.php | 51 ++++++++++++++--------- calendar/inc/class.calendar_bo.inc.php | 7 +++- calendar/inc/class.calendar_so.inc.php | 6 ++- mail/inc/class.mail_compose.inc.php | 5 ++- setup/inc/class.setup_translation.inc.php | 5 ++- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/api/src/Storage/Merge.php b/api/src/Storage/Merge.php index 2140ad1eec..f30f5c9e0c 100644 --- a/api/src/Storage/Merge.php +++ b/api/src/Storage/Merge.php @@ -703,8 +703,10 @@ abstract class Merge } // alternative syntax using double curly brackets (eg. {{cat_id}} instead $$cat_id$$), // agressivly removing all xml-tags eg. Word adds within placeholders - $content = preg_replace_callback('/{{[^}]+}}/i',create_function('$p','return \'$$\'.strip_tags(substr($p[0],2,-2)).\'$$\';'),$_content); - + $content = preg_replace_callback('/{{[^}]+}}/i', function($matches) + { + return '$$'.strip_tags(substr($matches[0], 2, -2)).'$$'; + }, $_content); // Handle escaped placeholder markers in RTF, they won't match when escaped if($mimetype == 'application/rtf') { diff --git a/api/src/Vfs.php b/api/src/Vfs.php index f23a32464e..9b620071c3 100644 --- a/api/src/Vfs.php +++ b/api/src/Vfs.php @@ -570,14 +570,11 @@ class Vfs extends Vfs\StreamWrapper self::_check_add($options,$path,$result); } } - // sort code, to place directories before files, if $dirsontop enabled - $dirsfirst = $dirsontop ? '($a[mime]==\''.self::DIR_MIME_TYPE.'\')!==($b[mime]==\''.self::DIR_MIME_TYPE.'\')?'. - '($a[mime]==\''.self::DIR_MIME_TYPE.'\'?-1:1):' : ''; // ordering of the rows if (isset($options['order'])) { - $sort = strtolower($options['sort']) == 'desc' ? '-' : ''; - switch($options['order']) + $sort_desc = strtolower($options['sort']) == 'desc'; + switch($order = $options['order']) { // sort numerical case 'size': @@ -586,32 +583,46 @@ class Vfs extends Vfs\StreamWrapper case 'mode': case 'ctime': case 'mtime': - $code = $dirsfirst.$sort.'($a[\''.$options['order'].'\']-$b[\''.$options['order'].'\']);'; - // always use name as second sort criteria - $code = '$cmp = '.$code.' return $cmp ? $cmp : strcasecmp($a[\'name\'],$b[\'name\']);'; - $ok = uasort($result,create_function('$a,$b',$code)); + $ok = uasort($result, function($a, $b) use ($dirsontop, $sort_desc, $order) + { + $cmp = $a[$order] - $b[$order]; + // sort code, to place directories before files, if $dirsontop enabled + if ($dirsontop && ($a['mime'] == self::DIR_MIME_TYPE) !== ($b['mime'] == self::DIR_MIME_TYPE)) + { + $cmp = $a['mime' ] == self::DIR_MIME_TYPE ? -1 : 1; + $sort_desc = false; + } + // reverse sort for descending + if ($sort_desc) $cmp *= -1; + // always use name as second sort criteria + if (!$cmp) $cmp = strcasecmp($a['name'], $b['name']); + return $cmp; + }); break; // sort alphanumerical default: - $options['order'] = 'name'; + $order = 'name'; // fall throught case 'name': case 'mime': - $code = $dirsfirst.$sort.'strcasecmp($a[\''.$options['order'].'\'],$b[\''.$options['order'].'\']);'; - if ($options['order'] != 'name') + $ok = uasort($result, function($a, $b) use ($dirsontop, $order, $sort_desc) { + $cmp = strcasecmp($a[$order], $b[$order]); + // sort code, to place directories before files, if $dirsontop enabled + if ($dirsontop && ($a['mime'] == self::DIR_MIME_TYPE) !== ($b['mime'] == self::DIR_MIME_TYPE)) + { + $cmp = $a['mime' ] == self::DIR_MIME_TYPE ? -1 : 1; + $sort_desc = false; + } + // reverse sort for descending + if ($sort_desc) $cmp *= -1; // always use name as second sort criteria - $code = '$cmp = '.$code.' return $cmp ? $cmp : strcasecmp($a[\'name\'],$b[\'name\']);'; - } - else - { - $code = 'return '.$code; - } - $ok = uasort($result,create_function('$a,$b',$code)); + if (!$cmp && $order != 'name') $cmp = strcasecmp($a['name'], $b['name']); + return $cmp; + }); break; } - //echo "

order='$options[order]', sort='$options[sort]' --> uasort($result,create_function(,'$code'))=".array2string($ok)."

>\n"; } // limit resultset self::$find_total = count($result); diff --git a/calendar/inc/class.calendar_bo.inc.php b/calendar/inc/class.calendar_bo.inc.php index e4aee72b21..8f41ca3e22 100644 --- a/calendar/inc/class.calendar_bo.inc.php +++ b/calendar/inc/class.calendar_bo.inc.php @@ -1854,7 +1854,12 @@ class calendar_bo if ($bdays) { // sort by month and day only - usort($bdays,create_function('$a,$b','return (int) $a[\'bday\'] == (int) $b[\'bday\'] ? strcmp($a[\'bday\'],$b[\'bday\']) : (int) $a[\'bday\'] - (int) $b[\'bday\'];')); + usort($bdays, function($a, $b) + { + return (int) $a['bday'] == (int) $b['bday'] ? + strcmp($a['bday'], $b['bday']) : + (int) $a['bday'] - (int) $b['bday']; + }); foreach($bdays as $pers) { if (empty($pers['bday']) || $pers['bday']=='0000-00-00 0' || $pers['bday']=='0000-00-00' || $pers['bday']=='0.0.00') diff --git a/calendar/inc/class.calendar_so.inc.php b/calendar/inc/class.calendar_so.inc.php index 080509c3d6..daefcc2c80 100644 --- a/calendar/inc/class.calendar_so.inc.php +++ b/calendar/inc/class.calendar_so.inc.php @@ -603,7 +603,11 @@ class calendar_so if ($cat_id) { $cats = $GLOBALS['egw']->categories->return_all_children($cat_id); - array_walk($cats,create_function('&$val,$key','$val = (int) $val;')); + array_walk($cats, function(&$val, $key) + { + unset($key); // not used, but required by function signature + $val = (int) $val; + }); if (is_array($cat_id) && count($cat_id)==1) $cat_id = $cat_id[0]; $sql = '(cal_category'.(count($cats) > 1 ? " IN ('".implode("','",$cats)."')" : '='.$this->db->quote((int)$cat_id)); foreach($cats as $cat) diff --git a/mail/inc/class.mail_compose.inc.php b/mail/inc/class.mail_compose.inc.php index 58db61e888..60f9040943 100644 --- a/mail/inc/class.mail_compose.inc.php +++ b/mail/inc/class.mail_compose.inc.php @@ -850,7 +850,10 @@ class mail_compose if ($contacts && $accounts) { $contacts = array_merge($contacts,$accounts); - usort($contacts,create_function('$a,$b','return strcasecmp($a["n_fn"],$b["n_fn"]);')); + usort($contacts, function($a, $b) + { + return strcasecmp($a['n_fn'], $b['n_fn']); + }); } elseif($accounts) { diff --git a/setup/inc/class.setup_translation.inc.php b/setup/inc/class.setup_translation.inc.php index e635c3a566..e0f49f35d8 100644 --- a/setup/inc/class.setup_translation.inc.php +++ b/setup/inc/class.setup_translation.inc.php @@ -141,7 +141,10 @@ class setup_translation } } $d->close(); - uasort($languages,create_function('$a,$b','return strcmp(@$a[\'descr\'],@$b[\'descr\']);')); + uasort($languages, function($a, $b) + { + return strcmp(@$a['descr'], @$b['descr']); + }); } else {