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 Mail 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 Mail, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Mail |
||
12 | { |
||
13 | protected $to = [], |
||
14 | $from = [], |
||
15 | $cc = [], |
||
16 | $bcc = [], |
||
17 | $subject, |
||
18 | $body_plain, |
||
19 | $body_html, |
||
20 | $attachments = [], |
||
21 | $images = [], |
||
22 | $headers = ['X-Mailer' => 'osCommerce'], |
||
23 | $body, |
||
24 | $content_transfer_encoding = '7bit', |
||
25 | $charset = 'utf-8'; |
||
26 | |||
27 | public function __construct($to_email_address = null, $to = null, $from_email_address = null, $from = null, $subject = null) |
||
41 | |||
42 | public function addTo($email_address, $name = null) |
||
49 | |||
50 | public function setFrom($email_address, $name = null) |
||
57 | |||
58 | public function addCC($email_address, $name = null) |
||
65 | |||
66 | public function addBCC($email_address, $name = null) |
||
73 | |||
74 | public function clearTo() |
||
75 | { |
||
76 | $this->to = []; |
||
77 | $this->cc = []; |
||
78 | $this->bcc = []; |
||
79 | |||
80 | if (isset($this->headers['Cc'])) { |
||
81 | unset($this->headers['Cc']); |
||
82 | } |
||
83 | |||
84 | if (isset($this->headers['Bcc'])) { |
||
85 | unset($this->headers['Bcc']); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public function setSubject($subject) |
||
93 | |||
94 | public function setBody($text, $html = null) |
||
95 | { |
||
96 | $this->setBodyPlain($text); |
||
97 | |||
98 | if (!isset($html) || empty($html)) { |
||
99 | $html = nl2br($text); |
||
100 | } |
||
101 | |||
102 | $this->setBodyHTML($html); |
||
103 | } |
||
104 | |||
105 | public function setBodyPlain($body) |
||
110 | |||
111 | public function setBodyHTML($body) |
||
116 | |||
117 | public function setContentTransferEncoding($encoding) |
||
121 | |||
122 | public function setCharset($charset) |
||
126 | |||
127 | public function addHeader($key, $value) |
||
139 | |||
140 | public function addAttachment($file, $is_uploaded = false) |
||
157 | |||
158 | public function addImage($file, $is_uploaded = false) |
||
176 | |||
177 | public function send() |
||
178 | { |
||
179 | if (empty($this->body)) { |
||
180 | if (!empty($this->body_plain) && !empty($this->body_html)) { |
||
181 | $boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
||
182 | $related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
||
183 | $alternative_boundary = '=_____MULTIPART_ALTERNATIVE_BOUNDARY____'; |
||
184 | |||
185 | $this->headers['MIME-Version'] = '1.0'; |
||
186 | $this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $boundary . '"'; |
||
187 | $this->headers['Content-Transfer-Encoding'] = $this->content_transfer_encoding; |
||
188 | |||
189 | if (!empty($this->images)) { |
||
190 | View Code Duplication | foreach ($this->images as $image) { |
|
191 | $this->body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->body_html); |
||
192 | } |
||
193 | |||
194 | unset($image); |
||
195 | } |
||
196 | |||
197 | $this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
||
198 | '--' . $boundary . "\n" . |
||
199 | 'Content-Type: multipart/alternative; boundary="' . $alternative_boundary . '";' . "\n\n" . |
||
200 | '--' . $alternative_boundary . "\n" . |
||
201 | 'Content-Type: text/plain; charset="' . $this->charset . '"' . "\n" . |
||
202 | 'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
||
203 | $this->body_plain . "\n\n" . |
||
204 | '--' . $alternative_boundary . "\n" . |
||
205 | 'Content-Type: multipart/related; boundary="' . $related_boundary . '"' . "\n\n" . |
||
206 | '--' . $related_boundary . "\n" . |
||
207 | 'Content-Type: text/html; charset="' . $this->charset . '"' . "\n" . |
||
208 | 'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
||
209 | $this->body_html . "\n\n"; |
||
210 | |||
211 | if (!empty($this->images)) { |
||
212 | foreach ($this->images as $image) { |
||
213 | $this->body .= $this->build_image($image, $related_boundary); |
||
214 | } |
||
215 | |||
216 | unset($image); |
||
217 | } |
||
218 | |||
219 | $this->body .= '--' . $related_boundary . '--' . "\n\n" . |
||
220 | '--' . $alternative_boundary . '--' . "\n\n"; |
||
221 | |||
222 | if (!empty($this->attachments)) { |
||
223 | foreach ($this->attachments as $attachment) { |
||
224 | $this->body .= $this->build_attachment($attachment, $boundary); |
||
225 | } |
||
226 | |||
227 | unset($attachment); |
||
228 | } |
||
229 | |||
230 | $this->body .= '--' . $boundary . '--' . "\n\n"; |
||
231 | } elseif (!empty($this->body_html) && !empty($this->images)) { |
||
232 | $boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
||
233 | $related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
||
234 | |||
235 | $this->headers['MIME-Version'] = '1.0'; |
||
236 | $this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $boundary . '"'; |
||
237 | |||
238 | View Code Duplication | foreach ($this->images as $image) { |
|
239 | $this->body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->body_html); |
||
240 | } |
||
241 | |||
242 | unset($image); |
||
243 | |||
244 | $this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
||
245 | '--' . $boundary . "\n" . |
||
246 | 'Content-Type: multipart/related; boundary="' . $related_boundary . '";' . "\n\n" . |
||
247 | '--' . $related_boundary . "\n" . |
||
248 | 'Content-Type: text/html; charset="' . $this->charset . '"' . "\n" . |
||
249 | 'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
||
250 | $this->body_html . "\n\n"; |
||
251 | |||
252 | foreach ($this->images as $image) { |
||
253 | $this->body .= $this->build_image($image, $related_boundary); |
||
254 | } |
||
255 | |||
256 | unset($image); |
||
257 | |||
258 | $this->body .= '--' . $related_boundary . '--' . "\n\n"; |
||
259 | |||
260 | foreach ($this->attachments as $attachment) { |
||
261 | $this->body .= $this->build_attachment($attachment, $boundary); |
||
262 | } |
||
263 | |||
264 | unset($attachment); |
||
265 | |||
266 | $this->body .= '--' . $boundary . '--' . "\n"; |
||
267 | } elseif (!empty($this->attachments)) { |
||
268 | $boundary = '=_____MULTIPART_MIXED_BOUNDARY____'; |
||
269 | $related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____'; |
||
270 | |||
271 | $this->headers['MIME-Version'] = '1.0'; |
||
272 | $this->headers['Content-Type'] = 'multipart/mixed; boundary="' . $boundary . '"'; |
||
273 | |||
274 | $this->body = 'This is a multi-part message in MIME format.' . "\n\n" . |
||
275 | '--' . $boundary . "\n" . |
||
276 | 'Content-Type: multipart/related; boundary="' . $related_boundary . '";' . "\n\n" . |
||
277 | '--' . $related_boundary . "\n" . |
||
278 | 'Content-Type: text/' . (empty($this->body_plain) ? 'html' : 'plain') . '; charset="' . $this->charset . '"' . "\n" . |
||
279 | 'Content-Transfer-Encoding: ' . $this->content_transfer_encoding . "\n\n" . |
||
280 | (empty($this->body_plain) ? $this->body_html : $this->body_plain) . "\n\n" . |
||
281 | '--' . $related_boundary . '--' . "\n\n"; |
||
282 | |||
283 | foreach ($this->attachments as $attachment) { |
||
284 | $this->body .= $this->build_attachment($attachment, $boundary); |
||
285 | } |
||
286 | |||
287 | unset($attachment); |
||
288 | |||
289 | $this->body .= '--' . $boundary . '--' . "\n"; |
||
290 | } elseif (!empty($this->body_html)) { |
||
291 | $this->headers['MIME-Version'] = '1.0'; |
||
292 | $this->headers['Content-Type'] = 'text/html; charset="' . $this->charset . '"'; |
||
293 | $this->headers['Content-Transfer-Encoding'] = $this->content_transfer_encoding; |
||
294 | |||
295 | $this->body = $this->body_html . "\n"; |
||
296 | } else { |
||
297 | $this->body = $this->body_plain . "\n"; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | $to_email_addresses = []; |
||
302 | |||
303 | foreach ($this->to as $to) { |
||
304 | View Code Duplication | if ((strpos($to['email_address'], "\n") !== false) || (strpos($to['email_address'], "\r") !== false)) { |
|
305 | return false; |
||
306 | } |
||
307 | |||
308 | View Code Duplication | if ((strpos($to['name'], "\n") !== false) || (strpos($to['name'], "\r") !== false)) { |
|
309 | return false; |
||
310 | } |
||
311 | |||
312 | if (empty($to['name'])) { |
||
313 | $to_email_addresses[] = $to['email_address']; |
||
314 | } else { |
||
315 | $to_email_addresses[] = '"' . $to['name'] . '" <' . $to['email_address'] . '>'; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | unset($to); |
||
320 | |||
321 | $cc_email_addresses = []; |
||
322 | |||
323 | View Code Duplication | foreach ($this->cc as $cc) { |
|
324 | if (empty($cc['name'])) { |
||
325 | $cc_email_addresses[] = $cc['email_address']; |
||
326 | } else { |
||
327 | $cc_email_addresses[] = '"' . $cc['name'] . '" <' . $cc['email_address'] . '>'; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | unset($cc); |
||
332 | |||
333 | $bcc_email_addresses = []; |
||
334 | |||
335 | View Code Duplication | foreach ($this->bcc as $bcc) { |
|
336 | if (empty($bcc['name'])) { |
||
337 | $bcc_email_addresses[] = $bcc['email_address']; |
||
338 | } else { |
||
339 | $bcc_email_addresses[] = '"' . $bcc['name'] . '" <' . $bcc['email_address'] . '>'; |
||
340 | } |
||
341 | } |
||
342 | |||
343 | unset($bcc); |
||
344 | |||
345 | if (empty($this->from['name'])) { |
||
346 | $this->addHeader('From', $this->from['email_address']); |
||
347 | } else { |
||
348 | $this->addHeader('From', '"' . $this->from['name'] . '" <' . $this->from['email_address'] . '>'); |
||
349 | } |
||
350 | |||
351 | if (!empty($cc_email_addresses)) { |
||
352 | $this->addHeader('Cc', implode(', ', $cc_email_addresses)); |
||
353 | } |
||
354 | |||
355 | if (!empty($bcc_email_addresses)) { |
||
356 | $this->addHeader('Bcc', implode(', ', $bcc_email_addresses)); |
||
357 | } |
||
358 | |||
359 | $headers = ''; |
||
360 | |||
361 | foreach ($this->headers as $key => $value) { |
||
362 | $headers .= $key . ': ' . $value . "\n"; |
||
363 | } |
||
364 | |||
365 | if (empty($this->from['email_address']) || empty($to_email_addresses)) { |
||
366 | return false; |
||
367 | } |
||
368 | |||
369 | if (empty($this->from['name'])) { |
||
370 | ini_set('sendmail_from', $this->from['email_address']); |
||
371 | } else { |
||
372 | ini_set('sendmail_from', '"' . $this->from['name'] . '" <' . $this->from['email_address'] . '>'); |
||
373 | } |
||
374 | |||
375 | mail(implode(', ', $to_email_addresses), $this->subject, $this->body, $headers, '-f' . $this->from['email_address']); |
||
376 | |||
377 | ini_restore('sendmail_from'); |
||
378 | } |
||
379 | |||
380 | protected function get_mime_type($file) |
||
402 | |||
403 | protected function build_attachment($attachment, $boundary) |
||
411 | |||
412 | protected function build_image($image, $boundary) |
||
421 | } |
||
422 |
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.