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.1'; |
||
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 | ]; |
||
164 | |||
165 | /** |
||
166 | * The last transaction ID issued in response to a DATA command, |
||
167 | * if one was detected. |
||
168 | * |
||
169 | * @var string|bool|null |
||
170 | */ |
||
171 | protected $last_smtp_transaction_id; |
||
172 | |||
173 | /** |
||
174 | * The socket for the server connection. |
||
175 | * |
||
176 | * @var ?resource |
||
177 | */ |
||
178 | protected $smtp_conn; |
||
179 | |||
180 | /** |
||
181 | * Error information, if any, for the last SMTP command. |
||
182 | * |
||
183 | * @var array |
||
184 | */ |
||
185 | protected $error = [ |
||
186 | 'error' => '', |
||
187 | 'detail' => '', |
||
188 | 'smtp_code' => '', |
||
189 | 'smtp_code_ex' => '', |
||
190 | ]; |
||
191 | |||
192 | /** |
||
193 | * The reply the server sent to us for HELO. |
||
194 | * If null, no HELO string has yet been received. |
||
195 | * |
||
196 | * @var string|null |
||
197 | */ |
||
198 | protected $helo_rply = null; |
||
199 | |||
200 | /** |
||
201 | * The set of SMTP extensions sent in reply to EHLO command. |
||
202 | * Indexes of the array are extension names. |
||
203 | * Value at index 'HELO' or 'EHLO' (according to command that was sent) |
||
204 | * represents the server name. In case of HELO it is the only element of the array. |
||
205 | * Other values can be boolean TRUE or an array containing extension options. |
||
206 | * If null, no HELO/EHLO string has yet been received. |
||
207 | * |
||
208 | * @var array|null |
||
209 | */ |
||
210 | protected $server_caps = null; |
||
211 | |||
212 | /** |
||
213 | * The most recent reply received from the server. |
||
214 | * |
||
215 | * @var string |
||
216 | */ |
||
217 | protected $last_reply = ''; |
||
218 | |||
219 | /** |
||
220 | * Output debugging info via a user-selected method. |
||
221 | * |
||
222 | * @param string $str Debug string to output |
||
223 | * @param int $level The debug level of this message; see DEBUG_* constants |
||
224 | * |
||
225 | * @see SMTP::$Debugoutput |
||
226 | * @see SMTP::$do_debug |
||
227 | */ |
||
228 | protected function edebug($str, $level = 0) |
||
276 | |||
277 | /** |
||
278 | * Connect to an SMTP server. |
||
279 | * |
||
280 | * @param string $host SMTP server IP or host name |
||
281 | * @param int $port The port number to connect to |
||
282 | * @param int $timeout How long to wait for the connection to open |
||
283 | * @param array $options An array of options for stream_context_create() |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function connect($host, $port = null, $timeout = 30, $options = []) |
||
376 | |||
377 | /** |
||
378 | * Initiate a TLS (encrypted) session. |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function startTLS() |
||
409 | |||
410 | /** |
||
411 | * Perform SMTP authentication. |
||
412 | * Must be run after hello(). |
||
413 | * |
||
414 | * @see hello() |
||
415 | * |
||
416 | * @param string $username The user name |
||
417 | * @param string $password The password |
||
418 | * @param string $authtype The auth type (CRAM-MD5, PLAIN, LOGIN, XOAUTH2) |
||
419 | * @param OAuth $OAuth An optional OAuth instance for XOAUTH2 authentication |
||
420 | * |
||
421 | * @return bool True if successfully authenticated |
||
422 | */ |
||
423 | public function authenticate( |
||
543 | |||
544 | /** |
||
545 | * Calculate an MD5 HMAC hash. |
||
546 | * Works like hash_hmac('md5', $data, $key) |
||
547 | * in case that function is not available. |
||
548 | * |
||
549 | * @param string $data The data to hash |
||
550 | * @param string $key The key to hash with |
||
551 | * |
||
552 | * @return string |
||
553 | */ |
||
554 | protected function hmac($data, $key) |
||
580 | |||
581 | /** |
||
582 | * Check connection state. |
||
583 | * |
||
584 | * @return bool True if connected |
||
585 | */ |
||
586 | public function connected() |
||
606 | |||
607 | /** |
||
608 | * Close the socket and clean up the state of the class. |
||
609 | * Don't use this function without first trying to use QUIT. |
||
610 | * |
||
611 | * @see quit() |
||
612 | */ |
||
613 | public function close() |
||
625 | |||
626 | /** |
||
627 | * Send an SMTP DATA command. |
||
628 | * Issues a data command and sends the msg_data to the server, |
||
629 | * finializing the mail transaction. $msg_data is the message |
||
630 | * that is to be send with the headers. Each header needs to be |
||
631 | * on a single line followed by a <CRLF> with the message headers |
||
632 | * and the message body being separated by an additional <CRLF>. |
||
633 | * Implements RFC 821: DATA <CRLF>. |
||
634 | * |
||
635 | * @param string $msg_data Message data to send |
||
636 | * |
||
637 | * @return bool |
||
638 | */ |
||
639 | public function data($msg_data) |
||
719 | |||
720 | /** |
||
721 | * Send an SMTP HELO or EHLO command. |
||
722 | * Used to identify the sending server to the receiving server. |
||
723 | * This makes sure that client and server are in a known state. |
||
724 | * Implements RFC 821: HELO <SP> <domain> <CRLF> |
||
725 | * and RFC 2821 EHLO. |
||
726 | * |
||
727 | * @param string $host The host name or IP to connect to |
||
728 | * |
||
729 | * @return bool |
||
730 | */ |
||
731 | public function hello($host = '') |
||
736 | |||
737 | /** |
||
738 | * Send an SMTP HELO or EHLO command. |
||
739 | * Low-level implementation used by hello(). |
||
740 | * |
||
741 | * @param string $hello The HELO string |
||
742 | * @param string $host The hostname to say we are |
||
743 | * |
||
744 | * @return bool |
||
745 | * |
||
746 | * @see hello() |
||
747 | */ |
||
748 | protected function sendHello($hello, $host) |
||
760 | |||
761 | /** |
||
762 | * Parse a reply to HELO/EHLO command to discover server extensions. |
||
763 | * In case of HELO, the only parameter that can be discovered is a server name. |
||
764 | * |
||
765 | * @param string $type `HELO` or `EHLO` |
||
766 | */ |
||
767 | protected function parseHelloFields($type) |
||
802 | |||
803 | /** |
||
804 | * Send an SMTP MAIL command. |
||
805 | * Starts a mail transaction from the email address specified in |
||
806 | * $from. Returns true if successful or false otherwise. If True |
||
807 | * the mail transaction is started and then one or more recipient |
||
808 | * commands may be called followed by a data command. |
||
809 | * Implements RFC 821: MAIL <SP> FROM:<reverse-path> <CRLF>. |
||
810 | * |
||
811 | * @param string $from Source address of this message |
||
812 | * |
||
813 | * @return bool |
||
814 | */ |
||
815 | public function mail($from) |
||
825 | |||
826 | /** |
||
827 | * Send an SMTP QUIT command. |
||
828 | * Closes the socket if there is no error or the $close_on_error argument is true. |
||
829 | * Implements from RFC 821: QUIT <CRLF>. |
||
830 | * |
||
831 | * @param bool $close_on_error Should the connection close if an error occurs? |
||
832 | * |
||
833 | * @return bool |
||
834 | */ |
||
835 | public function quit($close_on_error = true) |
||
846 | |||
847 | /** |
||
848 | * Send an SMTP RCPT command. |
||
849 | * Sets the TO argument to $toaddr. |
||
850 | * Returns true if the recipient was accepted false if it was rejected. |
||
851 | * Implements from RFC 821: RCPT <SP> TO:<forward-path> <CRLF>. |
||
852 | * |
||
853 | * @param string $address The address the message is being sent to |
||
854 | * |
||
855 | * @return bool |
||
856 | */ |
||
857 | public function recipient($address) |
||
865 | |||
866 | /** |
||
867 | * Send an SMTP RSET command. |
||
868 | * Abort any transaction that is currently in progress. |
||
869 | * Implements RFC 821: RSET <CRLF>. |
||
870 | * |
||
871 | * @return bool True on success |
||
872 | */ |
||
873 | public function reset() |
||
877 | |||
878 | /** |
||
879 | * Send a command to an SMTP server and check its return code. |
||
880 | * |
||
881 | * @param string $command The command name - not sent to the server |
||
882 | * @param string $commandstring The actual command to send |
||
883 | * @param int|array $expect One or more expected integer success codes |
||
884 | * |
||
885 | * @return bool True on success |
||
886 | */ |
||
887 | protected function sendCommand($command, $commandstring, $expect) |
||
943 | |||
944 | /** |
||
945 | * Send an SMTP SAML command. |
||
946 | * Starts a mail transaction from the email address specified in $from. |
||
947 | * Returns true if successful or false otherwise. If True |
||
948 | * the mail transaction is started and then one or more recipient |
||
949 | * commands may be called followed by a data command. This command |
||
950 | * will send the message to the users terminal if they are logged |
||
951 | * in and send them an email. |
||
952 | * Implements RFC 821: SAML <SP> FROM:<reverse-path> <CRLF>. |
||
953 | * |
||
954 | * @param string $from The address the message is from |
||
955 | * |
||
956 | * @return bool |
||
957 | */ |
||
958 | public function sendAndMail($from) |
||
962 | |||
963 | /** |
||
964 | * Send an SMTP VRFY command. |
||
965 | * |
||
966 | * @param string $name The name to verify |
||
967 | * |
||
968 | * @return bool |
||
969 | */ |
||
970 | public function verify($name) |
||
974 | |||
975 | /** |
||
976 | * Send an SMTP NOOP command. |
||
977 | * Used to keep keep-alives alive, doesn't actually do anything. |
||
978 | * |
||
979 | * @return bool |
||
980 | */ |
||
981 | public function noop() |
||
985 | |||
986 | /** |
||
987 | * Send an SMTP TURN command. |
||
988 | * This is an optional command for SMTP that this class does not support. |
||
989 | * This method is here to make the RFC821 Definition complete for this class |
||
990 | * and _may_ be implemented in future. |
||
991 | * Implements from RFC 821: TURN <CRLF>. |
||
992 | * |
||
993 | * @return bool |
||
994 | */ |
||
995 | public function turn() |
||
1002 | |||
1003 | /** |
||
1004 | * Send raw data to the server. |
||
1005 | * |
||
1006 | * @param string $data The data to send |
||
1007 | * |
||
1008 | * @return int|bool The number of bytes sent to the server or false on error |
||
1009 | */ |
||
1010 | public function client_send($data) |
||
1019 | |||
1020 | /** |
||
1021 | * Get the latest error. |
||
1022 | * |
||
1023 | * @return array |
||
1024 | */ |
||
1025 | public function getError() |
||
1029 | |||
1030 | /** |
||
1031 | * Get SMTP extensions available on the server. |
||
1032 | * |
||
1033 | * @return array|null |
||
1034 | */ |
||
1035 | public function getServerExtList() |
||
1039 | |||
1040 | /** |
||
1041 | * Get metadata about the SMTP server from its HELO/EHLO response. |
||
1042 | * The method works in three ways, dependent on argument value and current state: |
||
1043 | * 1. HELO/EHLO has not been sent - returns null and populates $this->error. |
||
1044 | * 2. HELO has been sent - |
||
1045 | * $name == 'HELO': returns server name |
||
1046 | * $name == 'EHLO': returns boolean false |
||
1047 | * $name == any other string: returns null and populates $this->error |
||
1048 | * 3. EHLO has been sent - |
||
1049 | * $name == 'HELO'|'EHLO': returns the server name |
||
1050 | * $name == any other string: if extension $name exists, returns True |
||
1051 | * or its options (e.g. AUTH mechanisms supported). Otherwise returns False. |
||
1052 | * |
||
1053 | * @param string $name Name of SMTP extension or 'HELO'|'EHLO' |
||
1054 | * |
||
1055 | * @return mixed |
||
1056 | */ |
||
1057 | public function getServerExt($name) |
||
1079 | |||
1080 | /** |
||
1081 | * Get the last reply from the server. |
||
1082 | * |
||
1083 | * @return string |
||
1084 | */ |
||
1085 | public function getLastReply() |
||
1089 | |||
1090 | /** |
||
1091 | * Read the SMTP server's response. |
||
1092 | * Either before eof or socket timeout occurs on the operation. |
||
1093 | * With SMTP we can tell if we have more lines to read if the |
||
1094 | * 4th character is '-' symbol. If it is a space then we don't |
||
1095 | * need to read anything else. |
||
1096 | * |
||
1097 | * @return string |
||
1098 | */ |
||
1099 | protected function get_lines() |
||
1154 | |||
1155 | /** |
||
1156 | * Enable or disable VERP address generation. |
||
1157 | * |
||
1158 | * @param bool $enabled |
||
1159 | */ |
||
1160 | public function setVerp($enabled = false) |
||
1164 | |||
1165 | /** |
||
1166 | * Get VERP address generation mode. |
||
1167 | * |
||
1168 | * @return bool |
||
1169 | */ |
||
1170 | public function getVerp() |
||
1174 | |||
1175 | /** |
||
1176 | * Set error messages and codes. |
||
1177 | * |
||
1178 | * @param string $message The error message |
||
1179 | * @param string $detail Further detail on the error |
||
1180 | * @param string $smtp_code An associated SMTP error code |
||
1181 | * @param string $smtp_code_ex Extended SMTP code |
||
1182 | */ |
||
1183 | protected function setError($message, $detail = '', $smtp_code = '', $smtp_code_ex = '') |
||
1192 | |||
1193 | /** |
||
1194 | * Set debug output method. |
||
1195 | * |
||
1196 | * @param string|callable $method The name of the mechanism to use for debugging output, or a callable to handle it |
||
1197 | */ |
||
1198 | public function setDebugOutput($method = 'echo') |
||
1202 | |||
1203 | /** |
||
1204 | * Get debug output method. |
||
1205 | * |
||
1206 | * @return string |
||
1207 | */ |
||
1208 | public function getDebugOutput() |
||
1212 | |||
1213 | /** |
||
1214 | * Set debug output level. |
||
1215 | * |
||
1216 | * @param int $level |
||
1217 | */ |
||
1218 | public function setDebugLevel($level = 0) |
||
1222 | |||
1223 | /** |
||
1224 | * Get debug output level. |
||
1225 | * |
||
1226 | * @return int |
||
1227 | */ |
||
1228 | public function getDebugLevel() |
||
1232 | |||
1233 | /** |
||
1234 | * Set SMTP timeout. |
||
1235 | * |
||
1236 | * @param int $timeout The timeout duration in seconds |
||
1237 | */ |
||
1238 | public function setTimeout($timeout = 0) |
||
1242 | |||
1243 | /** |
||
1244 | * Get SMTP timeout. |
||
1245 | * |
||
1246 | * @return int |
||
1247 | */ |
||
1248 | public function getTimeout() |
||
1252 | |||
1253 | /** |
||
1254 | * Reports an error number and string. |
||
1255 | * |
||
1256 | * @param int $errno The error number returned by PHP |
||
1257 | * @param string $errmsg The error message returned by PHP |
||
1258 | * @param string $errfile The file the error occurred in |
||
1259 | * @param int $errline The line number the error occurred on |
||
1260 | */ |
||
1261 | protected function errorHandler($errno, $errmsg, $errfile = '', $errline = 0) |
||
1274 | |||
1275 | /** |
||
1276 | * Extract and return the ID of the last SMTP transaction based on |
||
1277 | * a list of patterns provided in SMTP::$smtp_transaction_id_patterns. |
||
1278 | * Relies on the host providing the ID in response to a DATA command. |
||
1279 | * If no reply has been received yet, it will return null. |
||
1280 | * If no pattern was matched, it will return false. |
||
1281 | * |
||
1282 | * @return bool|null|string |
||
1283 | */ |
||
1284 | protected function recordLastTransactionID() |
||
1302 | |||
1303 | /** |
||
1304 | * Get the queue/transaction ID of the last SMTP transaction |
||
1305 | * If no reply has been received yet, it will return null. |
||
1306 | * If no pattern was matched, it will return false. |
||
1307 | * |
||
1308 | * @return bool|null|string |
||
1309 | * |
||
1310 | * @see recordLastTransactionID() |
||
1311 | */ |
||
1312 | public function getLastTransactionID() |
||
1316 | } |
||
1317 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.