| Conditions | 14 |
| Paths | 37 |
| Total Lines | 122 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 236 | function SaveButton() |
||
| 237 | { |
||
| 238 | global $context, $txt; |
||
| 239 | |||
| 240 | if (isset($_POST['submit'])) |
||
| 241 | { |
||
| 242 | $post_errors = array(); |
||
| 243 | $required_fields = array( |
||
| 244 | 'name', |
||
| 245 | 'link', |
||
| 246 | 'parent', |
||
| 247 | ); |
||
| 248 | $member_groups = array_column($this->um->list_groups('-3', 1), 'id'); |
||
| 249 | $button_names = $this->um->getButtonNames(); |
||
| 250 | $args = array( |
||
| 251 | 'in' => FILTER_VALIDATE_INT, |
||
| 252 | 'name' => FILTER_UNSAFE_RAW, |
||
| 253 | 'position' => array( |
||
| 254 | 'filter' => FILTER_CALLBACK, |
||
| 255 | 'options' => function ($v) |
||
| 256 | { |
||
| 257 | return in_array($v, ['before', 'child_of', 'after']) ? $v : false; |
||
| 258 | }, |
||
| 259 | ), |
||
| 260 | 'parent' => array( |
||
| 261 | 'filter' => FILTER_CALLBACK, |
||
| 262 | 'options' => function ($v) use ($button_names) |
||
| 263 | { |
||
| 264 | return isset($button_names[$v]) ? $v : false; |
||
| 265 | }, |
||
| 266 | ), |
||
| 267 | 'type' => array( |
||
| 268 | 'filter' => FILTER_CALLBACK, |
||
| 269 | 'options' => function ($v) |
||
| 270 | { |
||
| 271 | return in_array($v, ['forum', 'external']) ? $v : false; |
||
| 272 | }, |
||
| 273 | ), |
||
| 274 | 'link' => FILTER_UNSAFE_RAW, |
||
| 275 | 'permissions' => array( |
||
| 276 | 'filter' => FILTER_CALLBACK, |
||
| 277 | 'flags' => FILTER_REQUIRE_ARRAY, |
||
| 278 | 'options' => function ($v) use ($member_groups) |
||
| 279 | { |
||
| 280 | return in_array($v, $member_groups) ? $v : false; |
||
| 281 | }, |
||
| 282 | ), |
||
| 283 | 'status' => array( |
||
| 284 | 'filter' => FILTER_CALLBACK, |
||
| 285 | 'options' => function ($v) |
||
| 286 | { |
||
| 287 | return in_array($v, ['active', 'inactive']) ? $v : false; |
||
| 288 | }, |
||
| 289 | ), |
||
| 290 | 'target' => array( |
||
| 291 | 'filter' => FILTER_CALLBACK, |
||
| 292 | 'options' => function ($v) |
||
| 293 | { |
||
| 294 | return in_array($v, ['_self', '_blank']) ? $v : false; |
||
| 295 | }, |
||
| 296 | ), |
||
| 297 | ); |
||
| 298 | |||
| 299 | // Make sure we grab all of the content |
||
| 300 | $menu_entry = array_replace( |
||
| 301 | array( |
||
| 302 | 'target' => '_self', |
||
| 303 | 'type' => 'forum', |
||
| 304 | 'position' => 'before', |
||
| 305 | 'status' => 'active', |
||
| 306 | 'parent' => 'home', |
||
| 307 | ), |
||
| 308 | filter_input_array(INPUT_POST, $args) |
||
| 309 | ); |
||
| 310 | |||
| 311 | // These fields are required! |
||
| 312 | foreach ($required_fields as $required_field) |
||
| 313 | if (empty($menu_entry[$required_field])) |
||
| 314 | $post_errors[$required_field] = 'um_menu_empty_' . $required_field; |
||
| 315 | |||
| 316 | // Stop making numeric names! |
||
| 317 | if (is_numeric($menu_entry['name'])) |
||
| 318 | $post_errors['name'] = 'um_menu_numeric'; |
||
| 319 | |||
| 320 | // Let's make sure you're not trying to make a name that's already taken. |
||
| 321 | $check = $this->um->checkButton($menu_entry['id'], $menu_entry['name']); |
||
| 322 | if ($check > 0) |
||
| 323 | $post_errors['name'] = 'um_menu_mysql'; |
||
| 324 | |||
| 325 | // I see you made it to the final stage, my young padawan. |
||
| 326 | if (empty($post_errors)) |
||
| 327 | { |
||
| 328 | $this->um->saveButton($menu_entry); |
||
| 329 | $this->um->rebuildMenu(); |
||
| 330 | |||
| 331 | |||
| 332 | // Before we leave, we must clear the cache. See, SMF |
||
| 333 | // caches its menu at level 2 or higher. |
||
| 334 | clean_cache('menu_buttons'); |
||
| 335 | |||
| 336 | redirectexit('action=admin;area=umen'); |
||
| 337 | } |
||
| 338 | else |
||
| 339 | { |
||
| 340 | $context['page_title'] = $txt['um_menu_edit_title']; |
||
| 341 | $context['post_error'] = $post_errors; |
||
| 342 | $context['error_title'] = empty($menu_entry['id']) |
||
| 343 | ? 'um_menu_errors_create' |
||
| 344 | : 'um_menu_errors_modify'; |
||
| 345 | $context['button_data'] = array( |
||
| 346 | 'name' => $menu_entry['name'], |
||
| 347 | 'type' => $menu_entry['type'], |
||
| 348 | 'target' => $menu_entry['target'], |
||
| 349 | 'position' => $menu_entry['position'], |
||
| 350 | 'link' => $menu_entry['link'], |
||
| 351 | 'parent' => $menu_entry['parent'], |
||
| 352 | 'permissions' => $this->um->list_groups( |
||
| 353 | implode(',', array_filter($menu_entry['permissions'], 'strlen')), |
||
| 354 | 1 |
||
| 355 | ), |
||
| 356 | 'status' => $menu_entry['status'], |
||
| 357 | 'id' => $menu_entry['id'], |
||
| 358 | ); |
||
| 401 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.