| Conditions | 11 |
| Paths | 23 |
| Total Lines | 62 |
| Code Lines | 33 |
| 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 |
||
| 36 | public function handleBatch(array $records): void |
||
| 37 | { |
||
| 38 | $level = $this->level; |
||
| 39 | |||
| 40 | // filter records based on their level |
||
| 41 | $records = array_filter( |
||
| 42 | $records, |
||
| 43 | function ($record) use ($level) { |
||
| 44 | return $record['level'] >= $level; |
||
| 45 | } |
||
| 46 | ); |
||
| 47 | |||
| 48 | if (!$records) { |
||
|
|
|||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | // the record with the highest severity is the "main" one |
||
| 53 | $record = array_reduce( |
||
| 54 | $records, |
||
| 55 | function ($highest, $record) { |
||
| 56 | if ($record['level'] > $highest['level']) { |
||
| 57 | return $record; |
||
| 58 | } |
||
| 59 | |||
| 60 | return $highest; |
||
| 61 | } |
||
| 62 | ); |
||
| 63 | |||
| 64 | // the other ones are added as a context item |
||
| 65 | $logs = []; |
||
| 66 | foreach ($records as $r) { |
||
| 67 | $log = $this->processRecord($r); |
||
| 68 | |||
| 69 | $date = $log['datetime'] ?? time(); |
||
| 70 | |||
| 71 | $crumb = [ |
||
| 72 | 'category' => $log['channel'], |
||
| 73 | 'message' => $log['message'], |
||
| 74 | 'level' => strtolower($log['level_name']), |
||
| 75 | 'timestamp' => (float)($date instanceof \DateTime ? $date->format('U.u') : $date), |
||
| 76 | ]; |
||
| 77 | |||
| 78 | if (array_key_exists('context', $log)) { |
||
| 79 | if ($log['channel'] === 'request' && array_key_exists('route_parameters', $log['context'])) { |
||
| 80 | $crumb['data']['route'] = $log['context']['route_parameters']['_route']; |
||
| 81 | $crumb['data']['controller'] = $log['context']['route_parameters']['_controller']; |
||
| 82 | $crumb['data']['uri'] = $log['context']['request_uri']; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($log['channel'] === 'security' && array_key_exists('user', $log['context'])) { |
||
| 86 | $crumb['data']['user'] = $log['context']['user']['username']; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->ravenClient->breadcrumbs->record($crumb); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($logs) { |
||
| 94 | $record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs); |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->handle($record); |
||
| 98 | } |
||
| 176 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.