| Conditions | 9 |
| Paths | 24 |
| Total Lines | 77 |
| Code Lines | 41 |
| Lines | 20 |
| Ratio | 25.97 % |
| 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 |
||
| 25 | function reset_password() |
||
| 26 | { |
||
| 27 | $app = \Slim\Slim::getInstance(); |
||
| 28 | $final_global_template_vars = $app->config('final_global_template_vars'); |
||
| 29 | require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/register_account.class.php"; |
||
| 30 | require_once $_SERVER["PATH_TO_VENDOR"] . "phpmailer/phpmailer/PHPMailerAutoload.php"; |
||
| 31 | $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]); |
||
| 32 | $db_resource = $db_conn->get_resource(); |
||
| 33 | $register_account = new \PHPSkeleton\RegisterAccount($db_resource, $final_global_template_vars["session_key"]); |
||
| 34 | $mail = new PHPMailer(); |
||
| 35 | $posted_data = $app->request()->post() ? $app->request()->post() : false; |
||
| 36 | $account_email_exists = false; |
||
| 37 | |||
| 38 | // Is the email address in the database? |
||
| 39 | View Code Duplication | if ($posted_data) { |
|
| 40 | $account_email_exists = $register_account->account_email_exists($posted_data["user_account_email"]); |
||
| 41 | if (!$account_email_exists) { |
||
|
|
|||
| 42 | $app->flash('message', 'The entered email address was not found in our database.'); |
||
| 43 | $app->redirect($final_global_template_vars["path_to_this_module"]."/password/"); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | // If there are no errors, process posted data and email to user |
||
| 48 | if ($account_email_exists && $posted_data) { |
||
| 49 | |||
| 50 | $emailed_hash = md5(rand(0, 1000)); |
||
| 51 | // Attempt to update the emailed_hash and set account to inactive (returns boolean) |
||
| 52 | $updated = $register_account->update_emailed_hash($account_email_exists['user_account_id'], $emailed_hash); |
||
| 53 | |||
| 54 | if ($updated) { |
||
| 55 | |||
| 56 | // Prepare the email... |
||
| 57 | // The email subject. |
||
| 58 | $subject = 'Reset Password'; |
||
| 59 | // The message, including the link. |
||
| 60 | $message = '<h2>Reset Your Password</h2> |
||
| 61 | <hr> |
||
| 62 | <p>Please click this link to reset your password:<br /> |
||
| 63 | <a href="http://'.$_SERVER["SERVER_NAME"].'/user_account/reset/?user_account_email='.$account_email_exists['user_account_email'].'&emailed_hash='.$emailed_hash.'">http://'.$_SERVER["SERVER_NAME"].'/user_account/reset/?user_account_email='.$account_email_exists['user_account_email'].'&emailed_hash='.$emailed_hash.'</a></p>'; |
||
| 64 | |||
| 65 | // For the ability to send emails from an AWS EC2 instance... |
||
| 66 | // If you need this functionality, you can configure the settings accordingly in /default_global_settings.php |
||
| 67 | View Code Duplication | if ($final_global_template_vars["hosting_vendor"] && ($final_global_template_vars["hosting_vendor"] == "aws_ec2")) { |
|
| 68 | |||
| 69 | $email = array(); |
||
| 70 | require_once $final_global_template_vars["path_to_smtp_settings"]; |
||
| 71 | // SMTP Settings |
||
| 72 | $mail->IsSMTP(); |
||
| 73 | $mail->SMTPAuth = $email['settings']['smtpauth']; |
||
| 74 | $mail->SMTPSecure = $email['settings']['smtpsecure']; |
||
| 75 | $mail->Host = $email['settings']['host']; |
||
| 76 | $mail->Username = $email['settings']['username']; |
||
| 77 | $mail->Password = $email['settings']['password']; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | // From (verified email address). |
||
| 82 | $mail->SetFrom($final_global_template_vars["send_emails_from"], $final_global_template_vars["site_name"].' Accounts'); |
||
| 83 | // Subject |
||
| 84 | $mail->Subject = $subject; |
||
| 85 | // Message |
||
| 86 | $mail->MsgHTML($message); |
||
| 87 | // Recipient |
||
| 88 | $mail->AddAddress($posted_data['user_account_email']); |
||
| 89 | // Send the email. |
||
| 90 | $mail->Send(); |
||
| 91 | |||
| 92 | $app->flash('message', 'Thank you. Further instructions are being sent to your email address.'); |
||
| 93 | } |
||
| 94 | else |
||
| 95 | { |
||
| 96 | $app->flash('message', 'Processing failed.'); |
||
| 97 | } |
||
| 98 | |||
| 99 | $app->redirect($final_global_template_vars["path_to_this_module"]."/password/"); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: