| Conditions | 6 |
| Paths | 4 |
| Total Lines | 57 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 110 | protected function doSendEmail($data) |
||
| 111 | { |
||
| 112 | // Application relies on session for spam protection |
||
| 113 | if (!isset($_SESSION['contact']['lastEmailSent'])) { |
||
| 114 | return array('You have to accept the session cookie.'); |
||
| 115 | } |
||
| 116 | |||
| 117 | // See if enough time has passed to send another e-mail |
||
| 118 | if (isset($_SESSION['contact']['lastEmailSent']) |
||
| 119 | && ($_SESSION['contact']['lastEmailSent'] |
||
| 120 | + $this->emailInterval > time()) |
||
| 121 | ) { |
||
| 122 | return array(sprintf( |
||
| 123 | 'You have to wait a total of %d seconds before you can ' |
||
| 124 | . 'send another e-mail.', $this->emailInterval)); |
||
| 125 | } |
||
| 126 | |||
| 127 | require_once $this->pathToMailer . '/class.phpmailer.php'; |
||
| 128 | |||
| 129 | // Generate body |
||
| 130 | $body = sprintf("E-Mail: %s\n", $data['email']) |
||
| 131 | . sprintf("Title: %s\n", $this->titles[$data['title']]) |
||
| 132 | . sprintf("Name: %s\n", $data['name']) |
||
| 133 | . sprintf("Phone: %s\n", $data['phone']) |
||
| 134 | . sprintf("Callback: %s", ($data['callback']) ? 'yes' : 'no'); |
||
| 135 | |||
| 136 | $mail = new PHPMailer(); |
||
| 137 | |||
| 138 | // Setup mailing method |
||
| 139 | /** @todo Configuration settings should be moved to a config file */ |
||
| 140 | $mail->IsSMTP(true); |
||
| 141 | $mail->Host = 'ssl://smtp.example.org:465'; |
||
| 142 | $mail->SMTPAuth = true; |
||
| 143 | $mail->Username = ''; |
||
| 144 | $mail->Password = ''; |
||
| 145 | |||
| 146 | // Set e-mail headers to more or less fitting values |
||
| 147 | $mail->Subject = 'example.org -- Contact inquiry'; |
||
| 148 | $mail->Body = $body; |
||
| 149 | $mail->CharSet = 'UTF-8'; |
||
| 150 | |||
| 151 | $mail->AddReplyTo($data['email'], $data['name']); |
||
| 152 | $mail->SetFrom('[email protected]'); |
||
| 153 | |||
| 154 | $mail->AddAddress($this->emailRecipient); |
||
| 155 | |||
| 156 | // Try to send mail, return errors on failure |
||
| 157 | if (!$mail->Send()) { |
||
| 158 | return array('Mailer Error: ' . $mail->ErrorInfo); |
||
| 159 | } |
||
| 160 | |||
| 161 | // E-mail sent, as far as we can tell. No errors |
||
| 162 | $_SESSION['contact']['lastEmailSent'] = time(); |
||
| 163 | $_POST = array(); |
||
| 164 | |||
| 165 | return array(); |
||
| 166 | } |
||
| 167 | } |