From c98059e43d381d7dc9a12da8c17899cfeba8d693 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 26 Nov 2020 20:34:35 +0100 Subject: [PATCH] Short array syntax, CS fixes, tests --- program/lib/Roundcube/bootstrap.php | 22 ++-- program/lib/Roundcube/html.php | 186 ++++++++++++++-------------- program/lib/Roundcube/rcube.php | 151 +++++++++++----------- tests/Framework/Bootstrap.php | 89 ++++++++----- tests/Framework/Html.php | 122 +++++++++--------- tests/Framework/Rcube.php | 33 +++++ 6 files changed, 338 insertions(+), 265 deletions(-) diff --git a/program/lib/Roundcube/bootstrap.php b/program/lib/Roundcube/bootstrap.php index e80165a534d..d3f9486450e 100644 --- a/program/lib/Roundcube/bootstrap.php +++ b/program/lib/Roundcube/bootstrap.php @@ -25,7 +25,7 @@ * @subpackage Core */ -$config = array( +$config = [ 'error_reporting' => E_ALL & ~E_NOTICE & ~E_STRICT, 'display_errors' => false, 'log_errors' => true, @@ -33,16 +33,16 @@ // critical PHP settings here. Only these, which doesn't provide // an error/warning in the logs later. See (#1486307). 'mbstring.func_overload' => 0, -); +]; // check these additional ini settings if not called via CLI if (php_sapi_name() != 'cli') { - $config += array( + $config += [ 'suhosin.session.encrypt' => false, 'file_uploads' => true, 'session.auto_start' => false, 'zlib.output_compression' => false, - ); + ]; } foreach ($config as $optname => $optval) { @@ -109,7 +109,7 @@ * @param string $needle Needle value * @param array $heystack Array to search in * - * @return boolean True if found, False if not + * @return bool True if found, False if not */ function in_array_nocase($needle, $haystack) { @@ -279,7 +279,7 @@ function abbreviate_string($str, $maxlength, $placeholder = '...', $ending = fal */ function array_keys_recursive($array) { - $keys = array(); + $keys = []; if (!empty($array) && is_array($array)) { foreach ($array as $key => $child) { @@ -305,7 +305,7 @@ function array_keys_recursive($array) function asciiwords($str, $css_id = false, $replace_with = '') { $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : ''); - return preg_replace("/[^$allowed]/i", $replace_with, $str); + return preg_replace("/[^$allowed]+/i", $replace_with, $str); } /** @@ -378,8 +378,8 @@ function format_email($email) function version_parse($version) { return str_replace( - array('-stable', '-git'), - array('.0', '.99'), + ['-stable', '-git'], + ['.0', '.99'], $version ); } @@ -400,7 +400,7 @@ function idn_to_utf8($domain) static $idn, $loaded; if (!$loaded) { - $idn = new Net_IDNA2(array('version' => '2008')); + $idn = new Net_IDNA2(['version' => '2008']); $loaded = true; } @@ -431,7 +431,7 @@ function idn_to_ascii($domain) static $idn, $loaded; if (!$loaded) { - $idn = new Net_IDNA2(array('version' => '2008')); + $idn = new Net_IDNA2(['version' => '2008']); $loaded = true; } diff --git a/program/lib/Roundcube/html.php b/program/lib/Roundcube/html.php index ef548918af0..546c4f7316d 100644 --- a/program/lib/Roundcube/html.php +++ b/program/lib/Roundcube/html.php @@ -27,14 +27,14 @@ class html { protected $tagname; protected $content; - protected $attrib = array(); - protected $allowed = array(); + protected $attrib = []; + protected $allowed = []; public static $doctype = 'xhtml'; public static $lc_tags = true; - public static $common_attrib = array('id','class','style','title','align','unselectable','tabindex','role'); - public static $containers = array('iframe','div','span','p','h1','h2','h3','ul','form','textarea','table','thead','tbody','tr','th','td','style','script','a'); - public static $bool_attrib = array('checked','multiple','disabled','selected','autofocus','readonly','required'); + public static $common_attrib = ['id','class','style','title','align','unselectable','tabindex','role']; + public static $containers = ['iframe','div','span','p','h1','h2','h3','ul','form','textarea','table','thead','tbody','tr','th','td','style','script','a']; + public static $bool_attrib = ['checked','multiple','disabled','selected','autofocus','readonly','required']; /** @@ -42,7 +42,7 @@ class html * * @param array $attrib Hash array with tag attributes */ - public function __construct($attrib = array()) + public function __construct($attrib = []) { if (is_array($attrib)) { $this->attrib = $attrib; @@ -71,13 +71,13 @@ public function show() * * @return string The XHTML tag */ - public static function tag($tagname, $attrib = array(), $content = null, $allowed = null) + public static function tag($tagname, $attrib = [], $content = null, $allowed = null) { if (is_string($attrib)) { - $attrib = array('class' => $attrib); + $attrib = ['class' => $attrib]; } - $inline_tags = array('a','span','img'); + $inline_tags = ['a', 'span', 'img']; $suffix = (isset($attrib['nl']) && $content && $attrib['nl'] && !in_array($tagname, $inline_tags)) ? "\n" : ''; $tagname = self::$lc_tags ? strtolower($tagname) : $tagname; @@ -98,14 +98,14 @@ public static function tag($tagname, $attrib = array(), $content = null, $allowe */ public static function doctype($type) { - $doctypes = array( + $doctypes = [ 'html5' => '', 'xhtml' => '', 'xhtml-trans' => '', 'xhtml-strict' => '', - ); + ]; - if ($doctypes[$type]) { + if (!empty($doctypes[$type])) { self::$doctype = preg_replace('/-\w+$/', '', $type); return $doctypes[$type]; } @@ -125,10 +125,10 @@ public static function doctype($type) public static function div($attr = null, $cont = null) { if (is_string($attr)) { - $attr = array('class' => $attr); + $attr = ['class' => $attr]; } - return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick'))); + return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, ['onclick'])); } /** @@ -143,7 +143,7 @@ public static function div($attr = null, $cont = null) public static function p($attr = null, $cont = null) { if (is_string($attr)) { - $attr = array('class' => $attr); + $attr = ['class' => $attr]; } return self::tag('p', $attr, $cont, self::$common_attrib); @@ -152,7 +152,7 @@ public static function p($attr = null, $cont = null) /** * Derrived method to create * - * @param mixed $attr Hash array with tag attributes or string with image source (src) + * @param string|array $attr Hash array with tag attributes or string with image source (src) * * @return string HTML code * @see html::tag() @@ -160,18 +160,19 @@ public static function p($attr = null, $cont = null) public static function img($attr = null) { if (is_string($attr)) { - $attr = array('src' => $attr); + $attr = ['src' => $attr]; } - return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib, - array('src','alt','width','height','border','usemap','onclick','onerror','onload'))); + $allowed = ['src','alt','width','height','border','usemap','onclick','onerror','onload']; + + return self::tag('img', $attr + ['alt' => ''], null, array_merge(self::$common_attrib, $allowed)); } /** * Derrived method for link tags * - * @param mixed $attr Hash array with tag attributes or string with link location (href) - * @param string $cont Link content + * @param string|array $attr Hash array with tag attributes or string with link location (href) + * @param string $cont Link content * * @return string HTML code * @see html::tag() @@ -179,18 +180,19 @@ public static function img($attr = null) public static function a($attr, $cont) { if (is_string($attr)) { - $attr = array('href' => $attr); + $attr = ['href' => $attr]; } - return self::tag('a', $attr, $cont, array_merge(self::$common_attrib, - array('href','target','name','rel','onclick','onmouseover','onmouseout','onmousedown','onmouseup'))); + $allowed = ['href','target','name','rel','onclick','onmouseover','onmouseout','onmousedown','onmouseup']; + + return self::tag('a', $attr, $cont, array_merge(self::$common_attrib, $allowed)); } /** * Derrived method for inline span tags * - * @param mixed $attr Hash array with tag attributes or string with class name - * @param string $cont Tag content + * @param string|array $attr Hash array with tag attributes or string with class name + * @param string $cont Tag content * * @return string HTML code * @see html::tag() @@ -198,7 +200,7 @@ public static function a($attr, $cont) public static function span($attr, $cont) { if (is_string($attr)) { - $attr = array('class' => $attr); + $attr = ['class' => $attr]; } return self::tag('span', $attr, $cont, self::$common_attrib); @@ -207,8 +209,8 @@ public static function span($attr, $cont) /** * Derrived method for form element labels * - * @param mixed $attr Hash array with tag attributes or string with 'for' attrib - * @param string $cont Tag content + * @param string|array $attr Hash array with tag attributes or string with 'for' attrib + * @param string $cont Tag content * * @return string HTML code * @see html::tag() @@ -216,18 +218,17 @@ public static function span($attr, $cont) public static function label($attr, $cont) { if (is_string($attr)) { - $attr = array('for' => $attr); + $attr = ['for' => $attr]; } - return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, - array('for','onkeypress'))); + return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, ['for','onkeypress'])); } /** * Derrived method to create * - * @param mixed $attr Hash array with tag attributes or string with frame source (src) - * @param string $cont Tag content + * @param string|array $attr Hash array with tag attributes or string with frame source (src) + * @param string $cont Tag content * * @return string HTML code * @see html::tag() @@ -235,18 +236,19 @@ public static function label($attr, $cont) public static function iframe($attr = null, $cont = null) { if (is_string($attr)) { - $attr = array('src' => $attr); + $attr = ['src' => $attr]; } - return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib, - array('src','name','width','height','border','frameborder','onload','allowfullscreen'))); + $allowed = ['src','name','width','height','border','frameborder','onload','allowfullscreen']; + + return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib, $allowed)); } /** * Derrived method to create