| Conditions | 17 |
| Paths | 552 |
| Total Lines | 90 |
| Code Lines | 58 |
| 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 |
||
| 79 | public static function getRssFeedAsHtml(string $feed_url, int $maxItemCount = 10, bool $show_date = true, bool $show_description = true, int $max_words = 0, int $cache_timeout = 7200, string $cache_prefix = XOOPS_VAR_PATH . '/caches/xoops_cache/rss2html-'): string |
||
| 80 | { |
||
| 81 | $result = ''; |
||
| 82 | // get feeds and parse items |
||
| 83 | $rss = new \DOMDocument(); |
||
| 84 | $cacheFile = $cache_prefix . \md5($feed_url); |
||
| 85 | // load from file or load content |
||
| 86 | if ($cache_timeout > 0 |
||
| 87 | && \is_file($cacheFile) |
||
| 88 | && (\filemtime($cacheFile) + $cache_timeout > \time())) { |
||
| 89 | $rss->load($cacheFile); |
||
| 90 | } else { |
||
| 91 | $rss->load($feed_url); |
||
| 92 | /* |
||
| 93 | // if load() doesn't work, you might try this |
||
| 94 | $ch = curl_init($feed_url); |
||
| 95 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 96 | $content = curl_exec($ch); |
||
| 97 | curl_close($ch); |
||
| 98 | $rss->loadXML($content); |
||
| 99 | */ |
||
| 100 | if ($cache_timeout > 0) { |
||
| 101 | $rss->save($cacheFile); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | $feed = []; |
||
| 105 | foreach ($rss->getElementsByTagName('item') as $node) { |
||
| 106 | if (null !== $node) { |
||
| 107 | $item = [ |
||
| 108 | 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, |
||
| 109 | 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, |
||
| 110 | 'content' => $node->getElementsByTagName('description')->item(0)->nodeValue, |
||
| 111 | 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, |
||
| 112 | 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, |
||
| 113 | ]; |
||
| 114 | $content = $node->getElementsByTagName('encoded'); // <content:encoded> |
||
| 115 | if ($content->length > 0) { |
||
| 116 | $item['content'] = $content->item(0)->nodeValue; |
||
| 117 | } |
||
| 118 | $feed[] = $item; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | // real good count |
||
| 122 | if ($maxItemCount > \count($feed)) { |
||
| 123 | $maxItemCount = \count($feed); |
||
| 124 | } |
||
| 125 | $result .= '<ul class="feed-lists">'; |
||
| 126 | for ($x = 0; $x < $maxItemCount; $x++) { |
||
| 127 | $title = \str_replace(' & ', ' & ', $feed[$x]['title']); |
||
| 128 | $link = $feed[$x]['link']; |
||
| 129 | $result .= '<li class="feed-item">'; |
||
| 130 | $result .= '<div class="feed-title"><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong></div>'; |
||
| 131 | if ($show_date) { |
||
| 132 | $date = \date('l F d, Y', (int)\strtotime($feed[$x]['date'])); |
||
| 133 | $result .= '<small class="feed-date"><em>Posted on ' . $date . '</em></small>'; |
||
| 134 | } |
||
| 135 | if ($show_description) { |
||
| 136 | $description = $feed[$x]['desc']; |
||
| 137 | $content = $feed[$x]['content']; |
||
| 138 | // find the img |
||
| 139 | $hasImage = \preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $image); |
||
| 140 | // no html tags |
||
| 141 | $description = \strip_tags((string)\preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', '$1$3', $description), ''); |
||
| 142 | // whether cut by number of words |
||
| 143 | if ($max_words > 0) { |
||
| 144 | $arr = \explode(' ', $description); |
||
| 145 | if ($max_words < \count($arr)) { |
||
| 146 | $description = ''; |
||
| 147 | $wordsCount = 0; |
||
| 148 | foreach ($arr as $w) { |
||
| 149 | $description .= $w . ' '; |
||
| 150 | ++$wordsCount; |
||
| 151 | if ($wordsCount == $max_words) { |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | $description .= ' ...'; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | // add img if it exists |
||
| 159 | if (1 == $hasImage) { |
||
| 160 | $description = '<img class="feed-item-image" src="' . $image['src'] . '" />' . $description; |
||
| 161 | } |
||
| 162 | $result .= '<div class="feed-description">' . $description; |
||
| 163 | $result .= ' <a href="' . $link . '" title="' . $title . '">Continue Reading »</a>' . '</div>'; |
||
| 164 | } |
||
| 165 | $result .= '</li>'; |
||
| 166 | } |
||
| 167 | $result .= '</ul>'; |
||
| 168 | return $result; |
||
| 169 | } |
||
| 176 |