| Conditions | 19 |
| Paths | 18 |
| Total Lines | 241 |
| Code Lines | 136 |
| Lines | 20 |
| Ratio | 8.3 % |
| 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 |
||
| 74 | function ScheduledTasks() |
||
| 75 | { |
||
| 76 | global $context, $txt, $sourcedir, $smcFunc, $scripturl; |
||
| 77 | |||
| 78 | // Mama, setup the template first - cause it's like the most important bit, like pickle in a sandwich. |
||
| 79 | // ... ironically I don't like pickle. </grudge> |
||
| 80 | $context['sub_template'] = 'view_scheduled_tasks'; |
||
| 81 | $context['page_title'] = $txt['maintain_tasks']; |
||
| 82 | |||
| 83 | // Saving changes? |
||
| 84 | if (isset($_REQUEST['save']) && isset($_POST['enable_task'])) |
||
| 85 | { |
||
| 86 | checkSession(); |
||
| 87 | |||
| 88 | // We'll recalculate the dates at the end! |
||
| 89 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 90 | |||
| 91 | // Enable and disable as required. |
||
| 92 | $enablers = array(0); |
||
| 93 | foreach ($_POST['enable_task'] as $id => $enabled) |
||
| 94 | if ($enabled) |
||
| 95 | $enablers[] = (int) $id; |
||
| 96 | |||
| 97 | // Do the update! |
||
| 98 | $smcFunc['db_query']('', ' |
||
| 99 | UPDATE {db_prefix}scheduled_tasks |
||
| 100 | SET disabled = CASE WHEN id_task IN ({array_int:id_task_enable}) THEN 0 ELSE 1 END', |
||
| 101 | array( |
||
| 102 | 'id_task_enable' => $enablers, |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | |||
| 106 | // Update the "allow_expire_redirect" setting... |
||
| 107 | $get_info = $smcFunc['db_query']('', ' |
||
| 108 | SELECT disabled |
||
| 109 | FROM {db_prefix}scheduled_tasks |
||
| 110 | WHERE task = {string:remove_redirect}', |
||
| 111 | array( |
||
| 112 | 'remove_redirect' => 'remove_topic_redirect' |
||
| 113 | ) |
||
| 114 | ); |
||
| 115 | |||
| 116 | $temp = $smcFunc['db_fetch_assoc']($get_info); |
||
| 117 | $task_disabled = !empty($temp['disabled']) ? 0 : 1; |
||
| 118 | $smcFunc['db_free_result']($get_info); |
||
| 119 | |||
| 120 | updateSettings(array('allow_expire_redirect' => $task_disabled)); |
||
| 121 | |||
| 122 | // Pop along... |
||
| 123 | CalculateNextTrigger(); |
||
| 124 | } |
||
| 125 | |||
| 126 | // Want to run any of the tasks? |
||
| 127 | if (isset($_REQUEST['run']) && isset($_POST['run_task'])) |
||
| 128 | { |
||
| 129 | $task_string = ''; |
||
| 130 | |||
| 131 | // Lets figure out which ones they want to run. |
||
| 132 | $tasks = array(); |
||
| 133 | foreach ($_POST['run_task'] as $task => $dummy) |
||
| 134 | $tasks[] = (int) $task; |
||
| 135 | |||
| 136 | // Load up the tasks. |
||
| 137 | $request = $smcFunc['db_query']('', ' |
||
| 138 | SELECT id_task, task, callable |
||
| 139 | FROM {db_prefix}scheduled_tasks |
||
| 140 | WHERE id_task IN ({array_int:tasks}) |
||
| 141 | LIMIT {int:limit}', |
||
| 142 | array( |
||
| 143 | 'tasks' => $tasks, |
||
| 144 | 'limit' => count($tasks), |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | |||
| 148 | // Lets get it on! |
||
| 149 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 150 | ignore_user_abort(true); |
||
| 151 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 152 | { |
||
| 153 | // What kind of task are we handling? |
||
| 154 | View Code Duplication | if (!empty($row['callable'])) |
|
|
|
|||
| 155 | $task_string = $row['callable']; |
||
| 156 | |||
| 157 | // Default SMF task or old mods? |
||
| 158 | elseif (function_exists('scheduled_' . $row['task'])) |
||
| 159 | $task_string = 'scheduled_' . $row['task']; |
||
| 160 | |||
| 161 | // One last resource, the task name. |
||
| 162 | elseif (!empty($row['task'])) |
||
| 163 | $task_string = $row['task']; |
||
| 164 | |||
| 165 | $start_time = microtime(); |
||
| 166 | // The functions got to exist for us to use it. |
||
| 167 | if (empty($task_string)) |
||
| 168 | continue; |
||
| 169 | |||
| 170 | // Try to stop a timeout, this would be bad... |
||
| 171 | @set_time_limit(300); |
||
| 172 | if (function_exists('apache_reset_timeout')) |
||
| 173 | @apache_reset_timeout(); |
||
| 174 | |||
| 175 | // Get the callable. |
||
| 176 | $callable_task = call_helper($task_string, true); |
||
| 177 | |||
| 178 | // Perform the task. |
||
| 179 | if (!empty($callable_task)) |
||
| 180 | $completed = call_user_func($callable_task); |
||
| 181 | |||
| 182 | else |
||
| 183 | $completed = false; |
||
| 184 | |||
| 185 | // Log that we did it ;) |
||
| 186 | View Code Duplication | if ($completed) |
|
| 187 | { |
||
| 188 | $total_time = round(array_sum(explode(' ', microtime())) - array_sum(explode(' ', $start_time)), 3); |
||
| 189 | $smcFunc['db_insert']('', |
||
| 190 | '{db_prefix}log_scheduled_tasks', |
||
| 191 | array('id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float'), |
||
| 192 | array($row['id_task'], time(), $total_time), |
||
| 193 | array('id_task') |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | $smcFunc['db_free_result']($request); |
||
| 198 | |||
| 199 | // If we had any errors, push them to session so we can pick them up next time to tell the user. |
||
| 200 | if (!empty($context['scheduled_errors'])) |
||
| 201 | $_SESSION['st_error'] = $context['scheduled_errors']; |
||
| 202 | |||
| 203 | redirectexit('action=admin;area=scheduledtasks;done'); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (isset($_SESSION['st_error'])) |
||
| 207 | { |
||
| 208 | $context['scheduled_errors'] = $_SESSION['st_error']; |
||
| 209 | unset ($_SESSION['st_error']); |
||
| 210 | } |
||
| 211 | |||
| 212 | $listOptions = array( |
||
| 213 | 'id' => 'scheduled_tasks', |
||
| 214 | 'title' => $txt['maintain_tasks'], |
||
| 215 | 'base_href' => $scripturl . '?action=admin;area=scheduledtasks', |
||
| 216 | 'get_items' => array( |
||
| 217 | 'function' => 'list_getScheduledTasks', |
||
| 218 | ), |
||
| 219 | 'columns' => array( |
||
| 220 | 'name' => array( |
||
| 221 | 'header' => array( |
||
| 222 | 'value' => $txt['scheduled_tasks_name'], |
||
| 223 | 'style' => 'width: 40%;', |
||
| 224 | ), |
||
| 225 | 'data' => array( |
||
| 226 | 'sprintf' => array( |
||
| 227 | 'format' => ' |
||
| 228 | <a href="' . $scripturl . '?action=admin;area=scheduledtasks;sa=taskedit;tid=%1$d">%2$s</a><br><span class="smalltext">%3$s</span>', |
||
| 229 | 'params' => array( |
||
| 230 | 'id' => false, |
||
| 231 | 'name' => false, |
||
| 232 | 'desc' => false, |
||
| 233 | ), |
||
| 234 | ), |
||
| 235 | ), |
||
| 236 | ), |
||
| 237 | 'next_due' => array( |
||
| 238 | 'header' => array( |
||
| 239 | 'value' => $txt['scheduled_tasks_next_time'], |
||
| 240 | ), |
||
| 241 | 'data' => array( |
||
| 242 | 'db' => 'next_time', |
||
| 243 | 'class' => 'smalltext', |
||
| 244 | ), |
||
| 245 | ), |
||
| 246 | 'regularity' => array( |
||
| 247 | 'header' => array( |
||
| 248 | 'value' => $txt['scheduled_tasks_regularity'], |
||
| 249 | ), |
||
| 250 | 'data' => array( |
||
| 251 | 'db' => 'regularity', |
||
| 252 | 'class' => 'smalltext', |
||
| 253 | ), |
||
| 254 | ), |
||
| 255 | 'run_now' => array( |
||
| 256 | 'header' => array( |
||
| 257 | 'value' => $txt['scheduled_tasks_run_now'], |
||
| 258 | 'style' => 'width: 12%;', |
||
| 259 | 'class' => 'centercol', |
||
| 260 | ), |
||
| 261 | 'data' => array( |
||
| 262 | 'sprintf' => array( |
||
| 263 | 'format' => |
||
| 264 | '<input type="checkbox" name="run_task[%1$d]" id="run_task_%1$d">', |
||
| 265 | 'params' => array( |
||
| 266 | 'id' => false, |
||
| 267 | ), |
||
| 268 | ), |
||
| 269 | 'class' => 'centercol', |
||
| 270 | ), |
||
| 271 | ), |
||
| 272 | 'enabled' => array( |
||
| 273 | 'header' => array( |
||
| 274 | 'value' => $txt['scheduled_tasks_enabled'], |
||
| 275 | 'style' => 'width: 6%;', |
||
| 276 | 'class' => 'centercol', |
||
| 277 | ), |
||
| 278 | 'data' => array( |
||
| 279 | 'sprintf' => array( |
||
| 280 | 'format' => |
||
| 281 | '<input type="hidden" name="enable_task[%1$d]" id="task_%1$d" value="0"><input type="checkbox" name="enable_task[%1$d]" id="task_check_%1$d" %2$s>', |
||
| 282 | 'params' => array( |
||
| 283 | 'id' => false, |
||
| 284 | 'checked_state' => false, |
||
| 285 | ), |
||
| 286 | ), |
||
| 287 | 'class' => 'centercol', |
||
| 288 | ), |
||
| 289 | ), |
||
| 290 | ), |
||
| 291 | 'form' => array( |
||
| 292 | 'href' => $scripturl . '?action=admin;area=scheduledtasks', |
||
| 293 | ), |
||
| 294 | 'additional_rows' => array( |
||
| 295 | array( |
||
| 296 | 'position' => 'below_table_data', |
||
| 297 | 'value' => ' |
||
| 298 | <input type="submit" name="save" value="' . $txt['scheduled_tasks_save_changes'] . '" class="button_submit"> |
||
| 299 | <input type="submit" name="run" value="' . $txt['scheduled_tasks_run_now'] . '" class="button_submit">', |
||
| 300 | ), |
||
| 301 | array( |
||
| 302 | 'position' => 'after_title', |
||
| 303 | 'value' => $txt['scheduled_tasks_time_offset'], |
||
| 304 | ), |
||
| 305 | ), |
||
| 306 | ); |
||
| 307 | |||
| 308 | require_once($sourcedir . '/Subs-List.php'); |
||
| 309 | createList($listOptions); |
||
| 310 | |||
| 311 | $context['sub_template'] = 'view_scheduled_tasks'; |
||
| 312 | |||
| 313 | $context['tasks_were_run'] = isset($_GET['done']); |
||
| 314 | } |
||
| 315 | |||
| 633 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.