Conditions | 13 |
Paths | 22 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
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 |
||
173 | public function renderLog($cols) |
||
174 | { |
||
175 | // render the start of the table |
||
176 | $body = '<table class="table">'; |
||
177 | $body .= '<tr>'; |
||
178 | foreach ($cols as $heading) { |
||
179 | $body .= '<th>'.$heading.'</th>'; |
||
180 | } |
||
181 | $body .= '</tr>'; |
||
182 | |||
183 | $fp = fopen($this->path, 'r'); |
||
184 | |||
185 | while (($line = fgetcsv($fp)) !== false) { |
||
186 | $body .= '<tr>'; |
||
187 | |||
188 | for ($col = 0; $col < count($line); ++$col) { |
||
|
|||
189 | |||
190 | // if it is an error log, render the error types field in different colours |
||
191 | if ($col == 1 && $cols[1] == 'Level') { |
||
192 | switch ($line[$col]) { |
||
193 | case 'DEBUG': |
||
194 | $body .= '<td class="debug">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
195 | break; |
||
196 | case 'INFO': |
||
197 | $body .= '<td class="info">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
198 | break; |
||
199 | case 'WARN': |
||
200 | $body .= '<td class="warn">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
201 | break; |
||
202 | case 'ERROR': |
||
203 | $body .= '<td class="error">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
204 | break; |
||
205 | case 'FATAL': |
||
206 | $body .= '<td class="fatal">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
207 | break; |
||
208 | case 'SQL': |
||
209 | $body .= '<td class="sql">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
210 | break; |
||
211 | default: |
||
212 | $body .= '<td>'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
213 | break; |
||
214 | } |
||
215 | } else { |
||
216 | if ($cols[$col] == 'Message') { |
||
217 | $body .= '<td><pre>'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</pre></td>'; |
||
218 | } else { |
||
219 | $body .= '<td>'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | $body .= '</tr>'; |
||
225 | } |
||
226 | |||
227 | $body .= '</table>'; |
||
228 | |||
229 | return $body; |
||
230 | } |
||
231 | } |
||
232 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: