| Total Complexity | 60 |
| Total Lines | 252 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like XoopsLocalAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsLocalAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class XoopsLocalAbstract |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * XoopsLocalAbstract::substr() |
||
| 28 | * |
||
| 29 | * @param mixed $str |
||
| 30 | * @param mixed $start |
||
| 31 | * @param mixed $length |
||
| 32 | * @param string $trimmarker |
||
| 33 | * |
||
| 34 | * @return mixed|string |
||
| 35 | */ |
||
| 36 | public static function substr($str, $start, $length, $trimmarker = '...') |
||
| 37 | { |
||
| 38 | if (!XOOPS_USE_MULTIBYTES) { |
||
| 39 | return (strlen($str) - $start <= $length) ? substr($str, $start, $length) : substr($str, $start, $length - strlen($trimmarker)) . $trimmarker; |
||
| 40 | } |
||
| 41 | if (function_exists('mb_internal_encoding') && @mb_internal_encoding(_CHARSET)) { |
||
| 42 | $str2 = mb_strcut($str, $start, $length - strlen($trimmarker)); |
||
| 43 | |||
| 44 | return $str2 . (mb_strlen($str) != mb_strlen($str2) ? $trimmarker : ''); |
||
| 45 | } |
||
| 46 | |||
| 47 | return $str; |
||
| 48 | } |
||
| 49 | // Each local language should define its own equivalent utf8_encode |
||
| 50 | /** |
||
| 51 | * XoopsLocalAbstract::utf8_encode() |
||
| 52 | * |
||
| 53 | * @param mixed $text |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public static function utf8_encode($text) |
||
| 65 | } |
||
| 66 | |||
| 67 | // Each local language should define its own equivalent utf8_encode |
||
| 68 | /** |
||
| 69 | * XoopsLocalAbstract::utf8_decode() |
||
| 70 | * |
||
| 71 | * @param mixed $text |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public static function utf8_decode($text) |
||
| 75 | { |
||
| 76 | if (XOOPS_USE_MULTIBYTES == 1) { |
||
| 77 | if (function_exists('mb_convert_encoding')) { |
||
| 78 | return mb_convert_encoding($text, 'ISO-8859-1', 'auto'); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | return utf8_decode($text); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * XoopsLocalAbstract::convert_encoding() |
||
| 87 | * |
||
| 88 | * @param mixed $text |
||
| 89 | * @param string $to |
||
| 90 | * @param string $from |
||
| 91 | * @return mixed|string |
||
| 92 | */ |
||
| 93 | public static function convert_encoding($text, $to = 'utf-8', $from = '') |
||
| 94 | { |
||
| 95 | if (empty($text)) { |
||
| 96 | return $text; |
||
| 97 | } |
||
| 98 | if (empty($from)) { |
||
| 99 | $from = empty($GLOBALS['xlanguage']['charset_base']) ? _CHARSET : $GLOBALS['xlanguage']['charset_base']; |
||
| 100 | } |
||
| 101 | if (empty($to) || !strcasecmp($to, $from)) { |
||
| 102 | return $text; |
||
| 103 | } |
||
| 104 | |||
| 105 | if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) { |
||
| 106 | $converted_text = @mb_convert_encoding($text, $to, $from); |
||
| 107 | } elseif (function_exists('iconv')) { |
||
| 108 | $converted_text = @iconv($from, $to . '//TRANSLIT', $text); |
||
| 109 | } elseif ('utf-8' === $to) { |
||
| 110 | $converted_text = utf8_encode($text); |
||
| 111 | } |
||
| 112 | $text = empty($converted_text) ? $text : $converted_text; |
||
| 113 | |||
| 114 | return $text; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * XoopsLocalAbstract::trim() |
||
| 119 | * |
||
| 120 | * @param mixed $text |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public static function trim($text) |
||
| 124 | { |
||
| 125 | $ret = trim($text); |
||
| 126 | |||
| 127 | return $ret; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get description for setting time format |
||
| 132 | */ |
||
| 133 | public static function getTimeFormatDesc() |
||
| 134 | { |
||
| 135 | return _TIMEFORMAT_DESC; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Function to display formatted times in user timezone |
||
| 140 | * |
||
| 141 | * Setting $timeoffset to null (by default) will skip timezone calculation for user, using default timezone instead, which is a MUST for cached contents |
||
| 142 | * @param $time |
||
| 143 | * @param string $format |
||
| 144 | * @param null|string $timeoffset |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public static function formatTimestamp($time, $format = 'l', $timeoffset = null) |
||
| 148 | { |
||
| 149 | global $xoopsConfig, $xoopsUser; |
||
| 150 | |||
| 151 | $format_copy = $format; |
||
| 152 | $format = strtolower($format); |
||
| 153 | |||
| 154 | if ($format === 'rss' || $format === 'r') { |
||
| 155 | $TIME_ZONE = ''; |
||
| 156 | if (isset($GLOBALS['xoopsConfig']['server_TZ'])) { |
||
| 157 | $server_TZ = abs((int)($GLOBALS['xoopsConfig']['server_TZ'] * 3600.0)); |
||
| 158 | $prefix = ($GLOBALS['xoopsConfig']['server_TZ'] < 0) ? ' -' : ' +'; |
||
| 159 | $TIME_ZONE = $prefix . date('Hi', $server_TZ); |
||
| 160 | } |
||
| 161 | $date = gmdate('D, d M Y H:i:s', (int)$time) . $TIME_ZONE; |
||
| 162 | |||
| 163 | return $date; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (($format === 'elapse' || $format === 'e') && $time < time()) { |
||
| 167 | $elapse = time() - $time; |
||
| 168 | if ($days = floor($elapse / (24 * 3600))) { |
||
| 169 | $num = $days > 1 ? sprintf(_DAYS, $days) : _DAY; |
||
| 170 | } elseif ($hours = floor(($elapse % (24 * 3600)) / 3600)) { |
||
| 171 | $num = $hours > 1 ? sprintf(_HOURS, $hours) : _HOUR; |
||
| 172 | } elseif ($minutes = floor(($elapse % 3600) / 60)) { |
||
| 173 | $num = $minutes > 1 ? sprintf(_MINUTES, $minutes) : _MINUTE; |
||
| 174 | } else { |
||
| 175 | $seconds = $elapse % 60; |
||
| 176 | $num = $seconds > 1 ? sprintf(_SECONDS, $seconds) : _SECOND; |
||
| 177 | } |
||
| 178 | $ret = sprintf(_ELAPSE, $num); |
||
| 179 | |||
| 180 | return $ret; |
||
| 181 | } |
||
| 182 | // disable user timezone calculation and use default timezone, |
||
| 183 | // for cache consideration |
||
| 184 | if ($timeoffset === null) { |
||
| 185 | $timeoffset = ($xoopsConfig['default_TZ'] == '') ? '0.0' : $xoopsConfig['default_TZ']; |
||
| 186 | } |
||
| 187 | $usertimestamp = xoops_getUserTimestamp($time, $timeoffset); |
||
| 188 | switch ($format) { |
||
| 189 | case 's': |
||
| 190 | $datestring = _SHORTDATESTRING; |
||
| 191 | break; |
||
| 192 | |||
| 193 | case 'm': |
||
| 194 | $datestring = _MEDIUMDATESTRING; |
||
| 195 | break; |
||
| 196 | |||
| 197 | case 'mysql': |
||
| 198 | $datestring = 'Y-m-d H:i:s'; |
||
| 199 | break; |
||
| 200 | |||
| 201 | case 'l': |
||
| 202 | $datestring = _DATESTRING; |
||
| 203 | break; |
||
| 204 | |||
| 205 | case 'c': |
||
| 206 | case 'custom': |
||
| 207 | static $current_timestamp, $today_timestamp, $monthy_timestamp; |
||
| 208 | if (!isset($current_timestamp)) { |
||
| 209 | $current_timestamp = xoops_getUserTimestamp(time(), $timeoffset); |
||
| 210 | } |
||
| 211 | if (!isset($today_timestamp)) { |
||
| 212 | $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp)); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) { |
||
| 216 | $datestring = ($elapse_today > 0) ? _TODAY : _YESTERDAY; |
||
| 217 | } else { |
||
| 218 | if (!isset($monthy_timestamp)) { |
||
| 219 | $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp)); |
||
| 220 | $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp) + 1); |
||
| 221 | } |
||
| 222 | $datestring = _YEARMONTHDAY; |
||
| 223 | if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) { |
||
| 224 | $datestring = _MONTHDAY; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | break; |
||
| 228 | |||
| 229 | default: |
||
| 230 | $datestring = _DATESTRING; |
||
| 231 | if ($format != '') { |
||
| 232 | $datestring = $format_copy; |
||
| 233 | } |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | |||
| 237 | return ucfirst(date($datestring, $usertimestamp)); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * XoopsLocalAbstract::number_format() |
||
| 242 | * |
||
| 243 | * @param mixed $number |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | public function number_format($number) |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * XoopsLocalAbstract::money_format() |
||
| 253 | * |
||
| 254 | * @param mixed $format |
||
| 255 | * @param mixed $number |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | public function money_format($format, $number) |
||
| 259 | { |
||
| 260 | return $number; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * XoopsLocalAbstract::__call() |
||
| 265 | * |
||
| 266 | * @param mixed $name |
||
| 267 | * @param mixed $args |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function __call($name, $args) |
||
| 276 | } |
||
| 277 | } |
||
| 278 |