Conditions | 8 |
Paths | 2 |
Total Lines | 57 |
Code Lines | 43 |
Lines | 8 |
Ratio | 14.04 % |
Tests | 0 |
CRAP Score | 72 |
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'); |
||
88 | public function userGetHistory(int $page) : array { |
||
89 | $rowsPerPage = 50; |
||
90 | $query = $this->db |
||
91 | ->select('SQL_CALC_FOUND_ROWS |
||
92 | tt.title, tt.title_url, |
||
93 | ts.site, ts.site_class, |
||
94 | tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE) |
||
95 | ->from('tracker_user_history AS tuh') |
||
96 | ->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left') |
||
97 | ->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left') |
||
98 | ->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left') |
||
99 | ->where('tc.user_id', $this->User->id) |
||
100 | ->order_by('tuh.id DESC') |
||
101 | ->limit($rowsPerPage, ($rowsPerPage * ($page - 1))) |
||
102 | ->get(); |
||
103 | |||
104 | $arr = ['rows' => [], 'totalCount' => 0]; |
||
105 | if($query->num_rows() > 0) { |
||
106 | foreach($query->result() as $row) { |
||
107 | $arrRow = []; |
||
108 | |||
109 | $arrRow['updated_at'] = $row->updated_at; |
||
110 | $arrRow['title'] = $row->title; |
||
111 | $arrRow['title_url'] = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url); |
||
112 | |||
113 | $arrRow['site'] = $row->site; |
||
114 | $arrRow['site_sprite'] = str_replace('.', '-', $row->site); |
||
115 | |||
116 | switch($row->type) { |
||
117 | View Code Duplication | case 1: |
|
1 ignored issue
–
show
|
|||
118 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
119 | $arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'"; |
||
120 | break; |
||
121 | |||
122 | View Code Duplication | case 2: |
|
1 ignored issue
–
show
|
|||
123 | $chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1); |
||
124 | $arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'"; |
||
125 | break; |
||
126 | |||
127 | case 3: |
||
128 | $arrRow['status'] = "Series removed"; |
||
129 | break; |
||
130 | |||
131 | case 4: |
||
132 | $arrRow['status'] = "Tags set to '{$row->custom1}'"; |
||
133 | break; |
||
134 | |||
135 | case 5: |
||
136 | $arrRow['status'] = "Category set to '{$row->custom1}'"; |
||
137 | break; |
||
138 | } |
||
139 | $arr['rows'][] = $arrRow; |
||
140 | } |
||
141 | $arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage); |
||
142 | } |
||
143 | return $arr; |
||
144 | } |
||
145 | } |
||
146 |
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: