membersstats_edit()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 41
c 3
b 0
f 0
nc 1024
nop 1
dl 0
loc 58
rs 3.15

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
//  Author: SMD & Trabis
3
//  URL: http://www.xoopsmalaysia.org  & http://www.xuups.com
4
//  E-Mail: [email protected]  & [email protected]
5
6
use Xmf\IPAddress;
7
8
if (!defined('XOOPS_ROOT_PATH')) {
9
    exit;
10
}
11
12
/**
13
 * @param $options
14
 * @return array|bool
15
 */
16
function show_membersstats_block($options)
17
{
18
    global $xoopsConfig, $xoopsUser, $xoopsModule, $xoopsDB, $_SERVER;
19
    /* @var XoopsOnlineHandler $onlineHandler */
20
    $onlineHandler = xoops_getHandler('online');
21
    // set gc probabillity to 10% for now..
22
    if (mt_rand(1, 100) < 11) {
23
        $onlineHandler->gc(300);
24
    }
25
    if (is_object($xoopsUser)) {
26
        $uid   = $xoopsUser->getVar('uid');
27
        $uname = $xoopsUser->getVar('uname');
28
    } else {
29
        $uid   = 0;
30
        $uname = '';
31
    }
32
    $requestIp = IPAddress::fromRequest()->asReadable();
33
    $requestIp = (false === $requestIp) ? '0.0.0.0' : $requestIp;
0 ignored issues
show
introduced by
The condition false === $requestIp is always false.
Loading history...
34
    if (is_object($xoopsModule)) {
35
        $onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), $requestIp);
36
    } else {
37
        $onlineHandler->write($uid, $uname, time(), 0, $requestIp);
38
    }
39
    $onlines = $onlineHandler->getAll();
40
    if (!empty($onlines)) {
41
        $total   = count($onlines);
42
        $block   = [];
43
        $guests  = 0;
44
        $members = '';
45
        for ($i = 0; $i < $total; ++$i) {
46
            if ($onlines[$i]['online_uid'] > 0) {
47
                $members .= ' <a href="' . XOOPS_URL . '/userinfo.php?uid=' . $onlines[$i]['online_uid'] . '" title="' . $onlines[$i]['online_uname'] . '">' . $onlines[$i]['online_uname'] . '</a>,';
48
            } else {
49
                ++$guests;
50
            }
51
        }
52
        $block['online_total'] = sprintf(_ONLINEPHRASE, $total);
53
        if (is_object($xoopsModule)) {
54
            $mytotal               = $onlineHandler->getCount(new \Criteria('online_module', $xoopsModule->getVar('mid')));
55
            $block['online_total'] .= ' (' . sprintf(_ONLINEPHRASEX, $mytotal, $xoopsModule->getVar('name')) . ')';
56
        }
57
        // Membership Statistic
58
        /** @var \XoopsMemberHandler $memberHandler */
59
        $memberHandler      = xoops_getHandler('member');
60
        $today              = formatTimestamp(time());
0 ignored issues
show
Unused Code introduced by
The assignment to $today is dead and can be removed.
Loading history...
61
        $level_criteria     = new \Criteria('level', 0, '>');
62
        $criteria           = new \CriteriaCompo($level_criteria);
63
        $criteria24         = new \CriteriaCompo($level_criteria);
64
        $criteria48         = new \CriteriaCompo($level_criteria);
65
        $total_active_users = $memberHandler->getUserCount($level_criteria);
66
        //Fixing stats for last 24 and 48 hours
67
        $users_reg_24 = $memberHandler->getUserCount($criteria24->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '>=')), 'AND');
0 ignored issues
show
Unused Code introduced by
The call to XoopsMemberHandler::getUserCount() has too many arguments starting with 'AND'. ( Ignorable by Annotation )

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

67
        /** @scrutinizer ignore-call */ 
68
        $users_reg_24 = $memberHandler->getUserCount($criteria24->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (24 * 3600)), '>=')), 'AND');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
68
        $users_reg_48 = $memberHandler->getUserCount($criteria48->add(new \Criteria('user_regdate', (mktime(0, 0, 0) - (48 * 3600)), '>=')), 'AND');
69
        $limit        = 1;
70
        $criteria->setOrder('DESC');
71
        $criteria->setSort('user_regdate');
72
        $criteria->setLimit($limit);
73
        $lastmembers  = $memberHandler->getUsers($criteria);
74
        $lastusername = $lastmembers[0]->getVar('uname');
75
        $lastrealname = $lastmembers[0]->getVar('name');
76
        $lastid       = $lastmembers[0]->getVar('uid');
77
78
        //Total Post Count
79
        $sql                 = 'SELECT SUM(posts) AS totalposts FROM ' . $GLOBALS['xoopsDB']->prefix('users') . ' WHERE level > 0';
80
        $result              = $GLOBALS['xoopsDB']->query($sql);
81
        $myrow               = $GLOBALS['xoopsDB']->fetchArray($result);
82
        $block['totalposts'] = $myrow['totalposts']. ' ' . _MB_XOOPSMEMBERS_TOTALPOSTS;
83
		 
84
85
        // data
86
        $block['activeusers']    = $total_active_users. ' ' . _MB_XOOPSMEMBERS_REGISTEREDMEMBERS;
87
        $block['todayreg']       = $users_reg_24   . ' ' . _MB_XOOPSMEMBERS_REGISTEREDTODAY . ' ' . _MB_XOOPSMEMBERS_AND;
88
        $block['yesterdayreg']   = $users_reg_48 - $users_reg_24    . ' ' . _MB_XOOPSMEMBERS_REGISTEREDYESTERDAY;
89
        $block['online_names']   = $members;
90
        $block['online_members'] = $total - $guests  . ' ' . _MB_XOOPSMEMBERS_MEMBERS . ' ' . _MB_XOOPSMEMBERS_CURRENTONLINE;
91
        $block['online_guests']  = $guests . ' ' . _MB_XOOPSMEMBERS_GUESTS . ' ' . _MB_XOOPSMEMBERS_AND;
92
        $block['lang_more']      = _MB_XOOPSMEMBERS_MORE;
93
94
        $block['total_online'] = $total;
95
96
        if ('1' == $options[4] && '' != $lastrealname) {
97
            $block['latestmember'] = $lastrealname;
98
        } else {
99
            $block['latestmember'] = $lastusername;
100
        }
101
        $block['latest_id'] = $lastid;
102
103
        // Language Definition
104
        $block['membership_lang']          = _MB_XOOPSMEMBERS_MEMBERSHIP;
105
        $block['newestmember_lang']        = _MB_XOOPSMEMBERS_NEWESTMEMBER;
106
        $block['showtotalpost']            = $options[0];
107
        $block['showtotalonline']          = $options[1];
108
        $block['showreghistory']           = $options[2];
109
        $block['shownewmember']            = $options[3];
110
        $block['userealname']              = $options[4];
111
112
        return $block;
113
    } else {
114
        return false;
115
    }
116
}
117
118
/**
119
 * @param $options
120
 * @return string
121
 */
122
function membersstats_edit($options)
123
{
124
    $form = _MB_XOOPSMEMBERS_SHOWTOTALPOST . '&nbsp;';
125
    if (1 == $options[0]) {
126
        $chk = " checked";
127
    }
128
    $form .= "<input type='radio' name='options[0]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $chk does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
    $chk  = '';
130
    if (0 == $options[0]) {
131
        $chk = " checked";
132
    }
133
    $form .= "&nbsp;<input type='radio' name='options[0]' value='0'" . $chk . ' >' . _NO . '<br>';
134
135
    $form .= _MB_XOOPSMEMBERS_SHOWTOTALONLINE . '&nbsp;';
136
    if (1 == $options[1]) {
137
        $chk = " checked";
138
    }
139
    $form .= "<input type='radio' name='options[1]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
140
    $chk  = '';
141
    if (0 == $options[1]) {
142
        $chk = " checked";
143
    }
144
    $form .= "&nbsp;<input type='radio' name='options[1]' value='0'" . $chk . ' >' . _NO . '<br>';
145
146
    $form .= _MB_XOOPSMEMBERS_SHOWREGHISTORY . '&nbsp;';
147
    if (1 == $options[2]) {
148
        $chk = " checked";
149
    }
150
    $form .= "<input type='radio' name='options[2]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
151
    $chk  = '';
152
    if (0 == $options[2]) {
153
        $chk = " checked";
154
    }
155
    $form .= "&nbsp;<input type='radio' name='options[2]' value='0'" . $chk . ' >' . _NO . '<br>';
156
157
    $form .= _MB_XOOPSMEMBERS_SHOWNEWMEMBER . '&nbsp;';
158
    if (1 == $options[3]) {
159
        $chk = " checked";
160
    }
161
    $form .= "<input type='radio' name='options[3]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
162
    $chk  = '';
163
    if (0 == $options[3]) {
164
        $chk = " checked";
165
    }
166
    $form .= "&nbsp;<input type='radio' name='options[3]' value='0'" . $chk . ' >' . _NO . '<br>';
167
168
    $form .= _MB_XOOPSMEMBERS_USEREALNAME . '&nbsp;';
169
    if (1 == $options[4]) {
170
        $chk = " checked";
171
    }
172
    $form .= "<input type='radio' name='options[4]' value='1'" . $chk . ' >&nbsp;' . _YES . '';
173
    $chk  = '';
174
    if (0 == $options[4]) {
175
        $chk = " checked";
176
    }
177
    $form .= "&nbsp;<input type='radio' name='options[4]' value='0'" . $chk . ' >' . _NO . '<br>';
178
179
    return $form;
180
}
181