| Conditions | 2 |
| Paths | 2 |
| Total Lines | 53 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 21 | protected function getData(): array |
||
| 22 | { |
||
| 23 | $channels = NotificationChannel::where('is_active', true)->get(); |
||
| 24 | $labels = []; |
||
| 25 | $sentData = []; |
||
| 26 | $openedData = []; |
||
| 27 | $clickedData = []; |
||
| 28 | $colors = [ |
||
| 29 | 'rgba(59, 130, 246, 0.8)', |
||
| 30 | 'rgba(16, 185, 129, 0.8)', |
||
| 31 | 'rgba(245, 158, 11, 0.8)', |
||
| 32 | 'rgba(239, 68, 68, 0.8)', |
||
| 33 | 'rgba(139, 92, 246, 0.8)', |
||
| 34 | 'rgba(236, 72, 153, 0.8)', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | foreach ($channels as $index => $channel) { |
||
| 38 | $labels[] = $channel->title; |
||
| 39 | |||
| 40 | $sent = Notification::where('channel', $channel->type) |
||
| 41 | ->where('status', 'sent') |
||
| 42 | ->count(); |
||
| 43 | $opened = Notification::where('channel', $channel->type) |
||
| 44 | ->whereNotNull('opened_at') |
||
| 45 | ->count(); |
||
| 46 | $clicked = Notification::where('channel', $channel->type) |
||
| 47 | ->whereNotNull('clicked_at') |
||
| 48 | ->count(); |
||
| 49 | |||
| 50 | $sentData[] = $sent; |
||
| 51 | $openedData[] = $opened; |
||
| 52 | $clickedData[] = $clicked; |
||
| 53 | } |
||
| 54 | |||
| 55 | return [ |
||
| 56 | 'datasets' => [ |
||
| 57 | [ |
||
| 58 | 'label' => 'Sent', |
||
| 59 | 'data' => $sentData, |
||
| 60 | 'backgroundColor' => $colors[0], |
||
| 61 | ], |
||
| 62 | [ |
||
| 63 | 'label' => 'Opened', |
||
| 64 | 'data' => $openedData, |
||
| 65 | 'backgroundColor' => $colors[1], |
||
| 66 | ], |
||
| 67 | [ |
||
| 68 | 'label' => 'Clicked', |
||
| 69 | 'data' => $clickedData, |
||
| 70 | 'backgroundColor' => $colors[2], |
||
| 71 | ], |
||
| 72 | ], |
||
| 73 | 'labels' => $labels, |
||
| 74 | ]; |
||
| 100 |