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