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