| Conditions | 18 |
| Paths | 17 |
| Total Lines | 127 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 64 | public function action_notificationsSettings_display(): void |
||
| 65 | { |
||
| 66 | global $txt, $context, $modSettings; |
||
| 67 | |||
| 68 | Txt::load('Mentions'); |
||
| 69 | |||
| 70 | // Instantiate the form |
||
| 71 | $settingsForm = new SettingsForm(SettingsForm::DB_ADAPTER); |
||
| 72 | |||
| 73 | // Initialize it with our settings |
||
| 74 | $settingsForm->setConfigVars($this->_notificationsSettings()); |
||
| 75 | |||
| 76 | // Some context stuff |
||
| 77 | $context['page_title'] = $txt['mentions_settings']; |
||
| 78 | $context['sub_template'] = 'show_settings'; |
||
| 79 | |||
| 80 | // Saving the settings? |
||
| 81 | if ($this->_req->hasQuery('save')) |
||
| 82 | { |
||
| 83 | checkSession(); |
||
| 84 | |||
| 85 | call_integration_hook('integrate_save_modify_mention_settings'); |
||
| 86 | |||
| 87 | if (!empty($this->_req->post->mentions_enabled)) |
||
| 88 | { |
||
| 89 | enableModules('mentions', ['post', 'display']); |
||
| 90 | } |
||
| 91 | else |
||
| 92 | { |
||
| 93 | disableModules('mentions', ['post', 'display']); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!empty($modSettings['hidden_notification_methods'])) |
||
| 97 | { |
||
| 98 | foreach ($modSettings['hidden_notification_methods'] as $class) |
||
| 99 | { |
||
| 100 | $this->_req->post->notifications[$class::getType()] = $class::getSettings(); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (empty($this->_req->post->notifications)) |
||
| 105 | { |
||
| 106 | $notification_methods = serialize([]); |
||
| 107 | } |
||
| 108 | else |
||
| 109 | { |
||
| 110 | $notification_methods = []; |
||
| 111 | foreach ($this->_req->post->notifications as $type => $notification) |
||
| 112 | { |
||
| 113 | if (!empty($notification['enable'])) |
||
| 114 | { |
||
| 115 | $defaults = $notification['default'] ?? []; |
||
| 116 | unset($notification['enable'], $notification['default']); |
||
| 117 | foreach ($notification as $k => $v) |
||
| 118 | { |
||
| 119 | $notification[$k] = in_array($k, $defaults) ? Notifications::DEFAULT_LEVEL : $v; |
||
| 120 | } |
||
| 121 | |||
| 122 | $notification_methods[$type] = $notification; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $notification_methods = serialize($notification_methods); |
||
| 127 | } |
||
| 128 | |||
| 129 | require_once(SUBSDIR . '/Mentions.subs.php'); |
||
| 130 | $enabled_mentions = []; |
||
| 131 | $current_settings = Util::unserialize($modSettings['notification_methods']); |
||
| 132 | |||
| 133 | // Fist hide what was visible |
||
| 134 | $modules_toggle = ['enable' => [], 'disable' => []]; |
||
| 135 | foreach ($current_settings as $type => $val) |
||
| 136 | { |
||
| 137 | if (!isset($this->_req->post->notifications[$type])) |
||
| 138 | { |
||
| 139 | toggleMentionsVisibility($type, false); |
||
| 140 | $modules_toggle['disable'][] = $type; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // Then make visible what was hidden, but only if there is anything |
||
| 145 | if (!empty($this->_req->post->notifications)) |
||
| 146 | { |
||
| 147 | foreach ($this->_req->post->notifications as $type => $val) |
||
| 148 | { |
||
| 149 | if (!isset($current_settings[$type])) |
||
| 150 | { |
||
| 151 | toggleMentionsVisibility($type, true); |
||
| 152 | $modules_toggle['enable'][] = $type; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $enabled_mentions = array_keys($this->_req->post->notifications); |
||
| 157 | } |
||
| 158 | |||
| 159 | // Let's just keep it active, there are too many reasons it should be. |
||
| 160 | require_once(SUBSDIR . '/ScheduledTasks.subs.php'); |
||
| 161 | toggleTaskStatusByName('user_access_mentions'); |
||
| 162 | |||
| 163 | // Disable or enable modules as needed |
||
| 164 | foreach ($modules_toggle as $action => $toggles) |
||
| 165 | { |
||
| 166 | if (!empty($toggles)) |
||
| 167 | { |
||
| 168 | // The modules associated with the notification (mentionmem, likes, etc.) area |
||
| 169 | $modules = getMentionsModules($toggles); |
||
| 170 | |||
| 171 | // The action will either be enabled to disable |
||
| 172 | $function = $action . 'Modules'; |
||
| 173 | |||
| 174 | // Something like enableModule('mentions', array('post', 'display'); |
||
| 175 | foreach ($modules as $key => $val) |
||
| 176 | { |
||
| 177 | $function($key, $val); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | updateSettings(['enabled_mentions' => implode(',', array_unique($enabled_mentions)), 'notification_methods' => $notification_methods]); |
||
| 183 | $settingsForm->setConfigValues((array) $this->_req->post); |
||
| 184 | $settingsForm->save(); |
||
| 185 | redirectexit('action=admin;area=featuresettings;sa=mentions'); |
||
| 186 | } |
||
| 187 | |||
| 188 | // Prepare the settings for display |
||
| 189 | $context['post_url'] = getUrl('admin', ['action' => 'admin', 'area' => 'featuresettings', 'sa' => 'mentions', 'save']); |
||
| 190 | $settingsForm->prepare(); |
||
| 191 | } |
||
| 288 |