| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 126 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 57 | public function setup($parameters, $id) |
||
| 58 | { |
||
| 59 | global $txt; |
||
| 60 | |||
| 61 | $feed = !empty($parameters['url']) ? un_htmlspecialchars($parameters['url']) : ''; |
||
| 62 | $this->data['show_title'] = !empty($parameters['show_title']); |
||
| 63 | $this->data['show_content'] = !empty($parameters['show_content']); |
||
| 64 | $this->data['show_date'] = !empty($parameters['show_date']); |
||
| 65 | $strip_preserve = !empty($parameters['strip_preserve']) ? $parameters['strip_preserve'] : 'br'; |
||
| 66 | $strip_preserve = preg_match_all('~[A-Za-z0-9]+~', $strip_preserve, $match) ? $match[0] : array(); |
||
| 67 | $count = !empty($parameters['count']) ? (int) $parameters['count'] : 5; |
||
| 68 | $limit = !empty($parameters['limit']) ? (int) $parameters['limit'] : 0; |
||
| 69 | |||
| 70 | // Need a feed name to load it |
||
| 71 | if (empty($feed)) |
||
| 72 | { |
||
| 73 | $this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
||
| 74 | $this->setTemplate('template_sp_rssFeed_error'); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $rss = array(); |
||
| 80 | |||
| 81 | require_once(SUBSDIR . '/Package.subs.php'); |
||
| 82 | $data = fetch_web_data($feed); |
||
| 83 | $data_save = $data; |
||
| 84 | |||
| 85 | // Convert it to UTF8 if we can and its not already |
||
| 86 | preg_match('~encoding="([^"]*)"~', $data, $charset); |
||
| 87 | if (!empty($charset[1]) && $charset != 'UTF-8') |
||
| 88 | { |
||
| 89 | // Use iconv if its available |
||
| 90 | if (function_exists('iconv')) |
||
| 91 | { |
||
| 92 | $data = @iconv($charset[1], 'UTF-8//TRANSLIT//IGNORE', $data); |
||
| 93 | } |
||
| 94 | |||
| 95 | // No iconv or a false response from it |
||
| 96 | if (!function_exists('iconv') || ($data == false)) |
||
| 97 | { |
||
| 98 | // PHP (some 5.4 versions) mishandles //TRANSLIT//IGNORE and returns false: see https://bugs.php.net/bug.php?id=61484 |
||
| 99 | if ($data == false) |
||
| 100 | { |
||
| 101 | $data = $data_save; |
||
| 102 | } |
||
| 103 | |||
| 104 | if (function_exists('mb_convert_encoding')) |
||
| 105 | { |
||
| 106 | // Replace unknown characters with a space |
||
| 107 | @ini_set('mbstring.substitute_character', '32'); |
||
| 108 | $data = @mb_convert_encoding($data, 'UTF-8', $charset[1]); |
||
| 109 | } |
||
| 110 | elseif (function_exists('recode_string')) |
||
| 111 | { |
||
| 112 | $data = @recode_string($charset[1] . '..' . 'UTF-8', $data); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $data = str_replace(array("\n", "\r", "\t"), '', $data); |
||
| 118 | $data = preg_replace_callback('~<\!\[CDATA\[(.+?)\]\]>~u', function($m) { |
||
| 119 | return "#cdata_escape_encode#" . Util::htmlspecialchars($m[1]); |
||
| 120 | }, $data); |
||
| 121 | |||
| 122 | // Find all the feed items |
||
| 123 | preg_match_all('~<item>(.+?)</item>~', $data, $items); |
||
| 124 | foreach ($items[1] as $item_id => $item) |
||
| 125 | { |
||
| 126 | if ($item_id === $count) |
||
| 127 | { |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | |||
| 131 | preg_match_all('~<([A-Za-z]+)>(.+?)</\\1>~', $item, $match); |
||
| 132 | |||
| 133 | foreach (array_keys($match[0]) as $tag_id) |
||
| 134 | { |
||
| 135 | if (Util::strpos($match[2][$tag_id], '#cdata_escape_encode#') === 0) |
||
| 136 | { |
||
| 137 | $match[2][$tag_id] = stripslashes(un_htmlspecialchars(Util::substr($match[2][$tag_id], 21))); |
||
| 138 | } |
||
| 139 | |||
| 140 | $rss[$item_id][strtolower($match[1][$tag_id])] = un_htmlspecialchars($match[2][$tag_id]); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // Nothing, say its invalid |
||
| 145 | if (empty($rss)) |
||
| 146 | { |
||
| 147 | $this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
||
| 148 | $this->setTemplate('template_sp_rssFeed_error'); |
||
| 149 | |||
| 150 | return; |
||
| 151 | } |
||
| 152 | |||
| 153 | // Add all the items to an array |
||
| 154 | $this->data['items'] = array(); |
||
| 155 | foreach ($rss as $item) |
||
| 156 | { |
||
| 157 | $item['title'] = isset($item['title']) ? strip_tags($item['title']) : ''; |
||
| 158 | $item['description'] = isset($item['description']) ? strip_tags($item['description'], empty($strip_preserve) ? '' : '<' . implode('><', $strip_preserve) . '>') : ''; |
||
| 159 | |||
| 160 | $this->data['items'][] = array( |
||
| 161 | 'title' => $item['title'], |
||
| 162 | 'href' => $item['link'], |
||
| 163 | 'link' => $item['title'] === '' ? '' : ($item['link'] === '' ? $item['title'] : '<a href="' . $item['link'] . '" target="_blank" class="new_win">' . $item['title'] . '</a>'), |
||
| 164 | 'content' => $limit > 0 ? Util::shorten_text($item['description'], $limit, true) : $item['description'], |
||
| 165 | 'date' => !empty($item['pubdate']) ? standardTime(strtotime($item['pubdate']), '%d %B') : '', |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | // No items in the feed |
||
| 170 | if (empty($this->data['items'])) |
||
| 171 | { |
||
| 172 | $this->data['error_msg'] = $txt['error_sp_invalid_feed']; |
||
| 173 | $this->setTemplate('template_sp_rssFeed_error'); |
||
| 174 | |||
| 175 | return; |
||
| 176 | } |
||
| 177 | else |
||
| 178 | { |
||
| 179 | $this->data['items'][count($this->data['items']) - 1]['is_last'] = true; |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->setTemplate('template_sp_rssFeed'); |
||
| 183 | } |
||
| 243 |