Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SMTP often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SMTP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class SMTP |
||
31 | { |
||
32 | /** |
||
33 | * The PHPMailer SMTP version number. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | const VERSION = '6.0.5'; |
||
38 | |||
39 | /** |
||
40 | * SMTP line break constant. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | const LE = "\r\n"; |
||
45 | |||
46 | /** |
||
47 | * The SMTP port to use if one is not specified. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | const DEFAULT_PORT = 25; |
||
52 | |||
53 | /** |
||
54 | * The maximum line length allowed by RFC 2822 section 2.1.1. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | const MAX_LINE_LENGTH = 998; |
||
59 | |||
60 | /** |
||
61 | * Debug level for no output. |
||
62 | */ |
||
63 | const DEBUG_OFF = 0; |
||
64 | |||
65 | /** |
||
66 | * Debug level to show client -> server messages. |
||
67 | */ |
||
68 | const DEBUG_CLIENT = 1; |
||
69 | |||
70 | /** |
||
71 | * Debug level to show client -> server and server -> client messages. |
||
72 | */ |
||
73 | const DEBUG_SERVER = 2; |
||
74 | |||
75 | /** |
||
76 | * Debug level to show connection status, client -> server and server -> client messages. |
||
77 | */ |
||
78 | const DEBUG_CONNECTION = 3; |
||
79 | |||
80 | /** |
||
81 | * Debug level to show all messages. |
||
82 | */ |
||
83 | const DEBUG_LOWLEVEL = 4; |
||
84 | |||
85 | /** |
||
86 | * Debug output level. |
||
87 | * Options: |
||
88 | * * self::DEBUG_OFF (`0`) No debug output, default |
||
89 | * * self::DEBUG_CLIENT (`1`) Client commands |
||
90 | * * self::DEBUG_SERVER (`2`) Client commands and server responses |
||
91 | * * self::DEBUG_CONNECTION (`3`) As DEBUG_SERVER plus connection status |
||
92 | * * self::DEBUG_LOWLEVEL (`4`) Low-level data output, all messages. |
||
93 | * |
||
94 | * @var int |
||
95 | */ |
||
96 | public $do_debug = self::DEBUG_OFF; |
||
97 | |||
98 | /** |
||
99 | * How to handle debug output. |
||
100 | * Options: |
||
101 | * * `echo` Output plain-text as-is, appropriate for CLI |
||
102 | * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output |
||
103 | * * `error_log` Output to error log as configured in php.ini |
||
104 | * Alternatively, you can provide a callable expecting two params: a message string and the debug level: |
||
105 | * |
||
106 | * ```php |
||
107 | * $smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; |
||
108 | * ``` |
||
109 | * |
||
110 | * Alternatively, you can pass in an instance of a PSR-3 compatible logger, though only `debug` |
||
111 | * level output is used: |
||
112 | * |
||
113 | * ```php |
||
114 | * $mail->Debugoutput = new myPsr3Logger; |
||
115 | * ``` |
||
116 | * |
||
117 | * @var string|callable|\Psr\Log\LoggerInterface |
||
118 | */ |
||
119 | public $Debugoutput = 'echo'; |
||
120 | |||
121 | /** |
||
122 | * Whether to use VERP. |
||
123 | * |
||
124 | * @see http://en.wikipedia.org/wiki/Variable_envelope_return_path |
||
125 | * @see http://www.postfix.org/VERP_README.html Info on VERP |
||
126 | * |
||
127 | * @var bool |
||
128 | */ |
||
129 | public $do_verp = false; |
||
130 | |||
131 | /** |
||
132 | * The timeout value for connection, in seconds. |
||
133 | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2. |
||
134 | * This needs to be quite high to function correctly with hosts using greetdelay as an anti-spam measure. |
||
135 | * |
||
136 | * @see http://tools.ietf.org/html/rfc2821#section-4.5.3.2 |
||
137 | * |
||
138 | * @var int |
||
139 | */ |
||
140 | public $Timeout = 300; |
||
141 | |||
142 | /** |
||
143 | * How long to wait for commands to complete, in seconds. |
||
144 | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2. |
||
145 | * |
||
146 | * @var int |
||
147 | */ |
||
148 | public $Timelimit = 300; |
||
149 | |||
150 | /** |
||
151 | * Patterns to extract an SMTP transaction id from reply to a DATA command. |
||
152 | * The first capture group in each regex will be used as the ID. |
||
153 | * MS ESMTP returns the message ID, which may not be correct for internal tracking. |
||
154 | * |
||
155 | * @var string[] |
||
156 | */ |
||
157 | protected $smtp_transaction_id_patterns = [ |
||
158 | 'exim' => '/[0-9]{3} OK id=(.*)/', |
||
159 | 'sendmail' => '/[0-9]{3} 2.0.0 (.*) Message/', |
||
160 | 'postfix' => '/[0-9]{3} 2.0.0 Ok: queued as (.*)/', |
||
161 | 'Microsoft_ESMTP' => '/[0-9]{3} 2.[0-9].0 (.*)@(?:.*) Queued mail for delivery/', |
||
162 | 'Amazon_SES' => '/[0-9]{3} Ok (.*)/', |
||
163 | 'SendGrid' => '/[0-9]{3} Ok: queued as (.*)/', |
||
164 | ]; |
||
165 | |||
166 | /** |
||
167 | * The last transaction ID issued in response to a DATA command, |
||
168 | * if one was detected. |
||
169 | * |
||
170 | * @var string|bool|null |
||
171 | */ |
||
172 | protected $last_smtp_transaction_id; |
||
173 | |||
174 | /** |
||
175 | * The socket for the server connection. |
||
176 | * |
||
177 | * @var ?resource |
||
178 | */ |
||
179 | protected $smtp_conn; |
||
180 | |||
181 | /** |
||
182 | * Error information, if any, for the last SMTP command. |
||
183 | * |
||
184 | * @var array |
||
185 | */ |
||
186 | protected $error = [ |
||
187 | 'error' => '', |
||
188 | 'detail' => '', |
||
189 | 'smtp_code' => '', |
||
190 | 'smtp_code_ex' => '', |
||
191 | ]; |
||
192 | |||
193 | /** |
||
194 | * The reply the server sent to us for HELO. |
||
195 | * If null, no HELO string has yet been received. |
||
196 | * |
||
197 | * @var string|null |
||
198 | */ |
||
199 | protected $helo_rply = null; |
||
200 | |||
201 | /** |
||
202 | * The set of SMTP extensions sent in reply to EHLO command. |
||
203 | * Indexes of the array are extension names. |
||
204 | * Value at index 'HELO' or 'EHLO' (according to command that was sent) |
||
205 | * represents the server name. In case of HELO it is the only element of the array. |
||
206 | * Other values can be boolean TRUE or an array containing extension options. |
||
207 | * If null, no HELO/EHLO string has yet been received. |
||
208 | * |
||
209 | * @var array|null |
||
210 | */ |
||
211 | protected $server_caps = null; |
||
212 | |||
213 | /** |
||
214 | * The most recent reply received from the server. |
||
215 | * |
||
216 | * @var string |
||
217 | */ |
||
218 | protected $last_reply = ''; |
||
219 | |||
220 | /** |
||
221 | * Output debugging info via a user-selected method. |
||
222 | * |
||
223 | * @param string $str Debug string to output |
||
224 | * @param int $level The debug level of this message; see DEBUG_* constants |
||
225 | * |
||
226 | * @see SMTP::$Debugoutput |
||
227 | * @see SMTP::$do_debug |
||
228 | */ |
||
229 | protected function edebug($str, $level = 0) |
||
277 | |||
278 | /** |
||
279 | * Connect to an SMTP server. |
||
280 | * |
||
281 | * @param string $host SMTP server IP or host name |
||
282 | * @param int $port The port number to connect to |
||
|
|||
283 | * @param int $timeout How long to wait for the connection to open |
||
284 | * @param array $options An array of options for stream_context_create() |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function connect($host, $port = null, $timeout = 30, $options = []) |
||
377 | |||
378 | /** |
||
379 | * Initiate a TLS (encrypted) session. |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | public function startTLS() |
||
410 | |||
411 | /** |
||
412 | * Perform SMTP authentication. |
||
413 | * Must be run after hello(). |
||
414 | * |
||
415 | * @see hello() |
||
416 | * |
||
417 | * @param string $username The user name |
||
418 | * @param string $password The password |
||
419 | * @param string $authtype The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2) |
||
420 | * @param OAuth $OAuth An optional OAuth instance for XOAUTH2 authentication |
||
421 | * |
||
422 | * @return bool True if successfully authenticated |
||
423 | */ |
||
424 | public function authenticate( |
||
544 | |||
545 | /** |
||
546 | * Calculate an MD5 HMAC hash. |
||
547 | * Works like hash_hmac('md5', $data, $key) |
||
548 | * in case that function is not available. |
||
549 | * |
||
550 | * @param string $data The data to hash |
||
551 | * @param string $key The key to hash with |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | protected function hmac($data, $key) |
||
581 | |||
582 | /** |
||
583 | * Check connection state. |
||
584 | * |
||
585 | * @return bool True if connected |
||
586 | */ |
||
587 | public function connected() |
||
607 | |||
608 | /** |
||
609 | * Close the socket and clean up the state of the class. |
||
610 | * Don't use this function without first trying to use QUIT. |
||
611 | * |
||
612 | * @see quit() |
||
613 | */ |
||
614 | public function close() |
||
626 | |||
627 | /** |
||
628 | * Send an SMTP DATA command. |
||
629 | * Issues a data command and sends the msg_data to the server, |
||
630 | * finializing the mail transaction. $msg_data is the message |
||
631 | * that is to be send with the headers. Each header needs to be |
||
632 | * on a single line followed by a <CRLF> with the message headers |
||
633 | * and the message body being separated by an additional <CRLF>. |
||
634 | * Implements RFC 821: DATA <CRLF>. |
||
635 | * |
||
636 | * @param string $msg_data Message data to send |
||
637 | * |
||
638 | * @return bool |
||
639 | */ |
||
640 | public function data($msg_data) |
||
720 | |||
721 | /** |
||
722 | * Send an SMTP HELO or EHLO command. |
||
723 | * Used to identify the sending server to the receiving server. |
||
724 | * This makes sure that client and server are in a known state. |
||
725 | * Implements RFC 821: HELO <SP> <domain> <CRLF> |
||
726 | * and RFC 2821 EHLO. |
||
727 | * |
||
728 | * @param string $host The host name or IP to connect to |
||
729 | * |
||
730 | * @return bool |
||
731 | */ |
||
732 | public function hello($host = '') |
||
737 | |||
738 | /** |
||
739 | * Send an SMTP HELO or EHLO command. |
||
740 | * Low-level implementation used by hello(). |
||
741 | * |
||
742 | * @param string $hello The HELO string |
||
743 | * @param string $host The hostname to say we are |
||
744 | * |
||
745 | * @return bool |
||
746 | * |
||
747 | * @see hello() |
||
748 | */ |
||
749 | protected function sendHello($hello, $host) |
||
761 | |||
762 | /** |
||
763 | * Parse a reply to HELO/EHLO command to discover server extensions. |
||
764 | * In case of HELO, the only parameter that can be discovered is a server name. |
||
765 | * |
||
766 | * @param string $type `HELO` or `EHLO` |
||
767 | */ |
||
768 | protected function parseHelloFields($type) |
||
803 | |||
804 | /** |
||
805 | * Send an SMTP MAIL command. |
||
806 | * Starts a mail transaction from the email address specified in |
||
807 | * $from. Returns true if successful or false otherwise. If True |
||
808 | * the mail transaction is started and then one or more recipient |
||
809 | * commands may be called followed by a data command. |
||
810 | * Implements RFC 821: MAIL <SP> FROM:<reverse-path> <CRLF>. |
||
811 | * |
||
812 | * @param string $from Source address of this message |
||
813 | * |
||
814 | * @return bool |
||
815 | */ |
||
816 | public function mail($from) |
||
826 | |||
827 | /** |
||
828 | * Send an SMTP QUIT command. |
||
829 | * Closes the socket if there is no error or the $close_on_error argument is true. |
||
830 | * Implements from RFC 821: QUIT <CRLF>. |
||
831 | * |
||
832 | * @param bool $close_on_error Should the connection close if an error occurs? |
||
833 | * |
||
834 | * @return bool |
||
835 | */ |
||
836 | public function quit($close_on_error = true) |
||
847 | |||
848 | /** |
||
849 | * Send an SMTP RCPT command. |
||
850 | * Sets the TO argument to $toaddr. |
||
851 | * Returns true if the recipient was accepted false if it was rejected. |
||
852 | * Implements from RFC 821: RCPT <SP> TO:<forward-path> <CRLF>. |
||
853 | * |
||
854 | * @param string $address The address the message is being sent to |
||
855 | * |
||
856 | * @return bool |
||
857 | */ |
||
858 | public function recipient($address) |
||
866 | |||
867 | /** |
||
868 | * Send an SMTP RSET command. |
||
869 | * Abort any transaction that is currently in progress. |
||
870 | * Implements RFC 821: RSET <CRLF>. |
||
871 | * |
||
872 | * @return bool True on success |
||
873 | */ |
||
874 | public function reset() |
||
878 | |||
879 | /** |
||
880 | * Send a command to an SMTP server and check its return code. |
||
881 | * |
||
882 | * @param string $command The command name - not sent to the server |
||
883 | * @param string $commandstring The actual command to send |
||
884 | * @param int|array $expect One or more expected integer success codes |
||
885 | * |
||
886 | * @return bool True on success |
||
887 | */ |
||
888 | protected function sendCommand($command, $commandstring, $expect) |
||
944 | |||
945 | /** |
||
946 | * Send an SMTP SAML command. |
||
947 | * Starts a mail transaction from the email address specified in $from. |
||
948 | * Returns true if successful or false otherwise. If True |
||
949 | * the mail transaction is started and then one or more recipient |
||
950 | * commands may be called followed by a data command. This command |
||
951 | * will send the message to the users terminal if they are logged |
||
952 | * in and send them an email. |
||
953 | * Implements RFC 821: SAML <SP> FROM:<reverse-path> <CRLF>. |
||
954 | * |
||
955 | * @param string $from The address the message is from |
||
956 | * |
||
957 | * @return bool |
||
958 | */ |
||
959 | public function sendAndMail($from) |
||
963 | |||
964 | /** |
||
965 | * Send an SMTP VRFY command. |
||
966 | * |
||
967 | * @param string $name The name to verify |
||
968 | * |
||
969 | * @return bool |
||
970 | */ |
||
971 | public function verify($name) |
||
975 | |||
976 | /** |
||
977 | * Send an SMTP NOOP command. |
||
978 | * Used to keep keep-alives alive, doesn't actually do anything. |
||
979 | * |
||
980 | * @return bool |
||
981 | */ |
||
982 | public function noop() |
||
986 | |||
987 | /** |
||
988 | * Send an SMTP TURN command. |
||
989 | * This is an optional command for SMTP that this class does not support. |
||
990 | * This method is here to make the RFC821 Definition complete for this class |
||
991 | * and _may_ be implemented in future. |
||
992 | * Implements from RFC 821: TURN <CRLF>. |
||
993 | * |
||
994 | * @return bool |
||
995 | */ |
||
996 | public function turn() |
||
1003 | |||
1004 | /** |
||
1005 | * Send raw data to the server. |
||
1006 | * |
||
1007 | * @param string $data The data to send |
||
1008 | * @param string $command Optionally, the command this is part of, used only for controlling debug output |
||
1009 | * |
||
1010 | * @return int|bool The number of bytes sent to the server or false on error |
||
1011 | */ |
||
1012 | public function client_send($data, $command = '') |
||
1028 | |||
1029 | /** |
||
1030 | * Get the latest error. |
||
1031 | * |
||
1032 | * @return array |
||
1033 | */ |
||
1034 | public function getError() |
||
1038 | |||
1039 | /** |
||
1040 | * Get SMTP extensions available on the server. |
||
1041 | * |
||
1042 | * @return array|null |
||
1043 | */ |
||
1044 | public function getServerExtList() |
||
1048 | |||
1049 | /** |
||
1050 | * Get metadata about the SMTP server from its HELO/EHLO response. |
||
1051 | * The method works in three ways, dependent on argument value and current state: |
||
1052 | * 1. HELO/EHLO has not been sent - returns null and populates $this->error. |
||
1053 | * 2. HELO has been sent - |
||
1054 | * $name == 'HELO': returns server name |
||
1055 | * $name == 'EHLO': returns boolean false |
||
1056 | * $name == any other string: returns null and populates $this->error |
||
1057 | * 3. EHLO has been sent - |
||
1058 | * $name == 'HELO'|'EHLO': returns the server name |
||
1059 | * $name == any other string: if extension $name exists, returns True |
||
1060 | * or its options (e.g. AUTH mechanisms supported). Otherwise returns False. |
||
1061 | * |
||
1062 | * @param string $name Name of SMTP extension or 'HELO'|'EHLO' |
||
1063 | * |
||
1064 | * @return mixed |
||
1065 | */ |
||
1066 | public function getServerExt($name) |
||
1088 | |||
1089 | /** |
||
1090 | * Get the last reply from the server. |
||
1091 | * |
||
1092 | * @return string |
||
1093 | */ |
||
1094 | public function getLastReply() |
||
1098 | |||
1099 | /** |
||
1100 | * Read the SMTP server's response. |
||
1101 | * Either before eof or socket timeout occurs on the operation. |
||
1102 | * With SMTP we can tell if we have more lines to read if the |
||
1103 | * 4th character is '-' symbol. If it is a space then we don't |
||
1104 | * need to read anything else. |
||
1105 | * |
||
1106 | * @return string |
||
1107 | */ |
||
1108 | protected function get_lines() |
||
1163 | |||
1164 | /** |
||
1165 | * Enable or disable VERP address generation. |
||
1166 | * |
||
1167 | * @param bool $enabled |
||
1168 | */ |
||
1169 | public function setVerp($enabled = false) |
||
1173 | |||
1174 | /** |
||
1175 | * Get VERP address generation mode. |
||
1176 | * |
||
1177 | * @return bool |
||
1178 | */ |
||
1179 | public function getVerp() |
||
1183 | |||
1184 | /** |
||
1185 | * Set error messages and codes. |
||
1186 | * |
||
1187 | * @param string $message The error message |
||
1188 | * @param string $detail Further detail on the error |
||
1189 | * @param string $smtp_code An associated SMTP error code |
||
1190 | * @param string $smtp_code_ex Extended SMTP code |
||
1191 | */ |
||
1192 | protected function setError($message, $detail = '', $smtp_code = '', $smtp_code_ex = '') |
||
1201 | |||
1202 | /** |
||
1203 | * Set debug output method. |
||
1204 | * |
||
1205 | * @param string|callable $method The name of the mechanism to use for debugging output, or a callable to handle it |
||
1206 | */ |
||
1207 | public function setDebugOutput($method = 'echo') |
||
1211 | |||
1212 | /** |
||
1213 | * Get debug output method. |
||
1214 | * |
||
1215 | * @return string |
||
1216 | */ |
||
1217 | public function getDebugOutput() |
||
1221 | |||
1222 | /** |
||
1223 | * Set debug output level. |
||
1224 | * |
||
1225 | * @param int $level |
||
1226 | */ |
||
1227 | public function setDebugLevel($level = 0) |
||
1231 | |||
1232 | /** |
||
1233 | * Get debug output level. |
||
1234 | * |
||
1235 | * @return int |
||
1236 | */ |
||
1237 | public function getDebugLevel() |
||
1241 | |||
1242 | /** |
||
1243 | * Set SMTP timeout. |
||
1244 | * |
||
1245 | * @param int $timeout The timeout duration in seconds |
||
1246 | */ |
||
1247 | public function setTimeout($timeout = 0) |
||
1251 | |||
1252 | /** |
||
1253 | * Get SMTP timeout. |
||
1254 | * |
||
1255 | * @return int |
||
1256 | */ |
||
1257 | public function getTimeout() |
||
1261 | |||
1262 | /** |
||
1263 | * Reports an error number and string. |
||
1264 | * |
||
1265 | * @param int $errno The error number returned by PHP |
||
1266 | * @param string $errmsg The error message returned by PHP |
||
1267 | * @param string $errfile The file the error occurred in |
||
1268 | * @param int $errline The line number the error occurred on |
||
1269 | */ |
||
1270 | protected function errorHandler($errno, $errmsg, $errfile = '', $errline = 0) |
||
1283 | |||
1284 | /** |
||
1285 | * Extract and return the ID of the last SMTP transaction based on |
||
1286 | * a list of patterns provided in SMTP::$smtp_transaction_id_patterns. |
||
1287 | * Relies on the host providing the ID in response to a DATA command. |
||
1288 | * If no reply has been received yet, it will return null. |
||
1289 | * If no pattern was matched, it will return false. |
||
1290 | * |
||
1291 | * @return bool|null|string |
||
1292 | */ |
||
1293 | protected function recordLastTransactionID() |
||
1311 | |||
1312 | /** |
||
1313 | * Get the queue/transaction ID of the last SMTP transaction |
||
1314 | * If no reply has been received yet, it will return null. |
||
1315 | * If no pattern was matched, it will return false. |
||
1316 | * |
||
1317 | * @return bool|null|string |
||
1318 | * |
||
1319 | * @see recordLastTransactionID() |
||
1320 | */ |
||
1321 | public function getLastTransactionID() |
||
1325 | } |
||
1326 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.