| Total Complexity | 62 |
| Total Lines | 277 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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) |
||
| 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 | // Early exit if the text is empty |
||
| 96 | if (empty($text)) { |
||
| 97 | return $text; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Set default $from encoding if not provided |
||
| 101 | if (empty($from)) { |
||
| 102 | $from = empty($GLOBALS['xlanguage']['charset_base']) ? _CHARSET : $GLOBALS['xlanguage']['charset_base']; |
||
| 103 | } |
||
| 104 | |||
| 105 | // If $to and $from are the same, no conversion is needed |
||
| 106 | if (empty($to) || !strcasecmp($to, $from)) { |
||
| 107 | return $text; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Initialize a variable to store the converted text |
||
| 111 | $convertedText = ''; |
||
| 112 | |||
| 113 | // Try to use mb_convert_encoding if available |
||
| 114 | if (XOOPS_USE_MULTIBYTES && function_exists('mb_convert_encoding')) { |
||
| 115 | $convertedText = mb_convert_encoding($text, $to, $from); |
||
| 116 | if (false !== $convertedText) { |
||
| 117 | return $convertedText; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Try to use iconv if available |
||
| 122 | if (function_exists('iconv')) { |
||
| 123 | $convertedText = iconv($from, $to . '//TRANSLIT', $text); |
||
| 124 | if (false !== $convertedText) { |
||
| 125 | return $convertedText; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Try to use utf8_encode if target encoding is 'utf-8' |
||
| 130 | if ('utf-8' === $to) { |
||
| 131 | $convertedText = utf8_encode($text); |
||
| 132 | if (false !== $convertedText) { |
||
| 133 | return $convertedText; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // If all conversions fail, return the original text |
||
| 138 | return $text; |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * XoopsLocalAbstract::trim() |
||
| 144 | * |
||
| 145 | * @param mixed $text |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public static function trim($text) |
||
| 149 | { |
||
| 150 | $ret = trim($text); |
||
| 151 | |||
| 152 | return $ret; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get description for setting time format |
||
| 157 | */ |
||
| 158 | public static function getTimeFormatDesc() |
||
| 159 | { |
||
| 160 | return _TIMEFORMAT_DESC; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Function to display formatted times in user timezone |
||
| 165 | * |
||
| 166 | * Setting $timeoffset to null (by default) will skip timezone calculation for user, using default timezone instead, which is a MUST for cached contents |
||
| 167 | * @param $time |
||
| 168 | * @param string $format |
||
| 169 | * @param null|string $timeoffset |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public static function formatTimestamp($time, $format = 'l', $timeoffset = null) |
||
| 173 | { |
||
| 174 | global $xoopsConfig, $xoopsUser; |
||
| 175 | |||
| 176 | $format_copy = $format; |
||
| 177 | $format = strtolower($format); |
||
| 178 | |||
| 179 | if ($format === 'rss' || $format === 'r') { |
||
| 180 | $TIME_ZONE = ''; |
||
| 181 | if (isset($GLOBALS['xoopsConfig']['server_TZ'])) { |
||
| 182 | $server_TZ = abs((int)($GLOBALS['xoopsConfig']['server_TZ'] * 3600.0)); |
||
| 183 | $prefix = ($GLOBALS['xoopsConfig']['server_TZ'] < 0) ? ' -' : ' +'; |
||
| 184 | $TIME_ZONE = $prefix . date('Hi', $server_TZ); |
||
| 185 | } |
||
| 186 | $date = gmdate('D, d M Y H:i:s', (int)$time) . $TIME_ZONE; |
||
| 187 | |||
| 188 | return $date; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (($format === 'elapse' || $format === 'e') && $time < time()) { |
||
| 192 | $elapse = time() - $time; |
||
| 193 | if ($days = floor($elapse / (24 * 3600))) { |
||
| 194 | $num = $days > 1 ? sprintf(_DAYS, $days) : _DAY; |
||
| 195 | } elseif ($hours = floor(($elapse % (24 * 3600)) / 3600)) { |
||
| 196 | $num = $hours > 1 ? sprintf(_HOURS, $hours) : _HOUR; |
||
| 197 | } elseif ($minutes = floor(($elapse % 3600) / 60)) { |
||
| 198 | $num = $minutes > 1 ? sprintf(_MINUTES, $minutes) : _MINUTE; |
||
| 199 | } else { |
||
| 200 | $seconds = $elapse % 60; |
||
| 201 | $num = $seconds > 1 ? sprintf(_SECONDS, $seconds) : _SECOND; |
||
| 202 | } |
||
| 203 | $ret = sprintf(_ELAPSE, $num); |
||
| 204 | |||
| 205 | return $ret; |
||
| 206 | } |
||
| 207 | // disable user timezone calculation and use default timezone, |
||
| 208 | // for cache consideration |
||
| 209 | if ($timeoffset === null) { |
||
| 210 | $timeoffset = ($xoopsConfig['default_TZ'] == '') ? '0.0' : $xoopsConfig['default_TZ']; |
||
| 211 | } |
||
| 212 | $usertimestamp = xoops_getUserTimestamp($time, $timeoffset); |
||
| 213 | switch ($format) { |
||
| 214 | case 's': |
||
| 215 | $datestring = _SHORTDATESTRING; |
||
| 216 | break; |
||
| 217 | |||
| 218 | case 'm': |
||
| 219 | $datestring = _MEDIUMDATESTRING; |
||
| 220 | break; |
||
| 221 | |||
| 222 | case 'mysql': |
||
| 223 | $datestring = 'Y-m-d H:i:s'; |
||
| 224 | break; |
||
| 225 | |||
| 226 | case 'l': |
||
| 227 | $datestring = _DATESTRING; |
||
| 228 | break; |
||
| 229 | |||
| 230 | case 'c': |
||
| 231 | case 'custom': |
||
| 232 | static $current_timestamp, $today_timestamp, $monthy_timestamp; |
||
| 233 | if (!isset($current_timestamp)) { |
||
| 234 | $current_timestamp = xoops_getUserTimestamp(time(), $timeoffset); |
||
| 235 | } |
||
| 236 | if (!isset($today_timestamp)) { |
||
| 237 | $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp)); |
||
| 238 | } |
||
| 239 | |||
| 240 | if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) { |
||
| 241 | $datestring = ($elapse_today > 0) ? _TODAY : _YESTERDAY; |
||
| 242 | } else { |
||
| 243 | if (!isset($monthy_timestamp)) { |
||
| 244 | $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp)); |
||
| 245 | $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, date('Y', $current_timestamp) + 1); |
||
| 246 | } |
||
| 247 | $datestring = _YEARMONTHDAY; |
||
| 248 | if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) { |
||
| 249 | $datestring = _MONTHDAY; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | |||
| 254 | default: |
||
| 255 | $datestring = _DATESTRING; |
||
| 256 | if ($format != '') { |
||
| 257 | $datestring = $format_copy; |
||
| 258 | } |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | |||
| 262 | return ucfirst(date($datestring, $usertimestamp)); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * XoopsLocalAbstract::number_format() |
||
| 267 | * |
||
| 268 | * @param mixed $number |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | public function number_format($number) |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * XoopsLocalAbstract::money_format() |
||
| 278 | * |
||
| 279 | * @param mixed $format |
||
| 280 | * @param mixed $number |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | public function money_format($format, $number) |
||
| 284 | { |
||
| 285 | return $number; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * XoopsLocalAbstract::__call() |
||
| 290 | * |
||
| 291 | * @param mixed $name |
||
| 292 | * @param mixed $args |
||
| 293 | * @return mixed |
||
| 294 | */ |
||
| 295 | public function __call($name, $args) |
||
| 301 | } |
||
| 302 | } |
||
| 303 |