| Conditions | 20 |
| Paths | 12 |
| Total Lines | 125 |
| Code Lines | 85 |
| Lines | 42 |
| Ratio | 33.6 % |
| Tests | 0 |
| CRAP Score | 420 |
| 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 declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
||
| 8 | public function get() { |
||
| 9 | $query = $this->db |
||
| 10 | ->select('tracker_chapters.*, |
||
| 11 | tracker_titles.site_id, tracker_titles.title, tracker_titles.title_url, tracker_titles.latest_chapter, tracker_titles.last_updated AS title_last_updated, tracker_titles.status AS title_status, tracker_titles.last_checked > DATE_SUB(NOW(), INTERVAL 1 WEEK) AS title_active, |
||
| 12 | tracker_sites.site, tracker_sites.site_class, tracker_sites.status AS site_status') |
||
| 13 | ->from('tracker_chapters') |
||
| 14 | ->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.id', 'left') |
||
| 15 | ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
||
| 16 | ->where('tracker_chapters.user_id', $this->User->id) |
||
| 17 | ->where('tracker_chapters.active', 'Y') |
||
| 18 | ->get(); |
||
| 19 | |||
| 20 | $arr = ['series' => [], 'has_inactive' => FALSE]; |
||
| 21 | foreach($this->enabledCategories as $category => $name) { |
||
| 22 | $arr['series'][$category] = [ |
||
| 23 | 'name' => $name, |
||
| 24 | 'manga' => [], |
||
| 25 | 'unread_count' => 0 |
||
| 26 | ]; |
||
| 27 | } |
||
| 28 | if($query->num_rows() > 0) { |
||
| 29 | foreach ($query->result() as $row) { |
||
| 30 | $is_unread = intval($row->latest_chapter == $row->current_chapter ? '1' : '0'); |
||
| 31 | $arr['series'][$row->category]['unread_count'] = (($arr['series'][$row->category]['unread_count'] ?? 0) + !$is_unread); |
||
| 32 | $data = [ |
||
| 33 | 'id' => $row->id, |
||
| 34 | 'generated_current_data' => $this->sites->{$row->site_class}->getChapterData($row->title_url, $row->current_chapter), |
||
| 35 | 'generated_latest_data' => $this->sites->{$row->site_class}->getChapterData($row->title_url, $row->latest_chapter), |
||
| 36 | 'full_title_url' => $this->sites->{$row->site_class}->getFullTitleURL($row->title_url), |
||
| 37 | |||
| 38 | 'new_chapter_exists' => $is_unread, |
||
| 39 | 'tag_list' => $row->tags, |
||
| 40 | 'has_tags' => !empty($row->tags), |
||
| 41 | |||
| 42 | 'title_data' => [ |
||
| 43 | 'id' => $row->title_id, |
||
| 44 | 'title' => $row->title, |
||
| 45 | 'title_url' => $row->title_url, |
||
| 46 | 'latest_chapter' => $row->latest_chapter, |
||
| 47 | 'current_chapter' => $row->current_chapter, |
||
| 48 | 'last_updated' => $row->title_last_updated, |
||
| 49 | //NOTE: active is used to warn the user if a title hasn't updated (Maybe due to nobody active tracking it or other reasons). |
||
| 50 | // This will ONLY be false when an actively updating series (site enabled & title status = 0) hasn't updated within the past week. |
||
| 51 | 'active' => ($row->site_status == 'disabled' || in_array($row->title_status, [/*complete*/ 1, /* one-shot */ 2, /* ignored */ 255]) || $row->title_active == 1) |
||
| 52 | ], |
||
| 53 | 'site_data' => [ |
||
| 54 | 'id' => $row->site_id, |
||
| 55 | 'site' => $row->site, |
||
| 56 | 'status' => $row->site_status |
||
| 57 | ] |
||
| 58 | ]; |
||
| 59 | $arr['series'][$row->category]['manga'][] = $data; |
||
| 60 | |||
| 61 | if(!$arr['has_inactive']) $arr['has_inactive'] = !$data['title_data']['active']; |
||
| 62 | } |
||
| 63 | |||
| 64 | //CHECK: Is this good for speed? |
||
| 65 | //NOTE: This does not sort in the same way as tablesorter, but it works better. |
||
| 66 | switch($this->User_Options->get('list_sort_type')) { |
||
| 67 | case 'unread': |
||
|
|
|||
| 68 | foreach (array_keys($arr['series']) as $category) { |
||
| 69 | usort($arr['series'][$category]['manga'], function ($a, $b) { |
||
| 70 | $a_text = strtolower("{$a['new_chapter_exists']} - {$a['title_data']['title']}"); |
||
| 71 | $b_text = strtolower("{$b['new_chapter_exists']} - {$b['title_data']['title']}"); |
||
| 72 | |||
| 73 | if($this->User_Options->get('list_sort_order') == 'asc') { |
||
| 74 | return $a_text <=> $b_text; |
||
| 75 | } else { |
||
| 76 | return $b_text <=> $a_text; |
||
| 77 | } |
||
| 78 | }); |
||
| 79 | } |
||
| 80 | break; |
||
| 81 | |||
| 82 | View Code Duplication | case 'alphabetical': |
|
|
1 ignored issue
–
show
|
|||
| 83 | foreach (array_keys($arr['series']) as $category) { |
||
| 84 | usort($arr['series'][$category]['manga'], function ($a, $b) { |
||
| 85 | $a_text = strtolower("{$a['title_data']['title']}"); |
||
| 86 | $b_text = strtolower("{$b['title_data']['title']}"); |
||
| 87 | |||
| 88 | if($this->User_Options->get('list_sort_order') == 'asc') { |
||
| 89 | return $a_text <=> $b_text; |
||
| 90 | } else { |
||
| 91 | return $b_text <=> $a_text; |
||
| 92 | } |
||
| 93 | }); |
||
| 94 | } |
||
| 95 | break; |
||
| 96 | |||
| 97 | View Code Duplication | case 'my_status': |
|
| 98 | foreach (array_keys($arr['series']) as $category) { |
||
| 99 | usort($arr['series'][$category]['manga'], function ($a, $b) { |
||
| 100 | $a_text = strtolower("{$a['generated_current_data']['number']}"); |
||
| 101 | $b_text = strtolower("{$b['generated_current_data']['number']}"); |
||
| 102 | |||
| 103 | if($this->User_Options->get('list_sort_order') == 'asc') { |
||
| 104 | return $a_text <=> $b_text; |
||
| 105 | } else { |
||
| 106 | return $b_text <=> $a_text; |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | |||
| 112 | View Code Duplication | case 'latest': |
|
| 113 | foreach (array_keys($arr['series']) as $category) { |
||
| 114 | usort($arr['series'][$category]['manga'], function ($a, $b) { |
||
| 115 | $a_text = strtolower("{$a['generated_latest_data']['number']}"); |
||
| 116 | $b_text = strtolower("{$b['generated_latest_data']['number']}"); |
||
| 117 | |||
| 118 | if($this->User_Options->get('list_sort_order') == 'asc') { |
||
| 119 | return $a_text <=> $b_text; |
||
| 120 | } else { |
||
| 121 | return $b_text <=> $a_text; |
||
| 122 | } |
||
| 123 | }); |
||
| 124 | } |
||
| 125 | break; |
||
| 126 | |||
| 127 | default: |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | return $arr; |
||
| 132 | } |
||
| 133 | |||
| 232 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.