| Conditions | 12 |
| Paths | 23 |
| Total Lines | 51 |
| Code Lines | 27 |
| 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 |
||
| 37 | public static function log($message, $level = 0) { |
||
| 38 | |||
| 39 | if (!Debug::$enabled || Debug::$loglevel < $level) { |
||
| 40 | return false; |
||
| 41 | } |
||
| 42 | |||
| 43 | $ts = strftime("%H:%M:%S", time()); |
||
| 44 | if (function_exists('posix_getpid')) { |
||
| 45 | $ts = "$ts/".posix_getpid(); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (Debug::$logfile) { |
||
| 49 | $fp = fopen(Debug::$logfile, 'a+'); |
||
|
|
|||
| 50 | |||
| 51 | if ($fp) { |
||
| 52 | $locked = false; |
||
| 53 | |||
| 54 | if (function_exists("flock")) { |
||
| 55 | $tries = 0; |
||
| 56 | |||
| 57 | // try to lock logfile for writing |
||
| 58 | while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { |
||
| 59 | sleep(1); |
||
| 60 | ++$tries; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!$locked) { |
||
| 64 | fclose($fp); |
||
| 65 | user_error("Unable to lock debugging log file: ".Debug::$logfile, E_USER_WARNING); |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | fputs($fp, "[$ts] $message\n"); |
||
| 71 | |||
| 72 | if (function_exists("flock")) { |
||
| 73 | flock($fp, LOCK_UN); |
||
| 74 | } |
||
| 75 | |||
| 76 | fclose($fp); |
||
| 77 | |||
| 78 | if (Debug::$quiet) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | } else { |
||
| 83 | user_error("Unable to open debugging log file: ".Debug::$logfile, E_USER_WARNING); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | print "[$ts] $message\n"; |
||
| 88 | } |
||
| 89 | } |