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 |
||
10 | class TestMailer extends \Mailer |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var TestSessionEnvironment |
||
15 | */ |
||
16 | protected $testSessionEnvironment; |
||
17 | |||
18 | public function __construct() |
||
22 | |||
23 | /** |
||
24 | * Send a plain-text email. |
||
25 | * TestMailer will merely record that the email was asked to be sent, without sending anything. |
||
26 | */ |
||
27 | View Code Duplication | public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) |
|
42 | |||
43 | /** |
||
44 | * Send a multi-part HTML email |
||
45 | * TestMailer will merely record that the email was asked to be sent, without sending anything. |
||
46 | */ |
||
47 | View Code Duplication | public function sendHTML( |
|
71 | |||
72 | /** |
||
73 | * Clear the log of emails sent |
||
74 | */ |
||
75 | public function clearEmails() |
||
83 | |||
84 | /** |
||
85 | * Search for an email that was sent. |
||
86 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
87 | * |
||
88 | * @param $to |
||
89 | * @param $from |
||
90 | * @param $subject |
||
91 | * @param $content |
||
92 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
93 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
94 | */ |
||
95 | public function findEmail($to = null, $from = null, $subject = null, $content = null) |
||
103 | |||
104 | /** |
||
105 | * Search for all emails. |
||
106 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
107 | * |
||
108 | * @param $to |
||
109 | * @param $from |
||
110 | * @param $subject |
||
111 | * @param $content |
||
112 | * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', |
||
113 | * 'customHeaders', 'htmlContent', 'inlineImages' |
||
114 | */ |
||
115 | public function findEmails($to = null, $from = null, $subject = null, $content = null) |
||
147 | |||
148 | protected function saveEmail($data) |
||
157 | } |
||
158 |
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.