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 |
||
7 | class TestMailer extends Mailer |
||
8 | { |
||
9 | protected $emailsSent = array(); |
||
10 | |||
11 | /** |
||
12 | * Send a plain-text email. |
||
13 | * TestMailer will merely record that the email was asked to be sent, without sending anything. |
||
14 | * |
||
15 | * @param string $to |
||
16 | * @param string $from |
||
17 | * @param string $subject |
||
18 | * @param string $plainContent |
||
19 | * @param bool $attachedFiles |
||
20 | * @param bool $customHeaders |
||
21 | * @return bool|mixed |
||
22 | */ |
||
23 | View Code Duplication | public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) |
|
|
|||
24 | { |
||
25 | $this->saveEmail([ |
||
26 | 'Type' => 'plain', |
||
27 | 'To' => $to, |
||
28 | 'From' => $from, |
||
29 | 'Subject' => $subject, |
||
30 | |||
31 | 'Content' => $plainContent, |
||
32 | 'PlainContent' => $plainContent, |
||
33 | |||
34 | 'AttachedFiles' => $attachedFiles, |
||
35 | 'CustomHeaders' => $customHeaders, |
||
36 | ]); |
||
37 | |||
38 | return true; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Send a multi-part HTML email |
||
43 | * TestMailer will merely record that the email was asked to be sent, without sending anything. |
||
44 | * |
||
45 | * @param string $to |
||
46 | * @param string $from |
||
47 | * @param string $subject |
||
48 | * @param string $htmlContent |
||
49 | * @param bool $attachedFiles |
||
50 | * @param bool $customHeaders |
||
51 | * @param bool $plainContent |
||
52 | * @param bool $inlineImages |
||
53 | * @return bool|mixed |
||
54 | */ |
||
55 | public function sendHTML( |
||
83 | |||
84 | /** |
||
85 | * Save a single email to the log |
||
86 | * @param $data A map of information about the email |
||
87 | */ |
||
88 | protected function saveEmail($data) |
||
92 | |||
93 | /** |
||
94 | * Clear the log of emails sent |
||
95 | */ |
||
96 | public function clearEmails() |
||
100 | |||
101 | /** |
||
102 | * Search for an email that was sent. |
||
103 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
104 | * |
||
105 | * @param string $to |
||
106 | * @param string $from |
||
107 | * @param string $subject |
||
108 | * @param string $content |
||
109 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
110 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
111 | */ |
||
112 | public function findEmail($to, $from = null, $subject = null, $content = null) |
||
135 | } |
||
136 |
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.