| Conditions | 10 |
| Paths | 2 |
| Total Lines | 71 |
| Code Lines | 54 |
| Lines | 16 |
| Ratio | 22.54 % |
| Tests | 0 |
| CRAP Score | 110 |
| 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'); |
||
| 123 | public function userGetHistory(int $page) : array { |
||
| 124 | $rowsPerPage = 50; |
||
| 125 | $query = $this->db |
||
| 126 | ->select('SQL_CALC_FOUND_ROWS |
||
| 127 | tt.title, tt.title_url, |
||
| 128 | ts.site, ts.site_class, |
||
| 129 | tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE) |
||
| 130 | ->from('tracker_user_history AS tuh') |
||
| 131 | ->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left') |
||
| 132 | ->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left') |
||
| 133 | ->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left') |
||
| 134 | ->where('tc.user_id', $this->User->id) |
||
| 135 | ->order_by('tuh.id DESC') |
||
| 136 | ->limit($rowsPerPage, ($rowsPerPage * ($page - 1))) |
||
| 137 | ->get(); |
||
| 138 | |||
| 139 | $arr = ['rows' => [], 'totalCount' => 0]; |
||
| 140 | if($query->num_rows() > 0) { |
||
| 141 | foreach($query->result() as $row) { |
||
| 142 | $arrRow = []; |
||
| 143 | |||
| 144 | $arrRow['updated_at'] = $row->updated_at; |
||
| 145 | $arrRow['title'] = $row->title; |
||
| 146 | $arrRow['title_url'] = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url); |
||
| 147 | |||
| 148 | $arrRow['site'] = $row->site; |
||
| 149 | $arrRow['site_sprite'] = str_replace('.', '-', $row->site); |
||
| 150 | |||
| 151 | switch($row->type) { |
||
| 152 | View Code Duplication | case 1: |
|
|
1 ignored issue
–
show
|
|||
| 153 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 154 | $arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'"; |
||
| 155 | break; |
||
| 156 | |||
| 157 | View Code Duplication | case 2: |
|
|
1 ignored issue
–
show
|
|||
| 158 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 159 | $arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 160 | break; |
||
| 161 | |||
| 162 | case 3: |
||
| 163 | $arrRow['status'] = "Series removed"; |
||
| 164 | break; |
||
| 165 | |||
| 166 | case 4: |
||
| 167 | $arrRow['status'] = "Tags set to '{$row->custom1}'"; |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 5: |
||
| 171 | $arrRow['status'] = "Category set to '{$row->custom1}'"; |
||
| 172 | break; |
||
| 173 | |||
| 174 | View Code Duplication | case 6: |
|
|
1 ignored issue
–
show
|
|||
| 175 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 176 | $arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 177 | break; |
||
| 178 | |||
| 179 | View Code Duplication | case 7: |
|
|
1 ignored issue
–
show
|
|||
| 180 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 181 | $arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 182 | break; |
||
| 183 | |||
| 184 | default: |
||
| 185 | $arrRow['status'] = "Something went wrong!"; |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | $arr['rows'][] = $arrRow; |
||
| 189 | } |
||
| 190 | $arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage); |
||
| 191 | } |
||
| 192 | return $arr; |
||
| 193 | } |
||
| 194 | } |
||
| 195 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: