| Conditions | 6 |
| Paths | 4 |
| Total Lines | 74 |
| Code Lines | 51 |
| 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 |
||
| 72 | protected function getFormSchema(): array |
||
| 73 | { |
||
| 74 | $events = NotificationEvent::where('is_active', true) |
||
| 75 | ->orderBy('group') |
||
| 76 | ->orderBy('name') |
||
| 77 | ->get(); |
||
| 78 | |||
| 79 | $activeChannels = NotificationChannel::where('is_active', true) |
||
| 80 | ->orderBy('title') |
||
| 81 | ->get(); |
||
| 82 | |||
| 83 | $groupedEvents = $events->groupBy('group'); |
||
| 84 | |||
| 85 | $sections = []; |
||
| 86 | |||
| 87 | foreach ($groupedEvents as $group => $groupEvents) { |
||
| 88 | $fields = []; |
||
| 89 | |||
| 90 | foreach ($groupEvents as $event) { |
||
| 91 | $channelCheckboxes = []; |
||
| 92 | |||
| 93 | foreach ($activeChannels as $channel) { |
||
| 94 | $channelCheckboxes[] = Checkbox::make("event_{$event->id}.{$channel->type}") |
||
| 95 | ->label('') |
||
| 96 | ->inline(false); |
||
| 97 | } |
||
| 98 | |||
| 99 | $fields[] = Grid::make([ |
||
| 100 | 'default' => 1, |
||
| 101 | 'md' => $activeChannels->count() + 1, |
||
| 102 | ]) |
||
| 103 | ->schema([ |
||
| 104 | Forms\Components\Placeholder::make("event_label_{$event->id}") |
||
| 105 | ->label($event->name) |
||
| 106 | ->content($event->description ?: '') |
||
| 107 | ->extraAttributes([ |
||
| 108 | 'class' => 'font-medium text-gray-900' |
||
| 109 | ]), |
||
| 110 | ...$channelCheckboxes, |
||
| 111 | ]) |
||
| 112 | ->extraAttributes([ |
||
| 113 | 'class' => 'border-b border-gray-200 py-4 first:pt-0 last:border-b-0 last:pb-0' |
||
| 114 | ]); |
||
| 115 | } |
||
| 116 | |||
| 117 | $sections[] = Section::make($group ?: 'General') |
||
| 118 | ->schema([ |
||
| 119 | Grid::make([ |
||
| 120 | 'default' => 1, |
||
| 121 | 'md' => $activeChannels->count() + 1, |
||
| 122 | ]) |
||
| 123 | ->schema([ |
||
| 124 | Forms\Components\Placeholder::make('header_label') |
||
| 125 | ->label('') |
||
| 126 | ->content(''), |
||
| 127 | ...array_map(function ($channel) { |
||
| 128 | return Forms\Components\Placeholder::make("header_{$channel->type}") |
||
| 129 | ->label($channel->title) |
||
| 130 | ->content('') |
||
| 131 | ->extraAttributes([ |
||
| 132 | 'class' => 'font-semibold text-gray-700 text-sm' |
||
| 133 | ]); |
||
| 134 | }, $activeChannels->all()), |
||
| 135 | ]) |
||
| 136 | ->extraAttributes([ |
||
| 137 | 'class' => 'border-b-2 border-gray-300 pb-3 mb-2' |
||
| 138 | ]), |
||
| 139 | ...$fields, |
||
| 140 | ]) |
||
| 141 | ->collapsible() |
||
| 142 | ->collapsed(false); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $sections; |
||
| 146 | } |
||
| 219 |