| Conditions | 10 |
| Paths | 120 |
| Total Lines | 103 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 52 | public function setup($parameters, $id) |
||
| 53 | { |
||
| 54 | global $txt, $scripturl; |
||
| 55 | |||
| 56 | $limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 5; |
||
| 57 | $type = !empty($parameters['type']) ? (int) $parameters['type'] : 0; |
||
| 58 | |||
| 59 | // If not top poster of all time we need to set a start time |
||
| 60 | if (!empty($type)) |
||
| 61 | { |
||
| 62 | $start_time = time(); |
||
| 63 | |||
| 64 | // Today |
||
| 65 | if ($type == 1) |
||
| 66 | { |
||
| 67 | list($year, $month, $day) = explode('-', date('Y-m-d')); |
||
| 68 | $start_time = mktime(0, 0, 0, $month, $day, $year); |
||
| 69 | } |
||
| 70 | // This week |
||
| 71 | elseif ($type == 2) |
||
| 72 | { |
||
| 73 | $start_time = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N") * 3600 * 24); |
||
| 74 | } |
||
| 75 | // This month |
||
| 76 | elseif ($type == 3) |
||
| 77 | { |
||
| 78 | $months = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
||
| 79 | $start_time = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (3600 * 24 * $months[(int) date("m", time())]); |
||
| 80 | } |
||
| 81 | |||
| 82 | $start_time = forum_time(false, $start_time); |
||
| 83 | |||
| 84 | $request = $this->_db->query('', ' |
||
| 85 | SELECT |
||
| 86 | mem.id_member, mem.real_name, COUNT(*) as posts, mem.email_address, |
||
| 87 | mem.avatar, a.id_attach, a.attachment_type, a.filename |
||
| 88 | FROM {db_prefix}messages AS m |
||
| 89 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) |
||
| 90 | LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member) |
||
| 91 | WHERE m.poster_time > {int:start_time} |
||
| 92 | AND m.id_member != 0 |
||
| 93 | GROUP BY mem.id_member |
||
| 94 | ORDER BY posts DESC |
||
| 95 | LIMIT {int:limit}', |
||
| 96 | array( |
||
| 97 | 'start_time' => $start_time, |
||
| 98 | 'limit' => $limit, |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | // Or from the start of time |
||
| 103 | else |
||
| 104 | { |
||
| 105 | $request = $this->_db->query('', ' |
||
| 106 | SELECT |
||
| 107 | m.id_member, m.real_name, m.posts, m.avatar, m.email_address, |
||
| 108 | a.id_attach, a.attachment_type, a.filename |
||
| 109 | FROM {db_prefix}members AS m |
||
| 110 | LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = m.id_member) |
||
| 111 | ORDER BY posts DESC |
||
| 112 | LIMIT {int:limit}', |
||
| 113 | array( |
||
| 114 | 'limit' => $limit, |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | $this->data['members'] = array(); |
||
| 119 | while ($row = $this->_db->fetch_assoc($request)) |
||
| 120 | { |
||
| 121 | if (!empty($row['id_member'])) |
||
| 122 | { |
||
| 123 | $this->color_ids[$row['id_member']] = $row['id_member']; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Load the member data |
||
| 127 | $this->data['members'][] = array( |
||
| 128 | 'id' => $row['id_member'], |
||
| 129 | 'name' => $row['real_name'], |
||
| 130 | 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 131 | 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>', |
||
| 132 | 'posts' => comma_format($row['posts']), |
||
| 133 | 'avatar' => determineAvatar(array( |
||
| 134 | 'avatar' => $row['avatar'], |
||
| 135 | 'filename' => $row['filename'], |
||
| 136 | 'id_attach' => $row['id_attach'], |
||
| 137 | 'email_address' => $row['email_address'], |
||
| 138 | 'attachment_type' => $row['attachment_type'], |
||
| 139 | )), |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | $this->_db->free_result($request); |
||
| 143 | |||
| 144 | // Profile colors? |
||
| 145 | $this->_color_ids(); |
||
| 146 | |||
| 147 | if (empty($this->data['members'])) |
||
| 148 | { |
||
| 149 | $this->data['error_msg'] = $txt['error_sp_no_members_found']; |
||
| 150 | $this->setTemplate('template_sp_topPoster_error'); |
||
| 151 | } |
||
| 152 | else |
||
| 153 | { |
||
| 154 | $this->setTemplate('template_sp_topPoster'); |
||
| 155 | } |
||
| 220 |