Passed
Pull Request — master (#1363)
by Michael
10:31
created

XoopsLocalAbstract::formatTimestamp()   F

Complexity

Conditions 31
Paths 173

Size

Total Lines 91
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 91
rs 3.5583
c 0
b 0
f 0
cc 31
nc 173
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * XOOPS localization abstract
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.3.0
16
 * @author              Taiwen Jiang <[email protected]>
17
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Class XoopsLocalAbstract
23
 */
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)
57
    {
58
        if (XOOPS_USE_MULTIBYTES == 1) {
0 ignored issues
show
introduced by
The condition XOOPS_USE_MULTIBYTES == 1 is always false.
Loading history...
59
            if (function_exists('mb_convert_encoding')) {
60
                return mb_convert_encoding($text, 'UTF-8', 'auto');
61
            }
62
        }
63
64
        return 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) {
0 ignored issues
show
introduced by
The condition XOOPS_USE_MULTIBYTES == 1 is always false.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $server_TZ can also be of type double; however, parameter $timestamp of date() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
                $TIME_ZONE = $prefix . date('Hi', /** @scrutinizer ignore-type */ $server_TZ);
Loading history...
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));
0 ignored issues
show
Bug introduced by
date('d', $current_timestamp) of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), /** @scrutinizer ignore-type */ date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('m', $current_timestamp) of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
                    $today_timestamp = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m', $current_timestamp), date('d', $current_timestamp), date('Y', $current_timestamp));
Loading history...
Bug introduced by
date('Y', $current_timestamp) of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
                    $today_timestamp = mktime(0, 0, 0, date('m', $current_timestamp), date('d', $current_timestamp), /** @scrutinizer ignore-type */ date('Y', $current_timestamp));
Loading history...
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)
247
    {
248
        return $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)
271
    {
272
        if (function_exists($name)) {
273
            return call_user_func_array($name, $args);
274
        }
275
        return null;
276
    }
277
}
278