| Conditions | 12 |
| Paths | 90 |
| Total Lines | 102 |
| Code Lines | 57 |
| Lines | 24 |
| Ratio | 23.53 % |
| 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 |
||
| 88 | function smtp_send_mail ($to, $subject, $message, $headers) |
||
| 89 | { |
||
| 90 | debug_write_log(DEBUG_TRACE, '[smtp_send_mail]'); |
||
| 91 | |||
| 92 | $link = fsockopen(SMTP_SERVER_NAME, SMTP_SERVER_PORT); |
||
| 93 | |||
| 94 | if ($link === FALSE) |
||
| 95 | { |
||
| 96 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] Connection to SMTP server cannot be established.'); |
||
| 97 | return FALSE; |
||
| 98 | } |
||
| 99 | |||
| 100 | View Code Duplication | if (!smtp_read_response($link)) |
|
| 101 | { |
||
| 102 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] SMTP server replied a failure.'); |
||
| 103 | fclose($link); |
||
| 104 | return FALSE; |
||
| 105 | } |
||
| 106 | |||
| 107 | $requests = array('EHLO ' . php_uname('n')); |
||
| 108 | |||
| 109 | if (SMTP_USE_TLS) |
||
| 110 | { |
||
| 111 | array_push($requests, 'STARTTLS'); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (strlen(SMTP_USERNAME) != 0) |
||
| 115 | { |
||
| 116 | array_push($requests, 'AUTH LOGIN'); |
||
| 117 | array_push($requests, base64_encode(SMTP_USERNAME)); |
||
| 118 | array_push($requests, base64_encode(SMTP_PASSWORD)); |
||
| 119 | } |
||
| 120 | |||
| 121 | array_push($requests, 'MAIL FROM:<' . SMTP_MAILFROM . '>'); |
||
| 122 | |||
| 123 | $recipients = explode(', ', $to); |
||
| 124 | |||
| 125 | foreach ($recipients as $recipient) |
||
| 126 | { |
||
| 127 | array_push($requests, 'RCPT TO:<' . $recipient . '>'); |
||
| 128 | } |
||
| 129 | |||
| 130 | foreach ($requests as $request) |
||
| 131 | { |
||
| 132 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] ' . $request); |
||
| 133 | fwrite($link, $request . "\n"); |
||
| 134 | |||
| 135 | View Code Duplication | if (!smtp_read_response($link)) |
|
| 136 | { |
||
| 137 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] SMTP server replied a failure.'); |
||
| 138 | fclose($link); |
||
| 139 | return FALSE; |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($request == 'STARTTLS') |
||
| 143 | { |
||
| 144 | if (stream_socket_enable_crypto($link, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT)) |
||
| 145 | { |
||
| 146 | debug_write_log(DEBUG_NOTICE, '[smtp_send_mail] TLS encryption successfully initiated.'); |
||
| 147 | } |
||
| 148 | else |
||
| 149 | { |
||
| 150 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] TLS encryption failed.'); |
||
| 151 | fclose($link); |
||
| 152 | return FALSE; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] DATA'); |
||
| 158 | fwrite($link, "DATA\n"); |
||
| 159 | |||
| 160 | View Code Duplication | if (!smtp_read_response($link)) |
|
| 161 | { |
||
| 162 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] SMTP server replied a failure.'); |
||
| 163 | fclose($link); |
||
| 164 | return FALSE; |
||
| 165 | } |
||
| 166 | |||
| 167 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] ' . $headers); |
||
| 168 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] Subject: ' . $subject); |
||
| 169 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] ' . $message); |
||
| 170 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] .'); |
||
| 171 | |||
| 172 | fwrite($link, "{$headers}\r\n"); |
||
| 173 | fwrite($link, "Subject: {$subject}\r\n\r\n"); |
||
| 174 | fwrite($link, "{$message}\r\n"); |
||
| 175 | fwrite($link, ".\r\n"); |
||
| 176 | |||
| 177 | View Code Duplication | if (!smtp_read_response($link)) |
|
| 178 | { |
||
| 179 | debug_write_log(DEBUG_WARNING, '[smtp_send_mail] SMTP server replied a failure.'); |
||
| 180 | fclose($link); |
||
| 181 | return FALSE; |
||
| 182 | } |
||
| 183 | |||
| 184 | debug_write_log(DEBUG_DUMP, '[smtp_send_mail] QUIT'); |
||
| 185 | fwrite($link, "QUIT\n"); |
||
| 186 | |||
| 187 | fclose($link); |
||
| 188 | return TRUE; |
||
| 189 | } |
||
| 190 | |||
| 192 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: