| Conditions | 20 |
| Paths | 648 |
| Total Lines | 116 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 52 | function RemindPick() |
||
| 53 | { |
||
| 54 | global $context, $txt, $scripturl, $sourcedir, $user_info, $webmaster_email, $smcFunc, $language, $modSettings; |
||
| 55 | |||
| 56 | checkSession(); |
||
| 57 | validateToken('remind'); |
||
| 58 | createToken('remind'); |
||
| 59 | |||
| 60 | // Coming with a known ID? |
||
| 61 | if (!empty($_REQUEST['uid'])) |
||
| 62 | { |
||
| 63 | $where = 'id_member = {int:id_member}'; |
||
| 64 | $where_params['id_member'] = (int) $_REQUEST['uid']; |
||
|
|
|||
| 65 | } |
||
| 66 | elseif (isset($_POST['user']) && $_POST['user'] != '') |
||
| 67 | { |
||
| 68 | $where = 'member_name = {string:member_name}'; |
||
| 69 | $where_params['member_name'] = $_POST['user']; |
||
| 70 | $where_params['email_address'] = $_POST['user']; |
||
| 71 | } |
||
| 72 | |||
| 73 | // You must enter a username/email address. |
||
| 74 | if (empty($where)) |
||
| 75 | fatal_lang_error('username_no_exist', false); |
||
| 76 | |||
| 77 | // Make sure we are not being slammed |
||
| 78 | // Don't call this if you're coming from the "Choose a reminder type" page - otherwise you'll likely get an error |
||
| 79 | if (!isset($_POST['reminder_type']) || !in_array($_POST['reminder_type'], array('email', 'secret'))) |
||
| 80 | { |
||
| 81 | spamProtection('remind'); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Find the user! |
||
| 85 | $request = $smcFunc['db_query']('', ' |
||
| 86 | SELECT id_member, real_name, member_name, email_address, is_activated, validation_code, lngfile, secret_question |
||
| 87 | FROM {db_prefix}members |
||
| 88 | WHERE ' . $where . ' |
||
| 89 | LIMIT 1', |
||
| 90 | $where_params |
||
| 91 | ); |
||
| 92 | // Maybe email? |
||
| 93 | if ($smcFunc['db_num_rows']($request) == 0 && empty($_REQUEST['uid'])) |
||
| 94 | { |
||
| 95 | $smcFunc['db_free_result']($request); |
||
| 96 | |||
| 97 | $request = $smcFunc['db_query']('', ' |
||
| 98 | SELECT id_member, real_name, member_name, email_address, is_activated, validation_code, lngfile, secret_question |
||
| 99 | FROM {db_prefix}members |
||
| 100 | WHERE email_address = {string:email_address} |
||
| 101 | LIMIT 1', |
||
| 102 | $where_params |
||
| 103 | ); |
||
| 104 | if ($smcFunc['db_num_rows']($request) == 0) |
||
| 105 | fatal_lang_error('no_user_with_email', false); |
||
| 106 | } |
||
| 107 | |||
| 108 | $row = $smcFunc['db_fetch_assoc']($request); |
||
| 109 | $smcFunc['db_free_result']($request); |
||
| 110 | |||
| 111 | // If the user isn't activated/approved, give them some feedback on what to do next. |
||
| 112 | if ($row['is_activated'] != 1) |
||
| 113 | { |
||
| 114 | // Awaiting approval... |
||
| 115 | if (trim($row['validation_code']) == '') |
||
| 116 | fatal_error(sprintf($txt['registration_not_approved'], $scripturl . '?action=activate;user=' . $_POST['user']), false); |
||
| 117 | else |
||
| 118 | fatal_error(sprintf($txt['registration_not_activated'], $scripturl . '?action=activate;user=' . $_POST['user']), false); |
||
| 119 | } |
||
| 120 | |||
| 121 | // You can't get emailed if you have no email address. |
||
| 122 | $row['email_address'] = trim($row['email_address']); |
||
| 123 | if ($row['email_address'] == '') |
||
| 124 | fatal_error($txt['no_reminder_email'] . '<br>' . $txt['send_email'] . ' <a href="mailto:' . $webmaster_email . '">webmaster</a> ' . $txt['to_ask_password'] . '.'); |
||
| 125 | |||
| 126 | // If they have no secret question then they can only get emailed the item, or they are requesting the email, send them an email. |
||
| 127 | if (empty($row['secret_question']) || (isset($_POST['reminder_type']) && $_POST['reminder_type'] == 'email')) |
||
| 128 | { |
||
| 129 | // Randomly generate a new password, with only alpha numeric characters that is a max length of 10 chars. |
||
| 130 | require_once($sourcedir . '/Subs-Members.php'); |
||
| 131 | $password = generateValidationCode(); |
||
| 132 | |||
| 133 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 134 | $replacements = array( |
||
| 135 | 'REALNAME' => $row['real_name'], |
||
| 136 | 'REMINDLINK' => $scripturl . '?action=reminder;sa=setpassword;u=' . $row['id_member'] . ';code=' . $password, |
||
| 137 | 'IP' => $user_info['ip'], |
||
| 138 | 'MEMBERNAME' => $row['member_name'], |
||
| 139 | ); |
||
| 140 | |||
| 141 | $emaildata = loadEmailTemplate('forgot_password', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']); |
||
| 142 | $context['description'] = $txt['reminder_sent']; |
||
| 143 | |||
| 144 | sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, 'reminder', $emaildata['is_html'], 1); |
||
| 145 | |||
| 146 | // Set the password in the database. |
||
| 147 | updateMemberData($row['id_member'], array('validation_code' => substr(md5($password), 0, 10))); |
||
| 148 | |||
| 149 | // Set up the template. |
||
| 150 | $context['sub_template'] = 'sent'; |
||
| 151 | |||
| 152 | // Don't really. |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | // Otherwise are ready to answer the question? |
||
| 156 | elseif (isset($_POST['reminder_type']) && $_POST['reminder_type'] == 'secret') |
||
| 157 | { |
||
| 158 | return SecretAnswerInput(); |
||
| 159 | } |
||
| 160 | |||
| 161 | // No we're here setup the context for template number 2! |
||
| 162 | $context['sub_template'] = 'reminder_pick'; |
||
| 163 | $context['current_member'] = array( |
||
| 164 | 'id' => $row['id_member'], |
||
| 165 | 'name' => $row['member_name'], |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 401 | ?> |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.