| Conditions | 33 |
| Paths | 18464 |
| Total Lines | 160 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 |
||
| 187 | public function setup($parameters, $id) |
||
| 188 | { |
||
| 189 | global $context, $scripturl; |
||
| 190 | |||
| 191 | // Standard Variables |
||
| 192 | $type = !empty($parameters['type']) ? $parameters['type'] : 0; |
||
| 193 | $limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 5; |
||
| 194 | $sort_asc = !empty($parameters['sort_asc']); |
||
| 195 | |||
| 196 | // Time is in days, but we need seconds |
||
| 197 | $last_active_limit = !empty($parameters['last_active_limit']) ? $parameters['last_active_limit'] * 86400 : 0; |
||
| 198 | $this->data['enable_label'] = !empty($parameters['enable_label']); |
||
| 199 | $this->data['list_label'] = !empty($parameters['list_label']) ? $parameters['list_label'] : ''; |
||
| 200 | |||
| 201 | // Setup current block type |
||
| 202 | $current_system = $this->sp_topStatsSystem[$type]; |
||
| 203 | |||
| 204 | // Possible to output? |
||
| 205 | if (empty($current_system['enabled'])) |
||
| 206 | { |
||
| 207 | if (!empty($current_system['error_msg'])) |
||
| 208 | { |
||
| 209 | $this->setTemplate('template_sp_topStatsMember_error'); |
||
| 210 | $this->data['error_msg'] = $current_system['error_msg']; |
||
| 211 | } |
||
| 212 | |||
| 213 | return; |
||
| 214 | } |
||
| 215 | |||
| 216 | // Sort in reverse? |
||
| 217 | $sort_asc = !empty($current_system['reverse']) ? !$sort_asc : $sort_asc; |
||
| 218 | |||
| 219 | // Build the where statement |
||
| 220 | $where = array(); |
||
| 221 | |||
| 222 | // If this is already cached, use it |
||
| 223 | $chache_id = 'sp_chache_' . $id . '_topStatsMember'; |
||
| 224 | if (empty($this->_modSettings['sp_disableChache']) && !empty($this->_modSettings[$chache_id])) |
||
| 225 | { |
||
| 226 | $data = explode(';', $this->_modSettings[$chache_id]); |
||
| 227 | |||
| 228 | if ($data[0] == $type && $data[1] == $limit && !empty($data[2]) == $sort_asc && $data[3] > time() - 300) // 5 Minute cache |
||
| 229 | { |
||
| 230 | $where[] = 'mem.id_member IN (' . $data[4] . ')'; |
||
| 231 | } |
||
| 232 | else |
||
| 233 | { |
||
| 234 | unset($this->_modSettings[$chache_id]); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | // Last active remove |
||
| 239 | if (!empty($last_active_limit)) |
||
| 240 | { |
||
| 241 | $timeLimit = time() - $last_active_limit; |
||
| 242 | $where[] = "last_login > $timeLimit"; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!empty($current_system['where'])) |
||
| 246 | { |
||
| 247 | $where[] = $current_system['where']; |
||
| 248 | } |
||
| 249 | |||
| 250 | if (!empty($where)) |
||
| 251 | { |
||
| 252 | $where = 'WHERE (' . implode(') |
||
| 253 | AND (', $where) . ')'; |
||
| 254 | } |
||
| 255 | else |
||
| 256 | { |
||
| 257 | $where = ""; |
||
| 258 | } |
||
| 259 | |||
| 260 | // Finally make the query with the parameters we built |
||
| 261 | $request = $this->_db->query('', ' |
||
| 262 | SELECT |
||
| 263 | mem.id_member, mem.real_name, mem.avatar, mem.email_address, |
||
| 264 | a.id_attach, a.attachment_type, a.filename, |
||
| 265 | {raw:field} |
||
| 266 | FROM {db_prefix}members as mem |
||
| 267 | LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member) |
||
| 268 | {raw:where} |
||
| 269 | ORDER BY {raw:order} {raw:sort} |
||
| 270 | LIMIT {int:limit}', |
||
| 271 | array( |
||
| 272 | // Prevent delete of user if the cache was available |
||
| 273 | 'limit' => isset($context['common_stats']['total_members']) && $context['common_stats']['total_members'] > 100 ? ($limit + 5) : $limit, |
||
| 274 | 'field' => $current_system['field'], |
||
| 275 | 'where' => $where, |
||
| 276 | 'order' => $current_system['order'], |
||
| 277 | 'sort' => ($sort_asc ? 'ASC' : 'DESC'), |
||
| 278 | ) |
||
| 279 | ); |
||
| 280 | $this->data['members'] = array(); |
||
| 281 | $count = 1; |
||
| 282 | $chache_member_ids = array(); |
||
| 283 | while ($row = $this->_db->fetch_assoc($request)) |
||
| 284 | { |
||
| 285 | // Collect some to cache data |
||
| 286 | $chache_member_ids[$row['id_member']] = $row['id_member']; |
||
| 287 | if ($count++ > $limit) |
||
| 288 | { |
||
| 289 | continue; |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->color_ids[$row['id_member']] = $row['id_member']; |
||
| 293 | |||
| 294 | // Setup the row |
||
| 295 | $output = ''; |
||
| 296 | |||
| 297 | // Prepare some data of the row? |
||
| 298 | if (!empty($current_system['output_function'])) |
||
| 299 | { |
||
| 300 | $current_system['output_function']($row); |
||
| 301 | } |
||
| 302 | |||
| 303 | if (!empty($current_system['output_text'])) |
||
| 304 | { |
||
| 305 | $output = $current_system['output_text']; |
||
| 306 | foreach ($row as $item => $replacewith) |
||
| 307 | { |
||
| 308 | $output = str_replace('%' . $item . '%', $replacewith, $output); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | $this->data['members'][] = array( |
||
| 313 | 'id' => $row['id_member'], |
||
| 314 | 'name' => $row['real_name'], |
||
| 315 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 316 | 'link' => '<a style="font-size: 90%" href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>', |
||
| 317 | 'avatar' => determineAvatar(array( |
||
| 318 | 'avatar' => $row['avatar'], |
||
| 319 | 'filename' => $row['filename'], |
||
| 320 | 'id_attach' => $row['id_attach'], |
||
| 321 | 'email_address' => $row['email_address'], |
||
| 322 | 'attachment_type' => $row['attachment_type'], |
||
| 323 | )), |
||
| 324 | 'output' => $output, |
||
| 325 | 'complete_row' => $row, |
||
| 326 | ); |
||
| 327 | } |
||
| 328 | $this->_db->free_result($request); |
||
| 329 | |||
| 330 | // Update the cache, at least around 100 members are needed for a good working version |
||
| 331 | if (empty($this->_modSettings['sp_disableChache']) && isset($context['common_stats']['total_members']) && $context['common_stats']['total_members'] > 0 && !empty($chache_member_ids) && count($chache_member_ids) > $limit && empty($this->_modSettings[$chache_id])) |
||
| 332 | { |
||
| 333 | $toCache = array($type, $limit, ($sort_asc ? 1 : 0), time(), implode(',', $chache_member_ids)); |
||
| 334 | updateSettings(array($chache_id => implode(';', $toCache))); |
||
| 335 | } |
||
| 336 | // One time error, if this happens the cache needs an update |
||
| 337 | elseif (!empty($this->_modSettings[$chache_id])) |
||
| 338 | { |
||
| 339 | updateSettings(array($chache_id => '0;0;0;1000;0')); |
||
| 340 | } |
||
| 341 | |||
| 342 | // Color the id's |
||
| 343 | $this->_colorids(); |
||
| 344 | |||
| 345 | // Set the template to use |
||
| 346 | $this->setTemplate('template_sp_topStatsMember'); |
||
| 347 | } |
||
| 429 |