| Conditions | 7 |
| Paths | 20 |
| Total Lines | 72 |
| Code Lines | 46 |
| 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 |
||
| 18 | public function homeAction() |
||
| 19 | { |
||
| 20 | $templateVariables = array(); |
||
| 21 | $announcement_forum = 14; |
||
| 22 | |||
| 23 | $finishedAnnouncements = $this->getForumAnnouncements($announcement_forum); |
||
| 24 | |||
| 25 | $cache = $this->get('phpbb.cache'); |
||
| 26 | |||
| 27 | if ($cache->contains('blog_announcements_file') !== FALSE) |
||
| 28 | { |
||
| 29 | $blogFile = $cache->fetch('blog_announcements_file'); |
||
| 30 | $cacheStatus = 'Hit'; |
||
| 31 | } |
||
| 32 | else |
||
| 33 | { |
||
| 34 | $rss_content = @file_get_contents('https://blog.phpbb.com/rss'); |
||
| 35 | $announcements = []; |
||
| 36 | $blogFile = []; |
||
| 37 | |||
| 38 | if (isset($rss_content)) |
||
| 39 | { |
||
| 40 | $xml_elements = new \SimpleXMLElement($rss_content); |
||
| 41 | $i = 0; |
||
| 42 | |||
| 43 | foreach ($xml_elements->channel->item as $item) |
||
| 44 | { |
||
| 45 | $post_time = strtotime($item->pubDate); |
||
| 46 | $blog_preview = wordwrap(html_entity_decode(strip_tags((string)$item->description)), 200, '>'); |
||
| 47 | $blog_preview = substr($blog_preview, 0, strpos($blog_preview, '>')); |
||
| 48 | |||
| 49 | $announcements[$post_time] = array( |
||
| 50 | 'DAY' => date('d', $post_time), |
||
| 51 | 'MONTH' => date('M', $post_time), |
||
| 52 | 'YEAR' => date('Y', $post_time), |
||
| 53 | 'U_LINK' => (string) $item->link, |
||
| 54 | 'TITLE' => (string) $item->title, |
||
| 55 | 'FROM_BLOG' => true, |
||
| 56 | 'PREVIEW' => $blog_preview, |
||
| 57 | ); |
||
| 58 | |||
| 59 | $i++; |
||
| 60 | if ($i == 3) |
||
| 61 | break; |
||
| 62 | } |
||
| 63 | |||
| 64 | // JSON Output |
||
| 65 | $blogFile = json_encode($announcements); |
||
| 66 | } |
||
| 67 | $cache->save('blog_announcements_file', $blogFile, 3600); |
||
| 68 | $cacheStatus = 'Miss'; |
||
| 69 | } |
||
| 70 | |||
| 71 | $blogJson = @json_decode($blogFile, true); |
||
| 72 | |||
| 73 | $blogAnnouncements = $blogJson === null ? array() : $blogJson; |
||
| 74 | $finishedAnnouncements = $finishedAnnouncements === null ? array() : $finishedAnnouncements; |
||
| 75 | |||
| 76 | krsort($blogAnnouncements); |
||
| 77 | |||
| 78 | $announcements = array_merge($finishedAnnouncements, $blogAnnouncements); |
||
| 79 | |||
| 80 | $templateVariables += array( |
||
| 81 | 'homepage' => true, |
||
| 82 | 'announcements_forum' => '/community/viewforum.php?f=' . $announcement_forum, |
||
| 83 | 'announcements' => $announcements,); |
||
| 84 | |||
| 85 | $content = $this->renderView('AppBundle:Global:index.html.twig', $templateVariables); |
||
| 86 | $response = new Response($content); |
||
| 87 | $response->headers->set('X-Cache-Blog', $cacheStatus); |
||
| 88 | return $response; |
||
| 89 | } |
||
| 90 | |||
| 155 |