| Conditions | 20 |
| Paths | 242 |
| Total Lines | 75 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public static function phpagi_error_handler(int $level, string $message, string $file, int $line, array $context) { |
||
| 41 | if (ini_get('error_reporting') == 0) { |
||
| 42 | return; // this happens with an @ |
||
| 43 | } |
||
| 44 | |||
| 45 | @syslog(LOG_WARNING, $file . '[' . $line . ']: ' . $message); |
||
|
|
|||
| 46 | |||
| 47 | if (function_exists('mail') && !is_null(self::$phpagi_error_handler_email)) { // generate email debugging information |
||
| 48 | // decode error level |
||
| 49 | switch ($level) { |
||
| 50 | case E_WARNING: |
||
| 51 | case E_USER_WARNING: |
||
| 52 | $level = "Warning"; |
||
| 53 | break; |
||
| 54 | case E_NOTICE: |
||
| 55 | case E_USER_NOTICE: |
||
| 56 | $level = "Notice"; |
||
| 57 | break; |
||
| 58 | case E_USER_ERROR: |
||
| 59 | $level = "Error"; |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | |||
| 63 | // build message |
||
| 64 | $basefile = basename($file); |
||
| 65 | $subject = "$basefile/$line/$level: $message"; |
||
| 66 | $message = "$level: $message in $file on line $line\n\n"; |
||
| 67 | |||
| 68 | // figure out who we are |
||
| 69 | if (function_exists('socket_create')) { |
||
| 70 | $addr = null; |
||
| 71 | $port = 80; |
||
| 72 | $socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
||
| 73 | @socket_connect($socket, '64.0.0.0', $port); |
||
| 74 | @socket_getsockname($socket, $addr, $port); |
||
| 75 | @socket_close($socket); |
||
| 76 | $message .= "\n\nIP Address: $addr\n"; |
||
| 77 | } |
||
| 78 | |||
| 79 | // include variables |
||
| 80 | $message .= "\n\nContext:\n" . print_r($context, true); |
||
| 81 | $message .= "\n\nGLOBALS:\n" . print_r($GLOBALS, true); |
||
| 82 | $message .= "\n\nBacktrace:\n" . print_r(debug_backtrace(), true); |
||
| 83 | |||
| 84 | // include code fragment |
||
| 85 | if (file_exists($file)) { |
||
| 86 | $message .= "\n\n$file:\n"; |
||
| 87 | $code = @file($file); |
||
| 88 | for ($i = max(0, $line - 10); $i < min($line + 10, count($code)); $i++) { |
||
| 89 | $message .= ($i + 1) . "\t$code[$i]"; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // make sure message is fully readable (convert unprintable chars to hex representation) |
||
| 94 | $ret = ''; |
||
| 95 | for ($i = 0; $i < strlen($message); $i++) { |
||
| 96 | $c = ord($message[$i]); |
||
| 97 | if ($c == 10 || $c == 13 || $c == 9) { |
||
| 98 | $ret .= $message[$i]; |
||
| 99 | } elseif ($c < 16) { |
||
| 100 | $ret .= '\x0' . dechex($c); |
||
| 101 | } elseif ($c < 32 || $c > 127) { |
||
| 102 | $ret .= '\x' . dechex($c); |
||
| 103 | } else { |
||
| 104 | $ret .= $message[$i]; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | $message = $ret; |
||
| 108 | |||
| 109 | // send the mail if less than 5 errors |
||
| 110 | static $mailcount = 0; |
||
| 111 | if ($mailcount < 5) { |
||
| 112 | @mail(self::$phpagi_error_handler_email, $subject, $message); |
||
| 113 | } |
||
| 114 | $mailcount++; |
||
| 115 | } |
||
| 118 |
If you suppress an error, we recommend checking for the error condition explicitly: