Completed
Branch master (55a138)
by Michael
03:21
created

Time::getHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php namespace XoopsModules\Extcal;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 7.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use XoopsModules\Extcal;
4
5
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
6
7
require_once XOOPS_ROOT_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/calendar.php';
8
9
$moduleDirName = basename(dirname(__DIR__));
10
Extcal\Helper::getInstance()->loadLanguage('main');
11
12
/**
13
 * Class Time.
14
 */
15
class Time
16
{
17
    /**
18
     * @return Time
19
     */
20
    public static function getHandler()
21
    {
22
        static $timeHandler;
23
        if (!isset($timeHandler)) {
24
            $timeHandler = new self();
25
        }
26
27
        return $timeHandler;
28
    }
29
30
    /**
31
     * @param XoopsUser|string $user
32
     *
33
     * @return mixed
34
     */
35
    public function _getUserTimeZone($user)
36
    {
37
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
38
39
        return $user ? $user->timezone() : $xoopsConfig['default_TZ'];
40
    }
41
42
    /**
43
     * @param $id
44
     *
45
     * @return mixed
46
     */
47
    public function getMonthName($id)
48
    {
49
        $monthName = [
50
            '1'  => _CAL_JANUARY,
51
            '2'  => _CAL_FEBRUARY,
52
            '3'  => _CAL_MARCH,
53
            '4'  => _CAL_APRIL,
54
            '5'  => _CAL_MAY,
55
            '6'  => _CAL_JUNE,
56
            '7'  => _CAL_JULY,
57
            '8'  => _CAL_AUGUST,
58
            '9'  => _CAL_SEPTEMBER,
59
            '10' => _CAL_OCTOBER,
60
            '11' => _CAL_NOVEMBER,
61
            '12' => _CAL_DECEMBER,
62
        ];
63
64
        return $monthName[$id];
65
    }
66
67
    /**
68
     * @param $id
69
     *
70
     * @return mixed
71
     */
72
    public function getDayName($id)
73
    {
74
        $dayName = [
75
            _CAL_SUNDAY,
76
            _CAL_MONDAY,
77
            _CAL_TUESDAY,
78
            _CAL_WEDNESDAY,
79
            _CAL_THURSDAY,
80
            _CAL_FRIDAY,
81
            _CAL_SATURDAY,
82
        ];
83
84
        return $dayName[$id];
85
    }
86
87
    /**
88
     * @param $format
89
     * @param $timestamp
90
     *
91
     * @return mixed
92
     */
93
    public function getFormatedDate($format, $timestamp)
94
    {
95
        $patterns     = [
96
            '/January/',
97
            '/February/',
98
            '/March/',
99
            '/April/',
100
            '/May/',
101
            '/June/',
102
            '/July/',
103
            '/August/',
104
            '/September/',
105
            '/October/',
106
            '/November/',
107
            '/December/',
108
            '/Jan /',
109
            '/Feb /',
110
            '/Mar /',
111
            '/Apr /',
112
            '/May /',
113
            '/Jun /',
114
            '/Jul /',
115
            '/Aug /',
116
            '/Sep /',
117
            '/Oct /',
118
            '/Nov /',
119
            '/Dec /',
120
            '/Sunday/',
121
            '/Monday/',
122
            '/Tuesday/',
123
            '/Wednesday/',
124
            '/Thursday/',
125
            '/Friday/',
126
            '/Saturday/',
127
            '/Sun /',
128
            '/Mon /',
129
            '/Tue /',
130
            '/Wed /',
131
            '/Thu /',
132
            '/Fri /',
133
            '/Sat /',
134
        ];
135
        $replacements = [
136
            _CAL_JANUARY,
137
            _CAL_FEBRUARY,
138
            _CAL_MARCH,
139
            _CAL_APRIL,
140
            _CAL_MAY,
141
            _CAL_JUNE,
142
            _CAL_JULY,
143
            _CAL_AUGUST,
144
            _CAL_SEPTEMBER,
145
            _CAL_OCTOBER,
146
            _CAL_NOVEMBER,
147
            _CAL_DECEMBER,
148
            substr(_CAL_JANUARY, 0, 3) . ' ',
149
            substr(_CAL_FEBRUARY, 0, 3) . ' ',
150
            substr(_CAL_MARCH, 0, 3) . ' ',
151
            substr(_CAL_APRIL, 0, 3) . ' ',
152
            substr(_CAL_MAY, 0, 3) . ' ',
153
            substr(_CAL_JUNE, 0, 3) . ' ',
154
            substr(_CAL_JULY, 0, 3) . ' ',
155
            substr(_CAL_AUGUST, 0, 3) . ' ',
156
            substr(_CAL_SEPTEMBER, 0, 3) . ' ',
157
            substr(_CAL_OCTOBER, 0, 3) . ' ',
158
            substr(_CAL_NOVEMBER, 0, 3) . ' ',
159
            substr(_CAL_DECEMBER, 0, 3) . ' ',
160
            _CAL_SUNDAY,
161
            _CAL_MONDAY,
162
            _CAL_TUESDAY,
163
            _CAL_WEDNESDAY,
164
            _CAL_THURSDAY,
165
            _CAL_FRIDAY,
166
            _CAL_SATURDAY,
167
            substr(_CAL_SUNDAY, 0, 3) . ' ',
168
            substr(_CAL_MONDAY, 0, 3) . ' ',
169
            substr(_CAL_TUESDAY, 0, 3) . ' ',
170
            substr(_CAL_WEDNESDAY, 0, 3) . ' ',
171
            substr(_CAL_THURSDAY, 0, 3) . ' ',
172
            substr(_CAL_FRIDAY, 0, 3) . ' ',
173
            substr(_CAL_SATURDAY, 0, 3) . ' ',
174
        ];
175
176
        return preg_replace($patterns, $replacements, date($format, $timestamp));
177
    }
178
179
    /**
180
     * @param $event_recur_rules
181
     *
182
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
183
     */
184
    public function getFormatedReccurRule($event_recur_rules)
185
    {
186
        $eventOptions = explode('|', $event_recur_rules);
187
188
        switch ($eventOptions[0]) {
189
190
            case 'daily':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
191
192
                $interval = $eventOptions[1];
193
194
                return sprintf(_MD_EXTCAL_RR_DAILY, $interval);
195
196
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
197
198
            case 'weekly':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
199
200
                $daysName = [
201
                    'MO' => _CAL_MONDAY,
202
                    'TU' => _CAL_TUESDAY,
203
                    'WE' => _CAL_WEDNESDAY,
204
                    'TH' => _CAL_THURSDAY,
205
                    'FR' => _CAL_FRIDAY,
206
                    'SA' => _CAL_SATURDAY,
207
                    'SU' => _CAL_SUNDAY,
208
                ];
209
210
                $interval = $eventOptions[1];
211
                array_shift($eventOptions);
212
                array_shift($eventOptions);
213
                $day = '';
214
                foreach ($eventOptions as $option) {
215
                    $day .= ' ' . $daysName[$option] . ', ';
216
                }
217
                $ret = sprintf(_MD_EXTCAL_RR_WEEKLY, $day, $interval);
218
219
                return $ret;
220
221
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
222
223
            case 'monthly':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
224
225
                $monthDays = [
226
                    '1MO'  => _MD_EXTCAL_1_MO,
227
                    '1TU'  => _MD_EXTCAL_1_TU,
228
                    '1WE'  => _MD_EXTCAL_1_WE,
229
                    '1TH'  => _MD_EXTCAL_1_TH,
230
                    '1FR'  => _MD_EXTCAL_1_FR,
231
                    '1SA'  => _MD_EXTCAL_1_SA,
232
                    '1SU'  => _MD_EXTCAL_1_SU,
233
                    '2MO'  => _MD_EXTCAL_2_MO,
234
                    '2TU'  => _MD_EXTCAL_2_TU,
235
                    '2WE'  => _MD_EXTCAL_2_WE,
236
                    '2TH'  => _MD_EXTCAL_2_TH,
237
                    '2FR'  => _MD_EXTCAL_2_FR,
238
                    '2SA'  => _MD_EXTCAL_2_SA,
239
                    '2SU'  => _MD_EXTCAL_2_SU,
240
                    '3MO'  => _MD_EXTCAL_3_MO,
241
                    '3TU'  => _MD_EXTCAL_3_TU,
242
                    '3WE'  => _MD_EXTCAL_3_WE,
243
                    '3TH'  => _MD_EXTCAL_3_TH,
244
                    '3FR'  => _MD_EXTCAL_3_FR,
245
                    '3SA'  => _MD_EXTCAL_3_SA,
246
                    '3SU'  => _MD_EXTCAL_3_SU,
247
                    '4MO'  => _MD_EXTCAL_4_MO,
248
                    '4TU'  => _MD_EXTCAL_4_TU,
249
                    '4WE'  => _MD_EXTCAL_4_WE,
250
                    '4TH'  => _MD_EXTCAL_4_TH,
251
                    '4FR'  => _MD_EXTCAL_4_FR,
252
                    '4SA'  => _MD_EXTCAL_4_SA,
253
                    '4SU'  => _MD_EXTCAL_4_SU,
254
                    '-1MO' => _MD_EXTCAL_LAST_MO,
255
                    '-1TU' => _MD_EXTCAL_LAST_TU,
256
                    '-1WE' => _MD_EXTCAL_LAST_WE,
257
                    '-1TH' => _MD_EXTCAL_LAST_TH,
258
                    '-1FR' => _MD_EXTCAL_LAST_FR,
259
                    '-1SA' => _MD_EXTCAL_LAST_SA,
260
                    '-1SU' => _MD_EXTCAL_LAST_SU,
261
                ];
262
263
                $interval = $eventOptions[1];
264
                if (0 === strpos($eventOptions[2], 'MD')) {
265
                    return sprintf(_MD_EXTCAL_RR_MONTHLY, substr($eventOptions[2], 2), $interval);
266
                } else {
267
                    return sprintf(_MD_EXTCAL_RR_MONTHLY, $monthDays[$eventOptions[2]], $interval);
268
                }
269
270
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
271
272
            case 'yearly':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
273
274
                $monthDays = [
275
                    '1MO'  => _MD_EXTCAL_1_MO,
276
                    '1TU'  => _MD_EXTCAL_1_TU,
277
                    '1WE'  => _MD_EXTCAL_1_WE,
278
                    '1TH'  => _MD_EXTCAL_1_TH,
279
                    '1FR'  => _MD_EXTCAL_1_FR,
280
                    '1SA'  => _MD_EXTCAL_1_SA,
281
                    '1SU'  => _MD_EXTCAL_1_SU,
282
                    '2MO'  => _MD_EXTCAL_2_MO,
283
                    '2TU'  => _MD_EXTCAL_2_TU,
284
                    '2WE'  => _MD_EXTCAL_2_WE,
285
                    '2TH'  => _MD_EXTCAL_2_TH,
286
                    '2FR'  => _MD_EXTCAL_2_FR,
287
                    '2SA'  => _MD_EXTCAL_2_SA,
288
                    '2SU'  => _MD_EXTCAL_2_SU,
289
                    '3MO'  => _MD_EXTCAL_3_MO,
290
                    '3TU'  => _MD_EXTCAL_3_TU,
291
                    '3WE'  => _MD_EXTCAL_3_WE,
292
                    '3TH'  => _MD_EXTCAL_3_TH,
293
                    '3FR'  => _MD_EXTCAL_3_FR,
294
                    '3SA'  => _MD_EXTCAL_3_SA,
295
                    '3SU'  => _MD_EXTCAL_3_SU,
296
                    '4MO'  => _MD_EXTCAL_4_MO,
297
                    '4TU'  => _MD_EXTCAL_4_TU,
298
                    '4WE'  => _MD_EXTCAL_4_WE,
299
                    '4TH'  => _MD_EXTCAL_4_TH,
300
                    '4FR'  => _MD_EXTCAL_4_FR,
301
                    '4SA'  => _MD_EXTCAL_4_SA,
302
                    '4SU'  => _MD_EXTCAL_4_SU,
303
                    '-1MO' => _MD_EXTCAL_LAST_MO,
304
                    '-1TU' => _MD_EXTCAL_LAST_TU,
305
                    '-1WE' => _MD_EXTCAL_LAST_WE,
306
                    '-1TH' => _MD_EXTCAL_LAST_TH,
307
                    '-1FR' => _MD_EXTCAL_LAST_FR,
308
                    '-1SA' => _MD_EXTCAL_LAST_SA,
309
                    '-1SU' => _MD_EXTCAL_LAST_SU,
310
                ];
311
312
                $monthName = [
313
                    1  => _CAL_JANUARY,
314
                    2  => _CAL_FEBRUARY,
315
                    3  => _CAL_MARCH,
316
                    4  => _CAL_APRIL,
317
                    5  => _CAL_MAY,
318
                    6  => _CAL_JUNE,
319
                    7  => _CAL_JULY,
320
                    8  => _CAL_AUGUST,
321
                    9  => _CAL_SEPTEMBER,
322
                    10 => _CAL_OCTOBER,
323
                    11 => _CAL_NOVEMBER,
324
                    12 => _CAL_DECEMBER,
325
                ];
326
327
                $interval = $eventOptions[1];
328
                $day      = $eventOptions[2];
329
                array_shift($eventOptions);
330
                array_shift($eventOptions);
331
                array_shift($eventOptions);
332
                $month = '';
333
                foreach ($eventOptions as $option) {
334
                    $month .= ' ' . $monthName[$option] . ', ';
335
                }
336
                $dayString = $day;
337
                if (array_key_exists($day, $monthDays)) {
338
                    $dayString = $monthDays[$day];
339
                }
340
                $ret = sprintf(_MD_EXTCAL_RR_YEARLY, $month, $dayString, $interval);
341
342
                return $ret;
343
344
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
345
346
        }
347
348
        return false;
349
    }
350
}
351