| Conditions | 26 |
| Paths | 24 |
| Total Lines | 139 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 702 |
| 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(?int $userID = NULL) { |
||
| 9 | $userID = (is_null($userID) ? (int) $this->User->id : $userID); |
||
| 10 | |||
| 11 | $query = $this->db |
||
| 12 | ->select('tracker_chapters.*, |
||
| 13 | 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.mal_id AS title_mal_id, tracker_titles.last_checked > DATE_SUB(NOW(), INTERVAL 1 WEEK) AS title_active, |
||
| 14 | tracker_sites.site, tracker_sites.site_class, tracker_sites.status AS site_status') |
||
| 15 | ->from('tracker_chapters') |
||
| 16 | ->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.id', 'left') |
||
| 17 | ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
||
| 18 | ->where('tracker_chapters.user_id', $userID) |
||
| 19 | ->where('tracker_chapters.active', 'Y') |
||
| 20 | ->get(); |
||
| 21 | |||
| 22 | $arr = ['series' => [], 'has_inactive' => FALSE]; |
||
| 23 | $enabledCategories = $this->getEnabledCategories($userID); |
||
| 24 | foreach($enabledCategories as $category => $name) { |
||
| 25 | $arr['series'][$category] = [ |
||
| 26 | 'name' => $name, |
||
| 27 | 'manga' => [], |
||
| 28 | 'unread_count' => 0 |
||
| 29 | ]; |
||
| 30 | } |
||
| 31 | if($query->num_rows() > 0) { |
||
| 32 | foreach ($query->result() as $row) { |
||
| 33 | $is_unread = intval(($row->latest_chapter == $row->ignore_chapter) || ($row->latest_chapter == $row->current_chapter) ? '1' : '0'); |
||
| 34 | $arr['series'][$row->category]['unread_count'] = (($arr['series'][$row->category]['unread_count'] ?? 0) + !$is_unread); |
||
| 35 | $data = [ |
||
| 36 | 'id' => $row->id, |
||
| 37 | 'generated_current_data' => $this->sites->{$row->site_class}->getChapterData($row->title_url, $row->current_chapter), |
||
| 38 | 'generated_latest_data' => $this->sites->{$row->site_class}->getChapterData($row->title_url, $row->latest_chapter), |
||
| 39 | |||
| 40 | 'generated_ignore_number' => ($row->ignore_chapter ? ' <span class=\'hidden-chapter\' title=\'The latest chapter was marked as ignored.\'>'.htmlentities($this->sites->{$row->site_class}->getChapterData($row->title_url, $row->ignore_chapter)['number']).'</span>' : ''), |
||
| 41 | |||
| 42 | 'full_title_url' => $this->sites->{$row->site_class}->getFullTitleURL($row->title_url), |
||
| 43 | |||
| 44 | 'new_chapter_exists' => $is_unread, |
||
| 45 | 'tag_list' => $row->tags, |
||
| 46 | 'has_tags' => !empty($row->tags), |
||
| 47 | |||
| 48 | //TODO: We should have an option so chapter mal_id can take priority. |
||
| 49 | 'mal_id' => $row->mal_id ?? $row->title_mal_id, //TODO: This should have an option |
||
| 50 | 'mal_type' => (!is_null($row->mal_id) ? 'chapter' : 'title'), |
||
| 51 | |||
| 52 | 'title_data' => [ |
||
| 53 | 'id' => $row->title_id, |
||
| 54 | 'title' => $row->title, |
||
| 55 | 'title_url' => $row->title_url, |
||
| 56 | 'latest_chapter' => $row->latest_chapter, |
||
| 57 | 'current_chapter' => $row->current_chapter, |
||
| 58 | 'last_updated' => $row->title_last_updated, |
||
| 59 | //NOTE: active is used to warn the user if a title hasn't updated (Maybe due to nobody active tracking it or other reasons). |
||
| 60 | // This will ONLY be false when an actively updating series (site enabled & title status = 0) hasn't updated within the past week. |
||
| 61 | 'active' => ($row->site_status == 'disabled' || in_array($row->title_status, [/*complete*/ 1, /* one-shot */ 2, /* ignored */ 255]) || $row->title_active == 1) |
||
| 62 | ], |
||
| 63 | 'site_data' => [ |
||
| 64 | 'id' => $row->site_id, |
||
| 65 | 'site' => $row->site, |
||
| 66 | 'status' => $row->site_status |
||
| 67 | ] |
||
| 68 | ]; |
||
| 69 | $data['mal_icon'] = (!is_null($data['mal_id']) ? ($data['mal_id'] !== '0' ? "<a href=\"https://myanimelist.net/manga/{$data['mal_id']}\"><i class=\"sprite-site sprite-myanimelist-net\" title=\"{$data['mal_id']}\"></i></a>" : "<i class=\"sprite-site sprite-myanimelist-net-none\" title=\"none\"></i>") : ''); |
||
| 70 | |||
| 71 | $arr['series'][$row->category]['manga'][] = $data; |
||
| 72 | |||
| 73 | if(!$arr['has_inactive']) $arr['has_inactive'] = !$data['title_data']['active']; |
||
| 74 | } |
||
| 75 | |||
| 76 | //FIXME: This is not good for speed, but we're kind of required to do this for UX purposes. |
||
| 77 | // Tablesorter has a weird sorting algorithm and has a delay before sorting which is why I'd like to avoid it. |
||
| 78 | //FIXME: Is it possible to reduce duplication here without reducing speed? |
||
| 79 | $sortOrder = $this->User_Options->get('list_sort_order', $userID); |
||
| 80 | switch($this->User_Options->get('list_sort_type', $userID)) { |
||
| 81 | case 'unread': |
||
|
|
|||
| 82 | foreach (array_keys($arr['series']) as $category) { |
||
| 83 | usort($arr['series'][$category]['manga'], function ($a, $b) use($sortOrder) { |
||
| 84 | $a_text = strtolower("{$a['new_chapter_exists']} - {$a['title_data']['title']}"); |
||
| 85 | $b_text = strtolower("{$b['new_chapter_exists']} - {$b['title_data']['title']}"); |
||
| 86 | |||
| 87 | if($sortOrder == 'asc') { |
||
| 88 | return $a_text <=> $b_text; |
||
| 89 | } else { |
||
| 90 | return $b_text <=> $a_text; |
||
| 91 | } |
||
| 92 | }); |
||
| 93 | } |
||
| 94 | break; |
||
| 95 | |||
| 96 | case 'alphabetical': |
||
|
1 ignored issue
–
show
|
|||
| 97 | foreach (array_keys($arr['series']) as $category) { |
||
| 98 | usort($arr['series'][$category]['manga'], function($a, $b) use($sortOrder) { |
||
| 99 | $a_text = strtolower("{$a['title_data']['title']}"); |
||
| 100 | $b_text = strtolower("{$b['title_data']['title']}"); |
||
| 101 | |||
| 102 | if($sortOrder == 'asc') { |
||
| 103 | return $a_text <=> $b_text; |
||
| 104 | } else { |
||
| 105 | return $b_text <=> $a_text; |
||
| 106 | } |
||
| 107 | }); |
||
| 108 | } |
||
| 109 | break; |
||
| 110 | |||
| 111 | case 'my_status': |
||
| 112 | foreach (array_keys($arr['series']) as $category) { |
||
| 113 | usort($arr['series'][$category]['manga'], function($a, $b) use($sortOrder) { |
||
| 114 | $a_text = strtolower("{$a['generated_current_data']['number']}"); |
||
| 115 | $b_text = strtolower("{$b['generated_current_data']['number']}"); |
||
| 116 | |||
| 117 | if($sortOrder == 'asc') { |
||
| 118 | return $a_text <=> $b_text; |
||
| 119 | } else { |
||
| 120 | return $b_text <=> $a_text; |
||
| 121 | } |
||
| 122 | }); |
||
| 123 | } |
||
| 124 | break; |
||
| 125 | |||
| 126 | case 'latest': |
||
| 127 | foreach (array_keys($arr['series']) as $category) { |
||
| 128 | usort($arr['series'][$category]['manga'], function($a, $b) use($sortOrder) { |
||
| 129 | $a_text = strtolower("{$a['generated_latest_data']['number']}"); |
||
| 130 | $b_text = strtolower("{$b['generated_latest_data']['number']}"); |
||
| 131 | |||
| 132 | if($sortOrder == 'asc') { |
||
| 133 | return $a_text <=> $b_text; |
||
| 134 | } else { |
||
| 135 | return $b_text <=> $a_text; |
||
| 136 | } |
||
| 137 | }); |
||
| 138 | } |
||
| 139 | break; |
||
| 140 | |||
| 141 | default: |
||
|
1 ignored issue
–
show
|
|||
| 142 | break; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | return $arr; |
||
| 146 | } |
||
| 147 | |||
| 323 |
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.