| Conditions | 10 |
| Paths | 13 |
| Total Lines | 60 |
| Code Lines | 32 |
| 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 |
||
| 14 | public function handleBatch(array $records): void |
||
| 15 | { |
||
| 16 | $level = $this->level; |
||
| 17 | |||
| 18 | // filter records based on their level |
||
| 19 | $records = array_filter( |
||
| 20 | $records, |
||
| 21 | function ($record) use ($level) { |
||
| 22 | return $record['level'] >= $level; |
||
| 23 | } |
||
| 24 | ); |
||
| 25 | |||
| 26 | if (!$records) { |
||
|
|
|||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | // the record with the highest severity is the "main" one |
||
| 31 | $record = array_reduce( |
||
| 32 | $records, |
||
| 33 | function ($highest, $record) { |
||
| 34 | if ($record['level'] > $highest['level']) { |
||
| 35 | return $record; |
||
| 36 | } |
||
| 37 | |||
| 38 | return $highest; |
||
| 39 | } |
||
| 40 | ); |
||
| 41 | |||
| 42 | // the other ones are added as a context item |
||
| 43 | $logs = []; |
||
| 44 | foreach ($records as $r) { |
||
| 45 | $log = $this->processRecord($r); |
||
| 46 | |||
| 47 | $crumb = [ |
||
| 48 | 'category' => $log['channel'], |
||
| 49 | 'message' => $log['message'], |
||
| 50 | 'level' => strtolower($log['level_name']), |
||
| 51 | 'timestamp' => (float)$log['datetime']->format('U.u'), |
||
| 52 | ]; |
||
| 53 | |||
| 54 | if (array_key_exists('context', $log)) { |
||
| 55 | if ($log['channel'] === 'request' && array_key_exists('route_parameters', $log['context'])) { |
||
| 56 | $crumb['data']['route'] = $log['context']['route_parameters']['_route']; |
||
| 57 | $crumb['data']['controller'] = $log['context']['route_parameters']['_controller']; |
||
| 58 | $crumb['data']['uri'] = $log['context']['request_uri']; |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($log['channel'] === 'security' && array_key_exists('user', $log['context'])) { |
||
| 62 | $crumb['data']['user'] = $log['context']['user']['username']; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->ravenClient->breadcrumbs->record($crumb); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($logs) { |
||
| 70 | $record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->handle($record); |
||
| 74 | } |
||
| 76 |
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.