Conditions | 12 |
Paths | 1152 |
Total Lines | 69 |
Lines | 5 |
Ratio | 7.25 % |
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 |
||
70 | public static function writeExtra($app, $message, $level, $conditionalLogFile, $extraFields = []) { |
||
71 | $config = \OC::$server->getSystemConfig(); |
||
72 | |||
73 | // default to ISO8601 |
||
74 | $format = $config->getValue('logdateformat', 'c'); |
||
75 | $logTimeZone = $config->getValue("logtimezone", 'UTC'); |
||
76 | try { |
||
77 | $timezone = new \DateTimeZone($logTimeZone); |
||
78 | } catch (\Exception $e) { |
||
79 | $timezone = new \DateTimeZone('UTC'); |
||
80 | } |
||
81 | $time = \DateTime::createFromFormat("U.u", \number_format(\microtime(true), 4, ".", "")); |
||
82 | if ($time === false) { |
||
83 | $time = new \DateTime(null, $timezone); |
||
84 | } else { |
||
85 | // apply timezone if $time is created from UNIX timestamp |
||
86 | $time->setTimezone($timezone); |
||
87 | } |
||
88 | $request = \OC::$server->getRequest(); |
||
89 | $reqId = $request->getId(); |
||
90 | $remoteAddr = $request->getRemoteAddress(); |
||
91 | // remove username/passwords from URLs before writing the to the log file |
||
92 | $time = $time->format($format); |
||
93 | $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; |
||
94 | $method = \is_string($request->getMethod()) ? $request->getMethod() : '--'; |
||
95 | View Code Duplication | if (\OC::$server->getConfig()->getSystemValue('installed', false)) { |
|
|
|||
96 | $user = (\OC_User::getUser()) ? \OC_User::getUser() : '--'; |
||
97 | } else { |
||
98 | $user = '--'; |
||
99 | } |
||
100 | $entry = \compact( |
||
101 | 'reqId', |
||
102 | 'level', |
||
103 | 'time', |
||
104 | 'remoteAddr', |
||
105 | 'user', |
||
106 | 'app', |
||
107 | 'method', |
||
108 | 'url', |
||
109 | 'message' |
||
110 | ); |
||
111 | |||
112 | if (!empty($extraFields)) { |
||
113 | // augment with additional fields |
||
114 | $entry = \array_merge($entry, $extraFields); |
||
115 | } |
||
116 | |||
117 | $entry = \json_encode($entry); |
||
118 | if ($conditionalLogFile !== null) { |
||
119 | if ($conditionalLogFile[0] !== '/') { |
||
120 | $conditionalLogFile = \OC::$server->getConfig()->getSystemValue('datadirectory') . "/" . $conditionalLogFile; |
||
121 | } |
||
122 | self::createLogFile($conditionalLogFile); |
||
123 | $handle = @\fopen($conditionalLogFile, 'a'); |
||
124 | } else { |
||
125 | self::createLogFile(self::$logFile); |
||
126 | $handle = @\fopen(self::$logFile, 'a'); |
||
127 | } |
||
128 | if ($handle) { |
||
129 | \fwrite($handle, $entry."\n"); |
||
130 | \fclose($handle); |
||
131 | } else { |
||
132 | // Fall back to error_log |
||
133 | \error_log($entry); |
||
134 | } |
||
135 | if (\php_sapi_name() === 'cli-server') { |
||
136 | \error_log($message, 4); |
||
137 | } |
||
138 | } |
||
139 | |||
163 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.