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:
1 | <?php |
||
40 | class Mailsender extends PHPMailer |
||
41 | { |
||
42 | /** |
||
43 | * Mail attachment |
||
44 | * Array of string |
||
45 | * @var array |
||
46 | */ |
||
47 | public $mAttach = array(); |
||
48 | |||
49 | /** |
||
50 | * Mail body |
||
51 | * @var string |
||
52 | */ |
||
53 | public $mBody = ''; |
||
54 | |||
55 | /** |
||
56 | * Charset of mail |
||
57 | * @var string |
||
58 | */ |
||
59 | public $mCharset = 'utf-8'; |
||
60 | |||
61 | /** |
||
62 | * Encoding method of mail body |
||
63 | * @var string |
||
64 | */ |
||
65 | public $mEncoding = 'base64'; |
||
66 | |||
67 | /** |
||
68 | * Error count |
||
69 | * Reset when mail send success |
||
70 | * @var int |
||
71 | */ |
||
72 | public $mErrorCount = 0; |
||
73 | |||
74 | /** |
||
75 | * Error message |
||
76 | * Reset when mail send success |
||
77 | * @var string |
||
78 | */ |
||
79 | public $mErrorMsg = ''; |
||
80 | |||
81 | /** |
||
82 | * Mail from address |
||
83 | * @var string |
||
84 | */ |
||
85 | public $mFrom = ''; |
||
86 | |||
87 | /** |
||
88 | * Mail from name |
||
89 | * @var string |
||
90 | */ |
||
91 | public $mFromName = 'Aliens'; |
||
92 | |||
93 | /** |
||
94 | * Mail host |
||
95 | * Don't include port number |
||
96 | * @see $mPort |
||
97 | * @var string |
||
98 | */ |
||
99 | public $mHost = ''; |
||
100 | |||
101 | /** |
||
102 | * Html format mail ? |
||
103 | * @var boolean |
||
104 | */ |
||
105 | public $mIsHtml = false; |
||
106 | |||
107 | /** |
||
108 | * Auth type |
||
109 | * Smtp default. |
||
110 | * @var string |
||
111 | */ |
||
112 | public $mIsSmtp = true; |
||
113 | |||
114 | /** |
||
115 | * Pass to login mail host |
||
116 | * @var string |
||
117 | */ |
||
118 | public $mPass = ''; |
||
119 | |||
120 | /** |
||
121 | * Mail host port number |
||
122 | * @var int |
||
123 | */ |
||
124 | public $mPort = 25; |
||
125 | |||
126 | /** |
||
127 | * Keep smtp connection to furture useage ? |
||
128 | * Var in phpmailer is $SMTPKeepAlive default false |
||
129 | * @var boolean |
||
130 | */ |
||
131 | public $mSmtpKeepAlive = false; |
||
132 | |||
133 | /** |
||
134 | * Subject of mail |
||
135 | * @var string |
||
136 | */ |
||
137 | public $mSubject = ''; |
||
138 | |||
139 | /** |
||
140 | * Address to be mailed to |
||
141 | * Parsed data, always is an array |
||
142 | * @var array |
||
143 | */ |
||
144 | public $mTo = array(); |
||
145 | |||
146 | /** |
||
147 | * Username on mail host |
||
148 | * Some host is xxx, while some is [email protected] |
||
149 | * @var string |
||
150 | */ |
||
151 | public $mUser = ''; |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Construct |
||
156 | */ |
||
157 | public function __construct() |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Parse mail to address |
||
165 | * Input any type address, output standard array of address |
||
166 | * Parse string including email name and address to address=>name array. |
||
167 | * |
||
168 | * @var mixed $to |
||
169 | * @return array |
||
170 | */ |
||
171 | public function ParseTo($to) |
||
206 | |||
207 | |||
208 | /** |
||
209 | * Prepare - Common setup |
||
210 | */ |
||
211 | public function Prepare() |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Send mail |
||
227 | * @param string $from Only [email protected] format, no fromname |
||
228 | * @param mixed $to |
||
229 | * @param string $subject |
||
230 | * @param string $body |
||
231 | * @param mixed $attach |
||
232 | * @return boolean |
||
233 | */ |
||
234 | public function Send($from = '', $to = '', $subject = '', $body = '', $attach = '') |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Set mail attachment |
||
268 | * @param mixed $attach |
||
269 | */ |
||
270 | public function SetAttach($attach) |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Set host auth information |
||
289 | * @param string $userid |
||
290 | * @param string $passwd |
||
291 | */ |
||
292 | public function SetAuth($userid, $passwd) |
||
299 | |||
300 | |||
301 | /** |
||
302 | * Set mail body content |
||
303 | * @param string $body |
||
304 | */ |
||
305 | public function SetBody($body) |
||
310 | |||
311 | |||
312 | /** |
||
313 | * Set from & from name |
||
314 | * @param string $from |
||
315 | * @param string $fromname |
||
316 | */ |
||
317 | public function SetFrom($from, $fromname = 'Aliens') |
||
324 | |||
325 | |||
326 | /** |
||
327 | * Set host information |
||
328 | * @param string $addr |
||
329 | * @param int $port |
||
330 | * @param boolean $issmtp |
||
331 | */ |
||
332 | public function SetHost($addr, $port = 25, $issmtp = true) |
||
341 | |||
342 | |||
343 | /** |
||
344 | * Set mail subject |
||
345 | * @param string $sub |
||
346 | */ |
||
347 | public function SetSubject($sub) |
||
352 | |||
353 | |||
354 | /** |
||
355 | * Set address to mail to |
||
356 | * @param mixed $to |
||
357 | */ |
||
358 | public function SetTo($to) |
||
366 | |||
367 | } |
||
368 | ?> |
||
369 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.