| Conditions | 11 |
| Paths | 12 |
| Total Lines | 55 |
| Lines | 4 |
| Ratio | 7.27 % |
| 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 |
||
| 113 | function Sticky() |
||
| 114 | { |
||
| 115 | global $topic, $board, $sourcedir, $smcFunc; |
||
| 116 | |||
| 117 | // Make sure the user can sticky it, and they are stickying *something*. |
||
| 118 | isAllowedTo('make_sticky'); |
||
| 119 | |||
| 120 | // You can't sticky a board or something! |
||
| 121 | if (empty($topic)) |
||
| 122 | fatal_lang_error('not_a_topic', false); |
||
| 123 | |||
| 124 | checkSession('get'); |
||
| 125 | |||
| 126 | // We need Subs-Post.php for the sendNotifications() function. |
||
| 127 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 128 | |||
| 129 | // Is this topic already stickied, or no? |
||
| 130 | $request = $smcFunc['db_query']('', ' |
||
| 131 | SELECT is_sticky |
||
| 132 | FROM {db_prefix}topics |
||
| 133 | WHERE id_topic = {int:current_topic} |
||
| 134 | LIMIT 1', |
||
| 135 | array( |
||
| 136 | 'current_topic' => $topic, |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | list ($is_sticky) = $smcFunc['db_fetch_row']($request); |
||
| 140 | $smcFunc['db_free_result']($request); |
||
| 141 | |||
| 142 | // Another moderator got the job done first? |
||
| 143 | View Code Duplication | if (isset($_GET['sa']) && $_GET['sa'] == 'nonsticky' && $is_sticky == '0') |
|
| 144 | fatal_lang_error('error_topic_nonsticky_already', false); |
||
| 145 | elseif (isset($_GET['sa']) && $_GET['sa'] == 'sticky' && $is_sticky == '1') |
||
| 146 | fatal_lang_error('error_topic_sticky_already', false); |
||
| 147 | |||
| 148 | // Toggle the sticky value.... pretty simple ;). |
||
| 149 | $smcFunc['db_query']('', ' |
||
| 150 | UPDATE {db_prefix}topics |
||
| 151 | SET is_sticky = {int:is_sticky} |
||
| 152 | WHERE id_topic = {int:current_topic}', |
||
| 153 | array( |
||
| 154 | 'current_topic' => $topic, |
||
| 155 | 'is_sticky' => empty($is_sticky) ? 1 : 0, |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | |||
| 159 | // Log this sticky action - always a moderator thing. |
||
| 160 | logAction(empty($is_sticky) ? 'sticky' : 'unsticky', array('topic' => $topic, 'board' => $board)); |
||
| 161 | // Notify people that this topic has been stickied? |
||
| 162 | if (empty($is_sticky)) |
||
| 163 | sendNotifications($topic, 'sticky'); |
||
| 164 | |||
| 165 | // Take them back to the now stickied topic. |
||
| 166 | redirectexit('topic=' . $topic . '.' . $_REQUEST['start'] . ';moderate'); |
||
| 167 | } |
||
| 168 | |||
| 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