| Conditions | 11 |
| Paths | 384 |
| Total Lines | 61 |
| Code Lines | 45 |
| 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 |
||
| 38 | public function logDetails(string $app, $message, int $level): array { |
||
| 39 | // default to ISO8601 |
||
| 40 | $format = $this->config->getValue('logdateformat', \DateTime::ATOM); |
||
| 41 | $logTimeZone = $this->config->getValue('logtimezone', 'UTC'); |
||
| 42 | try { |
||
| 43 | $timezone = new \DateTimeZone($logTimeZone); |
||
| 44 | } catch (\Exception $e) { |
||
| 45 | $timezone = new \DateTimeZone('UTC'); |
||
| 46 | } |
||
| 47 | $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); |
||
| 48 | if ($time === false) { |
||
| 49 | $time = new \DateTime(null, $timezone); |
||
| 50 | } else { |
||
| 51 | // apply timezone if $time is created from UNIX timestamp |
||
| 52 | $time->setTimezone($timezone); |
||
| 53 | } |
||
| 54 | $request = \OC::$server->getRequest(); |
||
| 55 | $reqId = $request->getId(); |
||
| 56 | $remoteAddr = $request->getRemoteAddress(); |
||
| 57 | // remove username/passwords from URLs before writing the to the log file |
||
| 58 | $time = $time->format($format); |
||
| 59 | $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; |
||
| 60 | $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; |
||
|
|
|||
| 61 | if ($this->config->getValue('installed', false)) { |
||
| 62 | $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; |
||
| 63 | } else { |
||
| 64 | $user = '--'; |
||
| 65 | } |
||
| 66 | $userAgent = $request->getHeader('User-Agent'); |
||
| 67 | if ($userAgent === '') { |
||
| 68 | $userAgent = '--'; |
||
| 69 | } |
||
| 70 | $version = $this->config->getValue('version', ''); |
||
| 71 | $entry = compact( |
||
| 72 | 'reqId', |
||
| 73 | 'level', |
||
| 74 | 'time', |
||
| 75 | 'remoteAddr', |
||
| 76 | 'user', |
||
| 77 | 'app', |
||
| 78 | 'method', |
||
| 79 | 'url', |
||
| 80 | 'message', |
||
| 81 | 'userAgent', |
||
| 82 | 'version' |
||
| 83 | ); |
||
| 84 | |||
| 85 | if (is_array($message)) { |
||
| 86 | // Exception messages are extracted and the exception is put into a separate field |
||
| 87 | // anything else modern is split to 'message' (string) and |
||
| 88 | // data (array) fields |
||
| 89 | if (array_key_exists('Exception', $message)) { |
||
| 90 | $entry['exception'] = $message; |
||
| 91 | $entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message']; |
||
| 92 | } else { |
||
| 93 | $entry['data'] = $message; |
||
| 94 | $entry['message'] = $message['message'] ?? '(no message provided)'; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return $entry; |
||
| 99 | } |
||
| 117 |