| Conditions | 28 |
| Paths | 7560 |
| Total Lines | 162 |
| Code Lines | 89 |
| Lines | 6 |
| Ratio | 3.7 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 27 | function getMembersOnlineStats($membersOnlineOptions) |
||
| 28 | { |
||
| 29 | global $smcFunc, $scripturl, $user_info, $modSettings, $txt; |
||
| 30 | |||
| 31 | // The list can be sorted in several ways. |
||
| 32 | $allowed_sort_options = array( |
||
| 33 | '', // No sorting. |
||
| 34 | 'log_time', |
||
| 35 | 'real_name', |
||
| 36 | 'show_online', |
||
| 37 | 'online_color', |
||
| 38 | 'group_name', |
||
| 39 | ); |
||
| 40 | // Default the sorting method to 'most recent online members first'. |
||
| 41 | if (!isset($membersOnlineOptions['sort'])) |
||
| 42 | { |
||
| 43 | $membersOnlineOptions['sort'] = 'log_time'; |
||
| 44 | $membersOnlineOptions['reverse_sort'] = true; |
||
| 45 | } |
||
| 46 | |||
| 47 | // Not allowed sort method? Bang! Error! |
||
| 48 | elseif (!in_array($membersOnlineOptions['sort'], $allowed_sort_options)) |
||
| 49 | trigger_error('Sort method for getMembersOnlineStats() function is not allowed', E_USER_NOTICE); |
||
| 50 | |||
| 51 | // Initialize the array that'll be returned later on. |
||
| 52 | $membersOnlineStats = array( |
||
| 53 | 'users_online' => array(), |
||
| 54 | 'list_users_online' => array(), |
||
| 55 | 'online_groups' => array(), |
||
| 56 | 'num_guests' => 0, |
||
| 57 | 'num_spiders' => 0, |
||
| 58 | 'num_buddies' => 0, |
||
| 59 | 'num_users_hidden' => 0, |
||
| 60 | 'num_users_online' => 0, |
||
| 61 | ); |
||
| 62 | |||
| 63 | // Get any spiders if enabled. |
||
| 64 | $spiders = array(); |
||
| 65 | $spider_finds = array(); |
||
| 66 | if (!empty($modSettings['show_spider_online']) && ($modSettings['show_spider_online'] < 3 || allowedTo('admin_forum')) && !empty($modSettings['spider_name_cache'])) |
||
| 67 | $spiders = $smcFunc['json_decode']($modSettings['spider_name_cache'], true); |
||
| 68 | |||
| 69 | // Load the users online right now. |
||
| 70 | $request = $smcFunc['db_query']('', ' |
||
| 71 | SELECT |
||
| 72 | lo.id_member, lo.log_time, lo.id_spider, mem.real_name, mem.member_name, mem.show_online, |
||
| 73 | mg.online_color, mg.id_group, mg.group_name |
||
| 74 | FROM {db_prefix}log_online AS lo |
||
| 75 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member) |
||
| 76 | LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_mem_group} THEN mem.id_post_group ELSE mem.id_group END)', |
||
| 77 | array( |
||
| 78 | 'reg_mem_group' => 0, |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 82 | { |
||
| 83 | if (empty($row['real_name'])) |
||
| 84 | { |
||
| 85 | // Do we think it's a spider? |
||
| 86 | if ($row['id_spider'] && isset($spiders[$row['id_spider']])) |
||
| 87 | { |
||
| 88 | $spider_finds[$row['id_spider']] = isset($spider_finds[$row['id_spider']]) ? $spider_finds[$row['id_spider']] + 1 : 1; |
||
| 89 | $membersOnlineStats['num_spiders']++; |
||
| 90 | } |
||
| 91 | // Guests are only nice for statistics. |
||
| 92 | $membersOnlineStats['num_guests']++; |
||
| 93 | |||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | elseif (empty($row['show_online']) && empty($membersOnlineOptions['show_hidden'])) |
||
| 98 | { |
||
| 99 | // Just increase the stats and don't add this hidden user to any list. |
||
| 100 | $membersOnlineStats['num_users_hidden']++; |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Some basic color coding... |
||
| 105 | if (!empty($row['online_color'])) |
||
| 106 | $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>'; |
||
| 107 | else |
||
| 108 | $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>'; |
||
| 109 | |||
| 110 | // Buddies get counted and highlighted. |
||
| 111 | $is_buddy = in_array($row['id_member'], $user_info['buddies']); |
||
| 112 | if ($is_buddy) |
||
| 113 | { |
||
| 114 | $membersOnlineStats['num_buddies']++; |
||
| 115 | $link = '<strong>' . $link . '</strong>'; |
||
| 116 | } |
||
| 117 | |||
| 118 | // A lot of useful information for each member. |
||
| 119 | $membersOnlineStats['users_online'][$row[$membersOnlineOptions['sort']] . '_' . $row['member_name']] = array( |
||
| 120 | 'id' => $row['id_member'], |
||
| 121 | 'username' => $row['member_name'], |
||
| 122 | 'name' => $row['real_name'], |
||
| 123 | 'group' => $row['id_group'], |
||
| 124 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 125 | 'link' => $link, |
||
| 126 | 'is_buddy' => $is_buddy, |
||
| 127 | 'hidden' => empty($row['show_online']), |
||
| 128 | 'is_last' => false, |
||
| 129 | ); |
||
| 130 | |||
| 131 | // This is the compact version, simply implode it to show. |
||
| 132 | $membersOnlineStats['list_users_online'][$row[$membersOnlineOptions['sort']] . '_' . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link; |
||
| 133 | |||
| 134 | // Store all distinct (primary) membergroups that are shown. |
||
| 135 | View Code Duplication | if (!isset($membersOnlineStats['online_groups'][$row['id_group']])) |
|
| 136 | $membersOnlineStats['online_groups'][$row['id_group']] = array( |
||
| 137 | 'id' => $row['id_group'], |
||
| 138 | 'name' => $row['group_name'], |
||
| 139 | 'color' => $row['online_color'] |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | $smcFunc['db_free_result']($request); |
||
| 143 | |||
| 144 | // If there are spiders only and we're showing the detail, add them to the online list - at the bottom. |
||
| 145 | if (!empty($spider_finds) && $modSettings['show_spider_online'] > 1) |
||
| 146 | { |
||
| 147 | $sort = $membersOnlineOptions['sort'] === 'log_time' && $membersOnlineOptions['reverse_sort'] ? 0 : 'zzz_'; |
||
| 148 | foreach ($spider_finds as $id => $count) |
||
| 149 | { |
||
| 150 | $link = $spiders[$id] . ($count > 1 ? ' (' . $count . ')' : ''); |
||
| 151 | $membersOnlineStats['users_online'][$sort . '_' . $spiders[$id]] = array( |
||
| 152 | 'id' => 0, |
||
| 153 | 'username' => $spiders[$id], |
||
| 154 | 'name' => $link, |
||
| 155 | 'group' => $txt['spiders'], |
||
| 156 | 'href' => '', |
||
| 157 | 'link' => $link, |
||
| 158 | 'is_buddy' => false, |
||
| 159 | 'hidden' => false, |
||
| 160 | 'is_last' => false, |
||
| 161 | ); |
||
| 162 | $membersOnlineStats['list_users_online'][$sort . '_' . $spiders[$id]] = $link; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Time to sort the list a bit. |
||
| 167 | if (!empty($membersOnlineStats['users_online'])) |
||
| 168 | { |
||
| 169 | // Determine the sort direction. |
||
| 170 | $sortFunction = empty($membersOnlineOptions['reverse_sort']) ? 'ksort' : 'krsort'; |
||
| 171 | |||
| 172 | // Sort the two lists. |
||
| 173 | $sortFunction($membersOnlineStats['users_online']); |
||
| 174 | $sortFunction($membersOnlineStats['list_users_online']); |
||
| 175 | |||
| 176 | // Mark the last list item as 'is_last'. |
||
| 177 | $userKeys = array_keys($membersOnlineStats['users_online']); |
||
| 178 | $membersOnlineStats['users_online'][end($userKeys)]['is_last'] = true; |
||
| 179 | } |
||
| 180 | |||
| 181 | // Also sort the membergroups. |
||
| 182 | ksort($membersOnlineStats['online_groups']); |
||
| 183 | |||
| 184 | // Hidden and non-hidden members make up all online members. |
||
| 185 | $membersOnlineStats['num_users_online'] = count($membersOnlineStats['users_online']) + $membersOnlineStats['num_users_hidden'] - (isset($modSettings['show_spider_online']) && $modSettings['show_spider_online'] > 1 ? count($spider_finds) : 0); |
||
| 186 | |||
| 187 | return $membersOnlineStats; |
||
| 188 | } |
||
| 189 | |||
| 259 | ?> |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.