Conditions | 13 |
Paths | 336 |
Total Lines | 79 |
Code Lines | 50 |
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 |
||
34 | public function recent_news($widget = []) { |
||
35 | if ($widget['settings'] == FALSE) { |
||
36 | $settings = $this->defaults; |
||
37 | } else { |
||
38 | $settings = $widget['settings']; |
||
39 | } |
||
40 | |||
41 | $this->db->select("IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url) as full_url, content.id, content.title, prev_text, publish_date, showed, content.position, comments_count, author, category.name as cat_name", FALSE); |
||
42 | $this->db->join('category', 'category.id=content.category'); |
||
43 | $this->db->join('route', 'route.id=content.route_id'); |
||
44 | $this->db->where('post_status', 'publish'); |
||
45 | $this->db->where('prev_text !=', 'null'); |
||
46 | $this->db->where('publish_date <=', time()); |
||
47 | $this->db->where('lang', $this->config->item('cur_lang')); |
||
48 | |||
49 | if (count($settings['categories']) > 0) { |
||
50 | $this->db->where_in('category', $settings['categories']); |
||
51 | } |
||
52 | |||
53 | switch ($settings['display']) { |
||
54 | case 'recent': |
||
55 | $settings['display'] = 'publish_date'; // Recent news |
||
56 | break; |
||
57 | |||
58 | case 'popular': |
||
59 | $settings['display'] = 'showed'; // Popular news |
||
60 | break; |
||
61 | |||
62 | case 'position': |
||
63 | $settings['display'] = 'position'; // Position news |
||
64 | break; |
||
65 | |||
66 | default: |
||
67 | $settings['display'] = 'position'; // Position news |
||
68 | |||
69 | } |
||
70 | |||
71 | switch ($settings['sort_order']) { |
||
72 | case 'asc': |
||
73 | $settings['sort_order'] = 'asc'; |
||
74 | break; |
||
75 | |||
76 | case 'desc': |
||
77 | $settings['sort_order'] = 'desc'; |
||
78 | break; |
||
79 | |||
80 | default: |
||
81 | $settings['sort_order'] = 'desc'; |
||
82 | |||
83 | } |
||
84 | |||
85 | $this->db->order_by($settings['display'], $settings['sort_order']); |
||
86 | |||
87 | $query = $this->db->get('content', $settings['news_count']); |
||
88 | |||
89 | if ($query->num_rows() > 0) { |
||
90 | $news = $query->result_array(); |
||
91 | |||
92 | $cnt = count($news); |
||
93 | for ($i = 0; $i < $cnt; $i++) { |
||
94 | $news[$i]['prev_text'] = htmlspecialchars_decode($news[$i]['prev_text']); |
||
95 | |||
96 | // Truncate text |
||
97 | if ($settings['max_symdols'] > 0 AND mb_strlen($news[$i]['prev_text'], 'utf-8') > $settings['max_symdols']) { |
||
98 | $news[$i]['prev_text'] = mb_substr(strip_tags($news[$i]['prev_text']), 0, $settings['max_symdols'], 'utf-8') . '...'; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | foreach ($news as $k => $item) { |
||
103 | $news[$k] = $this->load->module('cfcm')->connect_fields($item, 'page'); |
||
104 | } |
||
105 | |||
106 | $data['recent_news'] = $news; |
||
107 | |||
108 | return $this->template->fetch('widgets/' . $widget['name'], $data); |
||
109 | } else { |
||
110 | return ''; |
||
111 | } |
||
112 | } |
||
113 | |||
243 |