Conditions | 29 |
Paths | > 20000 |
Total Lines | 135 |
Lines | 10 |
Ratio | 7.41 % |
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 |
||
253 | public function log($level, $message, array $context = []) { |
||
254 | $minLevel = \min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); |
||
255 | $logConditions = $this->config->getValue('log.conditions', []); |
||
256 | if (empty($logConditions)) { |
||
257 | $logConditions[] = $this->config->getValue('log.condition', []); |
||
258 | } |
||
259 | $logConditionFile = null; |
||
260 | |||
261 | $extraFields = []; |
||
262 | if (isset($context['extraFields'])) { |
||
263 | $extraFields = $context['extraFields']; |
||
264 | unset($context['extraFields']); |
||
265 | } |
||
266 | |||
267 | $exception = null; |
||
268 | if (isset($context['exception'])) { |
||
269 | $exception = $context['exception']; |
||
270 | unset($context['exception']); |
||
271 | } |
||
272 | |||
273 | if (isset($context['app'])) { |
||
274 | $app = $context['app']; |
||
275 | |||
276 | /** |
||
277 | * check log condition based on the context of each log message |
||
278 | * once this is met -> change the required log level to debug |
||
279 | */ |
||
280 | if (!empty($logConditions)) { |
||
281 | foreach ($logConditions as $logCondition) { |
||
282 | if (!empty($logCondition['apps']) |
||
283 | && \in_array($app, $logCondition['apps'], true)) { |
||
284 | $minLevel = Util::DEBUG; |
||
285 | if (!empty($logCondition['logfile'])) { |
||
286 | $logConditionFile = $logCondition['logfile']; |
||
287 | break; |
||
288 | } |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | } else { |
||
293 | $app = 'no app in context'; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * check for a special log condition - this enables an increased log on |
||
298 | * a per request/user base |
||
299 | */ |
||
300 | if ($this->logConditionSatisfied === null) { |
||
301 | // default to false to just process this once per request |
||
302 | $this->logConditionSatisfied = false; |
||
303 | if (!empty($logConditions)) { |
||
304 | foreach ($logConditions as $logCondition) { |
||
305 | |||
306 | // check for secret token in the request |
||
307 | if (!empty($logCondition['shared_secret'])) { |
||
308 | $request = \OC::$server->getRequest(); |
||
309 | |||
310 | // if token is found in the request change set the log condition to satisfied |
||
311 | if ($request && \hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
||
312 | $this->logConditionSatisfied = true; |
||
313 | if (!empty($logCondition['logfile'])) { |
||
314 | $logConditionFile = $logCondition['logfile']; |
||
315 | } |
||
316 | break; |
||
317 | } |
||
318 | } |
||
319 | |||
320 | // check for user |
||
321 | if (!empty($logCondition['users'])) { |
||
322 | $userSession = \OC::$server->getUserSession(); |
||
323 | if ($userSession instanceof IUserSession) { |
||
324 | $user = $userSession->getUser(); |
||
325 | |||
326 | // if the user matches set the log condition to satisfied |
||
327 | if ($user !== null && \in_array($user->getUID(), $logCondition['users'], true)) { |
||
328 | $this->logConditionSatisfied = true; |
||
329 | if (!empty($logCondition['logfile'])) { |
||
330 | $logConditionFile = $logCondition['logfile']; |
||
331 | } |
||
332 | break; |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // if log condition is satisfied change the required log level to DEBUG |
||
341 | if ($this->logConditionSatisfied) { |
||
342 | $minLevel = Util::DEBUG; |
||
343 | } |
||
344 | |||
345 | $skipEvents = false; |
||
346 | // avoid infinite loop in case an event handler logs something |
||
347 | if ($this->inEvent) { |
||
348 | $skipEvents = true; |
||
349 | } |
||
350 | |||
351 | $formattedMessage = $this->interpolate($message, $context); |
||
352 | |||
353 | $eventArgs = [ |
||
354 | 'app' => $app, |
||
355 | 'loglevel' => $level, |
||
356 | 'message' => $message, |
||
357 | 'formattedMessage' => $formattedMessage, |
||
358 | 'context' => $context, |
||
359 | 'extraFields' => $extraFields, |
||
360 | 'exception' => $exception |
||
361 | ]; |
||
362 | |||
363 | // note: regardless of log level we let listeners receive messages |
||
364 | $this->inEvent = true; |
||
365 | View Code Duplication | if (!$skipEvents) { |
|
|
|||
366 | $event = new GenericEvent(null); |
||
367 | $event->setArguments($eventArgs); |
||
368 | $this->eventDispatcher->dispatch('log.beforewrite', $event); |
||
369 | } |
||
370 | |||
371 | if ($level >= $minLevel) { |
||
372 | $logger = $this->logger; |
||
373 | // check if logger supports extra fields |
||
374 | if (!empty($extraFields) && \is_callable($logger, 'writeExtra')) { |
||
375 | \call_user_func([$logger, 'writeExtra'], $app, $formattedMessage, $level, $logConditionFile, $extraFields); |
||
376 | } else { |
||
377 | \call_user_func([$logger, 'write'], $app, $formattedMessage, $level, $logConditionFile); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | View Code Duplication | if (!$skipEvents) { |
|
382 | $event = new GenericEvent(null); |
||
383 | $event->setArguments($eventArgs); |
||
384 | $this->eventDispatcher->dispatch('log.afterwrite', $event); |
||
385 | } |
||
386 | $this->inEvent = false; |
||
387 | } |
||
388 | |||
442 |
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.