| Conditions | 10 |
| Paths | 84 |
| Total Lines | 50 |
| Code Lines | 31 |
| Lines | 5 |
| Ratio | 10 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 33 | public function recent_news($widget = []) { |
||
| 34 | View Code Duplication | if ($widget['settings'] == FALSE) { |
|
| 35 | $settings = $this->defaults; |
||
| 36 | } else { |
||
| 37 | $settings = $widget['settings']; |
||
| 38 | } |
||
| 39 | |||
| 40 | $this->db->select('CONCAT_WS("", content.cat_url, content.url) as full_url, content.id, content.title, prev_text, publish_date, showed, comments_count, author, category.name as cat_name, content.cat_url', FALSE); |
||
| 41 | $this->db->join('category', 'category.id=content.category'); |
||
| 42 | $this->db->where('post_status', 'publish'); |
||
| 43 | $this->db->where('prev_text !=', 'null'); |
||
| 44 | $this->db->where('publish_date <=', time()); |
||
| 45 | $this->db->where('lang', $this->config->item('cur_lang')); |
||
| 46 | |||
| 47 | if (count($settings['categories']) > 0) { |
||
| 48 | $this->db->where_in('category', $settings['categories']); |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($settings['display'] == 'recent') { |
||
| 52 | $this->db->order_by('publish_date', 'desc'); // Recent news |
||
| 53 | } elseif ($settings['display'] == 'popular') { |
||
| 54 | $this->db->order_by('showed', 'desc'); // Pupular news |
||
| 55 | } |
||
| 56 | |||
| 57 | $query = $this->db->get('content', $settings['news_count']); |
||
| 58 | |||
| 59 | if ($query->num_rows() > 0) { |
||
| 60 | $news = $query->result_array(); |
||
| 61 | |||
| 62 | $cnt = count($news); |
||
| 63 | for ($i = 0; $i < $cnt; $i++) { |
||
| 64 | $news[$i]['prev_text'] = htmlspecialchars_decode($news[$i]['prev_text']); |
||
| 65 | |||
| 66 | // Truncate text |
||
| 67 | if ($settings['max_symdols'] > 0 AND mb_strlen($news[$i]['prev_text'], 'utf-8') > $settings['max_symdols']) { |
||
| 68 | $news[$i]['prev_text'] = mb_substr(strip_tags($news[$i]['prev_text']), 0, $settings['max_symdols'], 'utf-8') . '...'; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | foreach ($news as $k => $item) { |
||
| 73 | $news[$k] = $this->load->module('cfcm')->connect_fields($item, 'page'); |
||
| 74 | } |
||
| 75 | |||
| 76 | $data['recent_news'] = $news; |
||
| 77 | |||
| 78 | return $this->template->fetch('widgets/' . $widget['name'], $data); |
||
| 79 | } else { |
||
| 80 | return ''; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 211 | } |