| Conditions | 10 |
| Paths | 7 |
| Total Lines | 56 |
| Code Lines | 30 |
| 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 |
||
| 97 | function getRss($feeds, $toChannel, $discord) |
||
| 98 | { |
||
| 99 | foreach ($feeds as $rssUrl){ |
||
| 100 | //Check that url is set |
||
| 101 | if (!isset($rssUrl) || $rssUrl == ""){ |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | |||
| 105 | $rss = simplexml_load_file($rssUrl); // XML parser |
||
| 106 | $feedLink = $rss->channel->link; |
||
| 107 | $latestTopicDate = getPermCache("rssFeed{$feedLink}"); |
||
| 108 | |||
| 109 | //Check if feed has been checked before |
||
| 110 | if ($latestTopicDate == NULL) { |
||
| 111 | // Set date to now to avoid posting old articles |
||
| 112 | setPermCache("rssFeed{$feedLink}", time()); |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | //Find item to check if feed is formatted |
||
| 117 | $itemTitle = (string) $rss->channel->item->title; |
||
| 118 | $itemUrl = (string) $rss->channel->item->link; |
||
| 119 | $itemDate = strtotime($rss->channel->item->pubDate); |
||
| 120 | |||
| 121 | //Check to see if feed is formatted correctly |
||
| 122 | if ($itemTitle == NULL || $itemUrl == NULL || $itemDate == NULL) { |
||
| 123 | $this->logger->addInfo("RSS Reader Error: {$rssUrl} is not a properly formatted RSS feed."); |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | |||
| 127 | //Find item to post |
||
| 128 | foreach($rss->channel->item as $item){ |
||
| 129 | //Get item details |
||
| 130 | $itemTitle = (string) $item->title; |
||
| 131 | $itemUrl = (string) $item->link; |
||
| 132 | $itemPubbed = $item->pubDate; |
||
| 133 | $itemDate = strtotime($item->pubDate); |
||
| 134 | |||
| 135 | //Check if item is old |
||
| 136 | if ($itemDate <= $latestTopicDate) { |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | //Set message |
||
| 141 | $msg = "\n**{$itemTitle}**\n\n*{$itemPubbed}*\n{$itemUrl}"; |
||
| 142 | |||
| 143 | //Send message |
||
| 144 | $channelID = $toChannel; |
||
| 145 | $guild = $discord->guilds->get('id', $this->guild); |
||
| 146 | $channel = $guild->channels->get('id', $channelID); |
||
| 147 | $channel->sendMessage($msg, false); |
||
| 148 | $this->logger->addInfo("RSS Reader Item Posted: {$itemTitle}"); |
||
| 149 | } |
||
| 150 | setPermCache("rssFeed{$feedLink}", $itemDate); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.