| Conditions | 12 |
| Paths | 18 |
| Total Lines | 81 |
| Code Lines | 47 |
| 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 |
||
| 25 | function getLastPosts($latestPostOptions) |
||
| 26 | { |
||
| 27 | global $scripturl, $modSettings, $smcFunc, $sourcedir; |
||
| 28 | |||
| 29 | // Find all the posts. Newer ones will have higher IDs. (assuming the last 20 * number are accessible...) |
||
| 30 | // @todo SLOW This query is now slow, NEEDS to be fixed. Maybe break into two? |
||
| 31 | $request = $smcFunc['db_query']('substring', ' |
||
| 32 | SELECT |
||
| 33 | m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, |
||
| 34 | COALESCE(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name, |
||
| 35 | SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled |
||
| 36 | FROM {db_prefix}messages AS m |
||
| 37 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
| 38 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
| 39 | LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) |
||
| 40 | WHERE m.id_msg >= {int:likely_max_msg}' . |
||
| 41 | (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
| 42 | AND b.id_board != {int:recycle_board}' : '') . ' |
||
| 43 | AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? ' |
||
| 44 | AND t.approved = {int:is_approved} |
||
| 45 | AND m.approved = {int:is_approved}' : '') . ' |
||
| 46 | ORDER BY m.id_msg DESC |
||
| 47 | LIMIT ' . $latestPostOptions['number_posts'], |
||
| 48 | array( |
||
| 49 | 'likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']), |
||
| 50 | 'recycle_board' => $modSettings['recycle_board'], |
||
| 51 | 'is_approved' => 1, |
||
| 52 | ) |
||
| 53 | ); |
||
| 54 | $rows = $smcFunc['db_fetch_all']($request); |
||
| 55 | |||
| 56 | // If the ability to embed attachments in posts is enabled, load the attachments now for efficiency |
||
| 57 | if (!empty($modSettings['attachmentEnable']) && (empty($modSettings['disabledBBC']) || !in_array('attach', explode(',', strtolower($modSettings['disabledBBC']))))) |
||
| 58 | { |
||
| 59 | $msgIDs = array(); |
||
| 60 | foreach ($rows as $row) |
||
| 61 | $msgIDs[] = $row['id_msg']; |
||
| 62 | |||
| 63 | require_once($sourcedir . '/Subs-Attachments.php'); |
||
| 64 | prepareAttachsByMsg($msgIDs); |
||
| 65 | } |
||
| 66 | |||
| 67 | $posts = array(); |
||
| 68 | foreach ($rows as $row) |
||
| 69 | { |
||
| 70 | // Censor the subject and post for the preview ;). |
||
| 71 | censorText($row['subject']); |
||
| 72 | censorText($row['body']); |
||
| 73 | |||
| 74 | $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => ' '))); |
||
| 75 | if ($smcFunc['strlen']($row['body']) > 128) |
||
| 76 | $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...'; |
||
| 77 | |||
| 78 | // Build the array. |
||
| 79 | $posts[] = array( |
||
| 80 | 'board' => array( |
||
| 81 | 'id' => $row['id_board'], |
||
| 82 | 'name' => $row['board_name'], |
||
| 83 | 'href' => $scripturl . '?board=' . $row['id_board'] . '.0', |
||
| 84 | 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>' |
||
| 85 | ), |
||
| 86 | 'topic' => $row['id_topic'], |
||
| 87 | 'poster' => array( |
||
| 88 | 'id' => $row['id_member'], |
||
| 89 | 'name' => $row['poster_name'], |
||
| 90 | 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'], |
||
| 91 | 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' |
||
| 92 | ), |
||
| 93 | 'subject' => $row['subject'], |
||
| 94 | 'short_subject' => shorten_subject($row['subject'], 24), |
||
| 95 | 'preview' => $row['body'], |
||
| 96 | 'time' => timeformat($row['poster_time']), |
||
| 97 | 'timestamp' => forum_time(true, $row['poster_time']), |
||
| 98 | 'raw_timestamp' => $row['poster_time'], |
||
| 99 | 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'], |
||
| 100 | 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>' |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | $smcFunc['db_free_result']($request); |
||
| 104 | |||
| 105 | return $posts; |
||
| 106 | } |
||
| 127 | ?> |