| Conditions | 20 |
| Paths | 96 |
| Total Lines | 72 |
| Lines | 4 |
| Ratio | 5.56 % |
| Changes | 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 |
||
| 30 | function LockTopic() |
||
| 31 | { |
||
| 32 | global $topic, $user_info, $sourcedir, $board, $smcFunc; |
||
|
|
|||
| 33 | |||
| 34 | // Just quit if there's no topic to lock. |
||
| 35 | if (empty($topic)) |
||
| 36 | fatal_lang_error('not_a_topic', false); |
||
| 37 | |||
| 38 | checkSession('get'); |
||
| 39 | |||
| 40 | // Get Subs-Post.php for sendNotifications. |
||
| 41 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 42 | |||
| 43 | // Find out who started the topic - in case User Topic Locking is enabled. |
||
| 44 | $request = $smcFunc['db_query']('', ' |
||
| 45 | SELECT id_member_started, locked |
||
| 46 | FROM {db_prefix}topics |
||
| 47 | WHERE id_topic = {int:current_topic} |
||
| 48 | LIMIT 1', |
||
| 49 | array( |
||
| 50 | 'current_topic' => $topic, |
||
| 51 | ) |
||
| 52 | ); |
||
| 53 | list ($starter, $locked) = $smcFunc['db_fetch_row']($request); |
||
| 54 | $smcFunc['db_free_result']($request); |
||
| 55 | |||
| 56 | // Can you lock topics here, mister? |
||
| 57 | $user_lock = !allowedTo('lock_any'); |
||
| 58 | if ($user_lock && $starter == $user_info['id']) |
||
| 59 | isAllowedTo('lock_own'); |
||
| 60 | else |
||
| 61 | isAllowedTo('lock_any'); |
||
| 62 | |||
| 63 | // Another moderator got the job done first? |
||
| 64 | View Code Duplication | if (isset($_GET['sa']) && $_GET['sa'] == 'unlock' && $locked == '0') |
|
| 65 | fatal_lang_error('error_topic_locked_already', false); |
||
| 66 | elseif (isset($_GET['sa']) && $_GET['sa'] == 'lock' && ($locked == '1' || $locked == '2')) |
||
| 67 | fatal_lang_error('error_topic_unlocked_already', false); |
||
| 68 | |||
| 69 | // Locking with high privileges. |
||
| 70 | if ($locked == '0' && !$user_lock) |
||
| 71 | $locked = '1'; |
||
| 72 | // Locking with low privileges. |
||
| 73 | elseif ($locked == '0') |
||
| 74 | $locked = '2'; |
||
| 75 | // Unlocking - make sure you don't unlock what you can't. |
||
| 76 | elseif ($locked == '2' || ($locked == '1' && !$user_lock)) |
||
| 77 | $locked = '0'; |
||
| 78 | // You cannot unlock this! |
||
| 79 | else |
||
| 80 | fatal_lang_error('locked_by_admin', 'user'); |
||
| 81 | |||
| 82 | // Actually lock the topic in the database with the new value. |
||
| 83 | $smcFunc['db_query']('', ' |
||
| 84 | UPDATE {db_prefix}topics |
||
| 85 | SET locked = {int:locked} |
||
| 86 | WHERE id_topic = {int:current_topic}', |
||
| 87 | array( |
||
| 88 | 'current_topic' => $topic, |
||
| 89 | 'locked' => $locked, |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | |||
| 93 | // If they are allowed a "moderator" permission, log it in the moderator log. |
||
| 94 | if (!$user_lock) |
||
| 95 | logAction($locked ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $board)); |
||
| 96 | // Notify people that this topic has been locked? |
||
| 97 | sendNotifications($topic, empty($locked) ? 'unlock' : 'lock'); |
||
| 98 | |||
| 99 | // Back to the topic! |
||
| 100 | redirectexit('topic=' . $topic . '.' . $_REQUEST['start'] . ';moderate'); |
||
| 101 | } |
||
| 102 | |||
| 169 | ?> |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state