| 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'); |
||
| 156 | public function userGetHistory(int $page) : array { |
||
| 157 | $rowsPerPage = 50; |
||
| 158 | $query = $this->db |
||
| 159 | ->select('SQL_CALC_FOUND_ROWS |
||
| 160 | tt.title, tt.title_url, |
||
| 161 | ts.site, ts.site_class, |
||
| 162 | tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE) |
||
| 163 | ->from('tracker_user_history AS tuh') |
||
| 164 | ->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left') |
||
| 165 | ->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left') |
||
| 166 | ->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left') |
||
| 167 | ->where('tc.user_id', $this->User->id) |
||
| 168 | ->order_by('tuh.id DESC') |
||
| 169 | ->limit($rowsPerPage, ($rowsPerPage * ($page - 1))) |
||
| 170 | ->get(); |
||
| 171 | |||
| 172 | $arr = ['rows' => [], 'totalPages' => 1]; |
||
| 173 | if($query->num_rows() > 0) { |
||
| 174 | foreach($query->result() as $row) { |
||
| 175 | $arrRow = []; |
||
| 176 | |||
| 177 | $arrRow['updated_at'] = $row->updated_at; |
||
| 178 | $arrRow['title'] = $row->title; |
||
| 179 | $arrRow['title_url'] = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url); |
||
| 180 | |||
| 181 | $arrRow['site'] = $row->site; |
||
| 182 | $arrRow['site_sprite'] = str_replace('.', '-', $row->site); |
||
| 183 | |||
| 184 | switch($row->type) { |
||
| 185 | View Code Duplication | case 1: |
|
|
1 ignored issue
–
show
|
|||
| 186 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 187 | $arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'"; |
||
| 188 | break; |
||
| 189 | |||
| 190 | View Code Duplication | case 2: |
|
|
1 ignored issue
–
show
|
|||
| 191 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 192 | $arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 193 | break; |
||
| 194 | |||
| 195 | case 3: |
||
| 196 | $arrRow['status'] = "Series removed"; |
||
| 197 | break; |
||
| 198 | |||
| 199 | case 4: |
||
| 200 | $arrRow['status'] = "Tags set to '{$row->custom1}'"; |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 5: |
||
| 204 | $arrRow['status'] = "Category set to '{$row->custom1}'"; |
||
| 205 | break; |
||
| 206 | |||
| 207 | View Code Duplication | case 6: |
|
|
1 ignored issue
–
show
|
|||
| 208 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 209 | $arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 210 | break; |
||
| 211 | |||
| 212 | View Code Duplication | case 7: |
|
|
1 ignored issue
–
show
|
|||
| 213 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
| 214 | $arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
| 215 | break; |
||
| 216 | |||
| 217 | default: |
||
| 218 | $arrRow['status'] = "Something went wrong!"; |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | $arr['rows'][] = $arrRow; |
||
| 222 | } |
||
| 223 | $arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage); |
||
| 224 | } |
||
| 225 | return $arr; |
||
| 226 | } |
||
| 227 | } |
||
| 228 |
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: