| Conditions | 73 |
| Paths | > 20000 |
| Total Lines | 291 |
| Code Lines | 152 |
| Lines | 14 |
| Ratio | 4.81 % |
| 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 |
||
| 359 | function list_getModLogEntries($start, $items_per_page, $sort, $query_string = '', $query_params = array(), $log_type = 1, $ignore_boards = false) |
||
| 360 | { |
||
| 361 | global $scripturl, $txt, $smcFunc, $user_info; |
||
| 362 | |||
| 363 | $modlog_query = allowedTo('admin_forum') || $user_info['mod_cache']['bq'] == '1=1' ? '1=1' : (($user_info['mod_cache']['bq'] == '0=1' || $ignore_boards) ? 'lm.id_board = 0 AND lm.id_topic = 0' : (strtr($user_info['mod_cache']['bq'], array('id_board' => 'b.id_board')) . ' AND ' . strtr($user_info['mod_cache']['bq'], array('id_board' => 't.id_board')))); |
||
| 364 | |||
| 365 | // Can they see the IP address? |
||
| 366 | $seeIP = allowedTo('moderate_forum'); |
||
| 367 | |||
| 368 | // Here we have the query getting the log details. |
||
| 369 | $result = $smcFunc['db_query']('', ' |
||
| 370 | SELECT |
||
| 371 | lm.id_action, lm.id_member, lm.ip, lm.log_time, lm.action, lm.id_board, lm.id_topic, lm.id_msg, lm.extra, |
||
| 372 | mem.real_name, mg.group_name |
||
| 373 | FROM {db_prefix}log_actions AS lm |
||
| 374 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lm.id_member) |
||
| 375 | LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_group_id} THEN mem.id_post_group ELSE mem.id_group END) |
||
| 376 | LEFT JOIN {db_prefix}boards AS b ON (b.id_board = lm.id_board) |
||
| 377 | LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = lm.id_topic) |
||
| 378 | WHERE id_log = {int:log_type} |
||
| 379 | AND {raw:modlog_query}' |
||
| 380 | . (!empty($query_string) ? ' |
||
| 381 | AND ' . $query_string : '') . ' |
||
| 382 | ORDER BY {raw:sort} |
||
| 383 | LIMIT {int:start}, {int:max}', |
||
| 384 | array_merge($query_params, array( |
||
| 385 | 'reg_group_id' => 0, |
||
| 386 | 'log_type' => $log_type, |
||
| 387 | 'modlog_query' => $modlog_query, |
||
| 388 | 'sort' => $sort, |
||
| 389 | 'start' => $start, |
||
| 390 | 'max' => $items_per_page, |
||
| 391 | )) |
||
| 392 | ); |
||
| 393 | |||
| 394 | // Arrays for decoding objects into. |
||
| 395 | $topics = array(); |
||
| 396 | $boards = array(); |
||
| 397 | $members = array(); |
||
| 398 | $messages = array(); |
||
| 399 | $entries = array(); |
||
| 400 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
| 401 | { |
||
| 402 | $row['extra'] = $smcFunc['json_decode']($row['extra'], true); |
||
| 403 | |||
| 404 | // Corrupt? |
||
| 405 | $row['extra'] = is_array($row['extra']) ? $row['extra'] : array(); |
||
| 406 | |||
| 407 | // Add on some of the column stuff info |
||
| 408 | if (!empty($row['id_board'])) |
||
| 409 | { |
||
| 410 | if ($row['action'] == 'move') |
||
| 411 | $row['extra']['board_to'] = $row['id_board']; |
||
| 412 | else |
||
| 413 | $row['extra']['board'] = $row['id_board']; |
||
| 414 | } |
||
| 415 | |||
| 416 | if (!empty($row['id_topic'])) |
||
| 417 | $row['extra']['topic'] = $row['id_topic']; |
||
| 418 | if (!empty($row['id_msg'])) |
||
| 419 | $row['extra']['message'] = $row['id_msg']; |
||
| 420 | |||
| 421 | // Is this associated with a topic? |
||
| 422 | View Code Duplication | if (isset($row['extra']['topic'])) |
|
| 423 | $topics[(int) $row['extra']['topic']][] = $row['id_action']; |
||
| 424 | View Code Duplication | if (isset($row['extra']['new_topic'])) |
|
| 425 | $topics[(int) $row['extra']['new_topic']][] = $row['id_action']; |
||
| 426 | |||
| 427 | // How about a member? |
||
| 428 | if (isset($row['extra']['member'])) |
||
| 429 | { |
||
| 430 | // Guests don't have names! |
||
| 431 | if (empty($row['extra']['member'])) |
||
| 432 | $row['extra']['member'] = $txt['modlog_parameter_guest']; |
||
| 433 | else |
||
| 434 | { |
||
| 435 | // Try to find it... |
||
| 436 | $members[(int) $row['extra']['member']][] = $row['id_action']; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | // Associated with a board? |
||
| 441 | View Code Duplication | if (isset($row['extra']['board_to'])) |
|
| 442 | $boards[(int) $row['extra']['board_to']][] = $row['id_action']; |
||
| 443 | View Code Duplication | if (isset($row['extra']['board_from'])) |
|
| 444 | $boards[(int) $row['extra']['board_from']][] = $row['id_action']; |
||
| 445 | View Code Duplication | if (isset($row['extra']['board'])) |
|
| 446 | $boards[(int) $row['extra']['board']][] = $row['id_action']; |
||
| 447 | |||
| 448 | // A message? |
||
| 449 | if (isset($row['extra']['message'])) |
||
| 450 | $messages[(int) $row['extra']['message']][] = $row['id_action']; |
||
| 451 | |||
| 452 | // IP Info? |
||
| 453 | if (isset($row['extra']['ip_range'])) |
||
| 454 | if ($seeIP) |
||
| 455 | $row['extra']['ip_range'] = '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['extra']['ip_range'] . '">' . $row['extra']['ip_range'] . '</a>'; |
||
| 456 | else |
||
| 457 | $row['extra']['ip_range'] = $txt['logged']; |
||
| 458 | |||
| 459 | // Email? |
||
| 460 | if (isset($row['extra']['email'])) |
||
| 461 | $row['extra']['email'] = '<a href="mailto:' . $row['extra']['email'] . '">' . $row['extra']['email'] . '</a>'; |
||
| 462 | |||
| 463 | // Bans are complex. |
||
| 464 | if ($row['action'] == 'ban' || $row['action'] == 'banremove') |
||
| 465 | { |
||
| 466 | $row['action_text'] = $txt['modlog_ac_ban' . ($row['action'] == 'banremove' ? '_remove' : '')]; |
||
| 467 | foreach (array('member', 'email', 'ip_range', 'hostname') as $type) |
||
| 468 | if (isset($row['extra'][$type])) |
||
| 469 | $row['action_text'] .= $txt['modlog_ac_ban_trigger_' . $type]; |
||
| 470 | } |
||
| 471 | |||
| 472 | // The array to go to the template. Note here that action is set to a "default" value of the action doesn't match anything in the descriptions. Allows easy adding of logging events with basic details. |
||
| 473 | $entries[$row['id_action']] = array( |
||
| 474 | 'id' => $row['id_action'], |
||
| 475 | 'ip' => $seeIP ? inet_dtop($row['ip']) : $txt['logged'], |
||
| 476 | 'position' => empty($row['real_name']) && empty($row['group_name']) ? $txt['guest'] : $row['group_name'], |
||
| 477 | 'moderator_link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>' : (empty($row['real_name']) ? ($txt['guest'] . (!empty($row['extra']['member_acted']) ? ' (' . $row['extra']['member_acted'] . ')' : '')) : $row['real_name']), |
||
| 478 | 'time' => timeformat($row['log_time']), |
||
| 479 | 'timestamp' => forum_time(true, $row['log_time']), |
||
| 480 | 'editable' => substr($row['action'], 0, 8) !== 'clearlog', |
||
| 481 | 'extra' => $row['extra'], |
||
| 482 | 'action' => $row['action'], |
||
| 483 | 'action_text' => isset($row['action_text']) ? $row['action_text'] : '', |
||
| 484 | ); |
||
| 485 | } |
||
| 486 | $smcFunc['db_free_result']($result); |
||
| 487 | |||
| 488 | if (!empty($boards)) |
||
| 489 | { |
||
| 490 | $request = $smcFunc['db_query']('', ' |
||
| 491 | SELECT id_board, name |
||
| 492 | FROM {db_prefix}boards |
||
| 493 | WHERE id_board IN ({array_int:board_list}) |
||
| 494 | LIMIT {int:limit}', |
||
| 495 | array( |
||
| 496 | 'board_list' => array_keys($boards), |
||
| 497 | 'limit' => count(array_keys($boards)), |
||
| 498 | ) |
||
| 499 | ); |
||
| 500 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 501 | { |
||
| 502 | foreach ($boards[$row['id_board']] as $action) |
||
| 503 | { |
||
| 504 | // Make the board number into a link - dealing with moving too. |
||
| 505 | if (isset($entries[$action]['extra']['board_to']) && $entries[$action]['extra']['board_to'] == $row['id_board']) |
||
| 506 | $entries[$action]['extra']['board_to'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; |
||
| 507 | View Code Duplication | elseif (isset($entries[$action]['extra']['board_from']) && $entries[$action]['extra']['board_from'] == $row['id_board']) |
|
| 508 | $entries[$action]['extra']['board_from'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; |
||
| 509 | View Code Duplication | elseif (isset($entries[$action]['extra']['board']) && $entries[$action]['extra']['board'] == $row['id_board']) |
|
| 510 | $entries[$action]['extra']['board'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | $smcFunc['db_free_result']($request); |
||
| 514 | } |
||
| 515 | |||
| 516 | if (!empty($topics)) |
||
| 517 | { |
||
| 518 | $request = $smcFunc['db_query']('', ' |
||
| 519 | SELECT ms.subject, t.id_topic |
||
| 520 | FROM {db_prefix}topics AS t |
||
| 521 | INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg) |
||
| 522 | WHERE t.id_topic IN ({array_int:topic_list}) |
||
| 523 | LIMIT {int:limit}', |
||
| 524 | array( |
||
| 525 | 'topic_list' => array_keys($topics), |
||
| 526 | 'limit' => count(array_keys($topics)), |
||
| 527 | ) |
||
| 528 | ); |
||
| 529 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 530 | { |
||
| 531 | foreach ($topics[$row['id_topic']] as $action) |
||
| 532 | { |
||
| 533 | $this_action = &$entries[$action]; |
||
| 534 | |||
| 535 | // This isn't used in the current theme. |
||
| 536 | $this_action['topic'] = array( |
||
| 537 | 'id' => $row['id_topic'], |
||
| 538 | 'subject' => $row['subject'], |
||
| 539 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0', |
||
| 540 | 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>' |
||
| 541 | ); |
||
| 542 | |||
| 543 | // Make the topic number into a link - dealing with splitting too. |
||
| 544 | if (isset($this_action['extra']['topic']) && $this_action['extra']['topic'] == $row['id_topic']) |
||
| 545 | $this_action['extra']['topic'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . (isset($this_action['extra']['message']) ? 'msg' . $this_action['extra']['message'] . '#msg' . $this_action['extra']['message'] : '0') . '">' . $row['subject'] . '</a>'; |
||
| 546 | elseif (isset($this_action['extra']['new_topic']) && $this_action['extra']['new_topic'] == $row['id_topic']) |
||
| 547 | $this_action['extra']['new_topic'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . (isset($this_action['extra']['message']) ? 'msg' . $this_action['extra']['message'] . '#msg' . $this_action['extra']['message'] : '0') . '">' . $row['subject'] . '</a>'; |
||
| 548 | } |
||
| 549 | } |
||
| 550 | $smcFunc['db_free_result']($request); |
||
| 551 | } |
||
| 552 | |||
| 553 | if (!empty($messages)) |
||
| 554 | { |
||
| 555 | $request = $smcFunc['db_query']('', ' |
||
| 556 | SELECT id_msg, subject |
||
| 557 | FROM {db_prefix}messages |
||
| 558 | WHERE id_msg IN ({array_int:message_list}) |
||
| 559 | LIMIT {int:limit}', |
||
| 560 | array( |
||
| 561 | 'message_list' => array_keys($messages), |
||
| 562 | 'limit' => count(array_keys($messages)), |
||
| 563 | ) |
||
| 564 | ); |
||
| 565 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 566 | { |
||
| 567 | foreach ($messages[$row['id_msg']] as $action) |
||
| 568 | { |
||
| 569 | $this_action = &$entries[$action]; |
||
| 570 | |||
| 571 | // This isn't used in the current theme. |
||
| 572 | $this_action['message'] = array( |
||
| 573 | 'id' => $row['id_msg'], |
||
| 574 | 'subject' => $row['subject'], |
||
| 575 | 'href' => $scripturl . '?msg=' . $row['id_msg'], |
||
| 576 | 'link' => '<a href="' . $scripturl . '?msg=' . $row['id_msg'] . '">' . $row['subject'] . '</a>', |
||
| 577 | ); |
||
| 578 | |||
| 579 | // Make the message number into a link. |
||
| 580 | if (isset($this_action['extra']['message']) && $this_action['extra']['message'] == $row['id_msg']) |
||
| 581 | $this_action['extra']['message'] = '<a href="' . $scripturl . '?msg=' . $row['id_msg'] . '">' . $row['subject'] . '</a>'; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | $smcFunc['db_free_result']($request); |
||
| 585 | } |
||
| 586 | |||
| 587 | if (!empty($members)) |
||
| 588 | { |
||
| 589 | $request = $smcFunc['db_query']('', ' |
||
| 590 | SELECT real_name, id_member |
||
| 591 | FROM {db_prefix}members |
||
| 592 | WHERE id_member IN ({array_int:member_list}) |
||
| 593 | LIMIT {int:limit}', |
||
| 594 | array( |
||
| 595 | 'member_list' => array_keys($members), |
||
| 596 | 'limit' => count(array_keys($members)), |
||
| 597 | ) |
||
| 598 | ); |
||
| 599 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 600 | { |
||
| 601 | foreach ($members[$row['id_member']] as $action) |
||
| 602 | { |
||
| 603 | // Not used currently. |
||
| 604 | $entries[$action]['member'] = array( |
||
| 605 | 'id' => $row['id_member'], |
||
| 606 | 'name' => $row['real_name'], |
||
| 607 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 608 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>' |
||
| 609 | ); |
||
| 610 | // Make the member number into a name. |
||
| 611 | $entries[$action]['extra']['member'] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>'; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | $smcFunc['db_free_result']($request); |
||
| 615 | } |
||
| 616 | |||
| 617 | // Do some formatting of the action string. |
||
| 618 | foreach ($entries as $k => $entry) |
||
| 619 | { |
||
| 620 | // Make any message info links so its easier to go find that message. |
||
| 621 | if (isset($entry['extra']['message']) && (empty($entry['message']) || empty($entry['message']['id']))) |
||
| 622 | $entries[$k]['extra']['message'] = '<a href="' . $scripturl . '?msg=' . $entry['extra']['message'] . '">' . $entry['extra']['message'] . '</a>'; |
||
| 623 | |||
| 624 | // Mark up any deleted members, topics and boards. |
||
| 625 | foreach (array('board', 'board_from', 'board_to', 'member', 'topic', 'new_topic') as $type) |
||
| 626 | if (!empty($entry['extra'][$type]) && is_numeric($entry['extra'][$type])) |
||
| 627 | $entries[$k]['extra'][$type] = sprintf($txt['modlog_id'], $entry['extra'][$type]); |
||
| 628 | |||
| 629 | if (isset($entry['extra']['report'])) |
||
| 630 | { |
||
| 631 | // Member profile reports go in a different area |
||
| 632 | if (stristr($entry['action'], 'user_report')) |
||
| 633 | $entries[$k]['extra']['report'] = '<a href="' . $scripturl . '?action=moderate;area=reportedmembers;sa=details;rid=' . $entry['extra']['report'] . '">' . $txt['modlog_report'] . '</a>'; |
||
| 634 | else |
||
| 635 | $entries[$k]['extra']['report'] = '<a href="' . $scripturl . '?action=moderate;area=reportedposts;sa=details;rid=' . $entry['extra']['report'] . '">' . $txt['modlog_report'] . '</a>'; |
||
| 636 | } |
||
| 637 | |||
| 638 | if (empty($entries[$k]['action_text'])) |
||
| 639 | $entries[$k]['action_text'] = isset($txt['modlog_ac_' . $entry['action']]) ? $txt['modlog_ac_' . $entry['action']] : $entry['action']; |
||
| 640 | $entries[$k]['action_text'] = preg_replace_callback('~\{([A-Za-z\d_]+)\}~i', |
||
| 641 | function ($matches) use ($entries, $k) |
||
| 642 | { |
||
| 643 | return isset($entries[$k]['extra'][$matches[1]]) ? $entries[$k]['extra'][$matches[1]] : ''; |
||
| 644 | }, $entries[$k]['action_text']); |
||
| 645 | } |
||
| 646 | |||
| 647 | // Back we go! |
||
| 648 | return $entries; |
||
| 649 | } |
||
| 650 | |||
| 651 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.