| Conditions | 13 |
| Paths | 336 |
| Total Lines | 79 |
| Lines | 5 |
| Ratio | 6.33 % |
| 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 | View Code Duplication | 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 | |||
| 242 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.