Conditions | 14 |
Paths | 109 |
Total Lines | 71 |
Code Lines | 45 |
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 |
||
186 | function _logMsg($level, $message) { |
||
187 | $roll = false; |
||
188 | |||
189 | if ($level < $this->_level) |
||
190 | return; |
||
191 | |||
192 | $logFile = $this->toOSPath($this->_path . "/" . $this->_filename); |
||
193 | |||
194 | switch ($level) { |
||
195 | case MC_LOGGER_DEBUG: |
||
196 | $levelName = "DEBUG"; |
||
197 | break; |
||
198 | |||
199 | case MC_LOGGER_INFO: |
||
200 | $levelName = "INFO"; |
||
201 | break; |
||
202 | |||
203 | case MC_LOGGER_WARN: |
||
204 | $levelName = "WARN"; |
||
205 | break; |
||
206 | |||
207 | case MC_LOGGER_ERROR: |
||
208 | $levelName = "ERROR"; |
||
209 | break; |
||
210 | |||
211 | case MC_LOGGER_FATAL: |
||
212 | $levelName = "FATAL"; |
||
213 | break; |
||
214 | } |
||
215 | |||
216 | $logFile = str_replace('{level}', strtolower($levelName), $logFile); |
||
217 | |||
218 | $text = $this->_format; |
||
219 | $text = str_replace('{time}', date("Y-m-d H:i:s"), $text); |
||
220 | $text = str_replace('{level}', strtolower($levelName), $text); |
||
221 | $text = str_replace('{message}', $message, $text); |
||
222 | $message = $text . "\r\n"; |
||
223 | |||
224 | // Check filesize |
||
225 | if (file_exists($logFile)) { |
||
226 | $size = @filesize($logFile); |
||
227 | |||
228 | if ($size + strlen($message) > $this->_maxSizeBytes) |
||
229 | $roll = true; |
||
230 | } |
||
231 | |||
232 | // Roll if the size is right |
||
233 | if ($roll) { |
||
234 | for ($i=$this->_maxFiles-1; $i>=1; $i--) { |
||
235 | $rfile = $this->toOSPath($logFile . "." . $i); |
||
236 | $nfile = $this->toOSPath($logFile . "." . ($i+1)); |
||
237 | |||
238 | if (@file_exists($rfile)) |
||
239 | @rename($rfile, $nfile); |
||
240 | } |
||
241 | |||
242 | @rename($logFile, $this->toOSPath($logFile . ".1")); |
||
243 | |||
244 | // Delete last logfile |
||
245 | $delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1)); |
||
246 | if (@file_exists($delfile)) |
||
247 | @unlink($delfile); |
||
248 | } |
||
249 | |||
250 | // Append log line |
||
251 | if (($fp = @fopen($logFile, "a")) != null) { |
||
252 | @fputs($fp, $message); |
||
253 | @fflush($fp); |
||
254 | @fclose($fp); |
||
255 | } |
||
256 | } |
||
257 | |||
269 |