| Conditions | 8 |
| Paths | 6 |
| Total Lines | 60 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 94 | private function getForumAnnouncements($announcement_forum) |
||
| 95 | { |
||
| 96 | $retrieve_limit = 3; |
||
| 97 | |||
| 98 | $phpbbConnection = $this->get('doctrine.dbal.phpbb_connection'); |
||
| 99 | $forumAnnouncements = PhpbbHandling::getTopicsFromForum( |
||
| 100 | $phpbbConnection, |
||
| 101 | $announcement_forum, |
||
| 102 | $retrieve_limit, |
||
| 103 | $this->container->getParameter('phpbb_database_prefix') |
||
| 104 | ); |
||
| 105 | $finishedAnnouncements = array(); |
||
| 106 | |||
| 107 | foreach ($forumAnnouncements as $announcement) { |
||
| 108 | $preview = $announcement['post_text']; |
||
| 109 | $preview = PhpbbHandling::bbcodeStripping($preview, $announcement['bbcode_uid']); |
||
| 110 | $preview = trim(preg_replace('#http(?:\:|&\#58;)//\S+#', '', $preview)); |
||
| 111 | |||
| 112 | // Decide how large the preview text should be |
||
| 113 | $preview_max_len = 200; |
||
| 114 | $preview_len = strlen($preview); |
||
| 115 | |||
| 116 | if ($preview_len > $preview_max_len) { |
||
| 117 | // If the first thing is a link, nuke it |
||
| 118 | $preview_clean = str_replace(':', ':', $preview); |
||
| 119 | |||
| 120 | if (substr($preview_clean, 0, 8) == 'https://' || substr($preview_clean, 0, 7) == 'http://') { |
||
| 121 | $preview = preg_replace('/^(\S*)\s/', '', $preview); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Truncate to the maximum length |
||
| 125 | $preview = substr($preview, 0, $preview_max_len); |
||
| 126 | |||
| 127 | // See if there is a nice place to break close to the max length |
||
| 128 | $breakchars = array(' ', ',', '.'); |
||
| 129 | $clean_break_pos = 0; |
||
| 130 | |||
| 131 | foreach ($breakchars as $char) { |
||
| 132 | $clean_break_pos_new = strrpos($preview, $char); |
||
| 133 | $clean_break_pos = $clean_break_pos_new > $clean_break_pos ? $clean_break_pos_new : $clean_break_pos; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($clean_break_pos) { |
||
| 137 | $preview = substr($preview, 0, $clean_break_pos); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $finishedAnnouncements[$announcement['topic_time']] = array( |
||
| 142 | 'DAY' => date('d', $announcement['topic_time']), |
||
| 143 | 'MONTH' => date('M', $announcement['topic_time']), |
||
| 144 | 'YEAR' => date('Y', $announcement['topic_time']), |
||
| 145 | 'U_LINK' => '/community/viewtopic.php?f=' . $announcement_forum . '&t=' . $announcement['topic_id'], |
||
| 146 | 'TITLE' => $announcement['topic_title'], |
||
| 147 | 'FROM_BLOG' => false, |
||
| 148 | 'PREVIEW' => $preview, |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $finishedAnnouncements; |
||
| 153 | } |
||
| 154 | } |
||
| 155 |