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