timeDifference()   C
last analyzed

Complexity

Conditions 12
Paths 144

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 12
eloc 30
c 3
b 1
f 0
nc 144
nop 3
dl 0
loc 49
rs 6.6

How to fix   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 declare(strict_types=1);
2
// Author: Lio MJ
3
// Website: https://www.github.com/liomj/
4
5
if (!defined('XOOPS_ROOT_PATH')) {
6
    exit;
7
}
8
9
/**
10
 * @param $options
11
 * @return array
12
 */
13
function show_memberslastlogin_block($options)
14
{
15
    $x      = 0;
16
    $now    = time();
17
    $hours  = 24;
18
    $time   = ((int)$hours > 0) ? time() - ((int)$hours * 3600) : (time() - 24 * 3600);
19
    $block  = [];
20
    $sql    = 'SELECT distinct uid, name, uname, user_avatar, last_login FROM ' . $GLOBALS['xoopsDB']->prefix('users') . " WHERE level > 0 AND last_login >= '" . $time . "' ORDER BY last_login DESC LIMIT " . $options[3] . '';
21
    $result = $GLOBALS['xoopsDB']->query($sql);
22
    while ([$uid, $name, $uname, $user_avatar, $last_login] = $GLOBALS['xoopsDB']->fetchRow($result)) {
23
        $sincelastlogin = ' ' . timeDifference($last_login, $now, _MB_XOOPSMEMBERS_HOURS) . ' ' . _MB_XOOPSMEMBERS_AGO;
24
        $x++;
25
26
        $recentlogin        = [];
27
        $recentlogin['uid'] = $uid;
28
        if ('' != $name && '1' == $options[2]) {
29
            $recentlogin['name'] = htmlspecialchars($name, ENT_QUOTES);
30
        } else {
31
            $recentlogin['name'] = $uname;
32
        }
33
        $recentlogin['user_avatar']    = $user_avatar;
34
        $recentlogin['last_login']     = $last_login;
35
        $recentlogin['sincelastlogin'] = $sincelastlogin;
36
37
        $block['recentlogin'][] = $recentlogin;
38
        unset($recentlogin);
39
    }
40
41
    $block['showrecentloginname']   = $options[0];
42
    $block['showrecentloginavatar'] = $options[1];
43
    $block['userealname']           = $options[2];
44
    $block['memberdisplay']         = $options[3];
45
46
    return $block;
47
}
48
49
/**
50
 * @param        $start
51
 * @param        $end
52
 * @param string $return
53
 * @return string
54
 */
55
function timeDifference($start, $end, $return = 'days')
56
{
57
    //change times to Unix timestamp.
58
    //$start = strtotime($start);
59
    //$end = strtotime($end);
60
    //subtract dates
61
    $difference = max($end, $start) - min($end, $start);
62
    $time       = null;
63
    //24 hours equal to 86400
64
    //calculate time difference.
65
    switch ($return) {
66
        case 'days':
67
            $days         = floor($difference / 86400);
68
            $difference   = $difference % 86400;
69
            $time['days'] = $days;
70
        // no break
71
        case 'hours':
72
            $hours         = floor($difference / 3600);
73
            $difference    = $difference % 3600;
74
            $time['hours'] = $hours;
75
        // no break
76
        case 'minutes':
77
            $minutes         = floor($difference / 60);
78
            $difference      = $difference % 60;
79
            $time['minutes'] = $minutes;
80
        // no break
81
        case 'seconds':
82
            $seconds         = $difference;
83
            $time['seconds'] = $seconds;
84
    }
85
86
    $output = [];
87
    if (is_array($time)) {
0 ignored issues
show
introduced by
The condition is_array($time) is always false.
Loading history...
88
        $showSec = true;
89
        if (isset($time['hours']) && $time['hours'] > 0) {
90
            $output[] = $time['hours'] . ' ' . _MB_XOOPSMEMBERS_HOUR;
91
            $showSec  = false;
92
        }
93
94
        if (isset($time['minutes']) && $time['minutes'] > 0) {
95
            $output[] = $time['minutes'] . ' ' . _MB_XOOPSMEMBERS_MINUTES;
96
            $showSec  = false;
97
        }
98
99
        if (isset($time['seconds']) && true === $showSec) {
100
            return $time['seconds'] . ' ' . _MB_XOOPSMEMBERS_SECONDS;
101
        }
102
103
        return implode(', ', $output);
104
    }
105
}
106
107
/**
108
 * @param $options
109
 * @return string
110
 */
111
function memberslastlogin_edit($options)
112
{
113
    $form = _MB_XOOPSMEMBERS_SHOWRECENTLOGINNAME . '&nbsp;';
114
    $chk  = '';
115
    if (1 == $options[0]) {
116
        $chk = ' checked';
117
    }
118
    $form .= "<input type='radio' name='options[0]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
119
    $chk  = '';
120
    if (0 == $options[0]) {
121
        $chk = ' checked';
122
    }
123
    $form .= "&nbsp;<input type='radio' name='options[0]' value='0'" . $chk . ' >' . _NO . '<br>';
124
125
    $form .= _MB_XOOPSMEMBERS_SHOWRECENTLOGINAVATAR . '&nbsp;';
126
    if (1 == $options[1]) {
127
        $chk = ' checked';
128
    }
129
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
130
    $chk  = '';
131
    if (0 == $options[1]) {
132
        $chk = ' checked';
133
    }
134
    $form .= "&nbsp;<input type='radio' name='options[1]' value='0'" . $chk . ' >' . _NO . '<br>';
135
136
    $form .= _MB_XOOPSMEMBERS_USEREALNAME . '&nbsp;';
137
    if (1 == $options[2]) {
138
        $chk = ' checked';
139
    }
140
    $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
141
    $chk  = '';
142
    if (0 == $options[2]) {
143
        $chk = ' checked';
144
    }
145
    $form .= "&nbsp;<input type='radio' name='options[2]' value='0'" . $chk . ' >' . _NO . '<br>';
146
147
    $form .= _MB_XOOPSMEMBERS_MEMBERDISPLAY . '&nbsp;';
148
    $form .= "<input type='text' name='options[3]' value='" . $options[3] . "'>";
149
150
    return $form;
151
}
152