| Conditions | 13 |
| Paths | 22 |
| Total Lines | 58 |
| Code Lines | 41 |
| Lines | 18 |
| Ratio | 31.03 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 | View Code Duplication | case 'INFO': |
|
| 197 | $body .= '<td class="info">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
| 198 | break; |
||
| 199 | View Code Duplication | case 'WARN': |
|
| 200 | $body .= '<td class="warn">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
| 201 | break; |
||
| 202 | View Code Duplication | case 'ERROR': |
|
| 203 | $body .= '<td class="error">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
| 204 | break; |
||
| 205 | View Code Duplication | case 'FATAL': |
|
| 206 | $body .= '<td class="fatal">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
| 207 | break; |
||
| 208 | View Code Duplication | case 'SQL': |
|
| 209 | $body .= '<td class="sql">'.htmlentities($line[$col], ENT_COMPAT, 'utf-8').'</td>'; |
||
| 210 | break; |
||
| 211 | View Code Duplication | 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 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.