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