| Conditions | 10 |
| Paths | 10 |
| Total Lines | 58 |
| 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 |
||
| 41 | public function handleBatch(array $records): void |
||
| 42 | { |
||
| 43 | $level = $this->level; |
||
| 44 | |||
| 45 | // filter records based on their level |
||
| 46 | $records = array_filter( |
||
|
|
|||
| 47 | $records, |
||
| 48 | function ($record) use ($level) { |
||
| 49 | return $record['level'] >= $level; |
||
| 50 | } |
||
| 51 | ); |
||
| 52 | |||
| 53 | if (!$records) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | // the record with the highest severity is the "main" one |
||
| 58 | $record = array_reduce( |
||
| 59 | $records, |
||
| 60 | function ($highest, $record) { |
||
| 61 | if ($record['level'] > $highest['level']) { |
||
| 62 | return $record; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $highest; |
||
| 66 | }, |
||
| 67 | reset($records) |
||
| 68 | ); |
||
| 69 | |||
| 70 | foreach ($records as $r) { |
||
| 71 | $log = $this->processRecord($r); |
||
| 72 | |||
| 73 | $date = $log['datetime'] ?? time(); |
||
| 74 | |||
| 75 | $crumb = [ |
||
| 76 | 'category' => $log['channel'], |
||
| 77 | 'message' => $log['message'], |
||
| 78 | 'level' => strtolower($log['level_name']), |
||
| 79 | 'timestamp' => (float) ($date instanceof \DateTimeInterface ? $date->format('U.u') : $date), |
||
| 80 | ]; |
||
| 81 | |||
| 82 | if (array_key_exists('context', $log)) { |
||
| 83 | if ('request' === $log['channel'] && array_key_exists('route_parameters', $log['context'])) { |
||
| 84 | $crumb['data']['route'] = $log['context']['route_parameters']['_route']; |
||
| 85 | $crumb['data']['controller'] = $log['context']['route_parameters']['_controller']; |
||
| 86 | $crumb['data']['uri'] = $log['context']['request_uri']; |
||
| 87 | } elseif ('security' === $log['channel'] && array_key_exists('user', $log['context'])) { |
||
| 88 | $crumb['data']['user'] = $log['context']['user']['username']; |
||
| 89 | } else { |
||
| 90 | $crumb['data'] = $log['context']; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->ravenClient->breadcrumbs->record($crumb); |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->handle($record); |
||
| 98 | } |
||
| 99 | |||
| 178 |