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 |
||
32 | class Message { |
||
33 | /** @var Swift_Message */ |
||
34 | private $swiftMessage; |
||
35 | |||
36 | /** |
||
37 | * @param Swift_Message $swiftMessage |
||
38 | */ |
||
39 | public function __construct(Swift_Message $swiftMessage) { |
||
42 | |||
43 | /** |
||
44 | * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
||
45 | * FIXME: Remove this once SwiftMailer supports IDN |
||
46 | * |
||
47 | * @param array $addresses Array of mail addresses, key will get converted |
||
48 | * @return array Converted addresses if `idn_to_ascii` exists |
||
49 | */ |
||
50 | protected function convertAddresses($addresses) { |
||
79 | |||
80 | /** |
||
81 | * Set the from address of this message. |
||
82 | * |
||
83 | * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
||
84 | * |
||
85 | * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function setFrom(array $addresses) { |
||
94 | |||
95 | /** |
||
96 | * Get the from address of this message. |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function getFrom() { |
||
103 | |||
104 | /** |
||
105 | * Set the Reply-To address of this message |
||
106 | * |
||
107 | * @param array $addresses |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setReplyTo(array $addresses) { |
||
116 | |||
117 | /** |
||
118 | * Returns the Reply-To address of this message |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | public function getReplyTo() { |
||
125 | |||
126 | /** |
||
127 | * Set the to addresses of this message. |
||
128 | * |
||
129 | * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setTo(array $recipients) { |
||
138 | |||
139 | /** |
||
140 | * Get the to address of this message. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function getTo() { |
||
147 | |||
148 | /** |
||
149 | * Set the CC recipients of this message. |
||
150 | * |
||
151 | * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function setCc(array $recipients) { |
||
160 | |||
161 | /** |
||
162 | * Get the cc address of this message. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getCc() { |
||
169 | |||
170 | /** |
||
171 | * Set the BCC recipients of this message. |
||
172 | * |
||
173 | * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setBcc(array $recipients) { |
||
182 | |||
183 | /** |
||
184 | * Get the Bcc address of this message. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | public function getBcc() { |
||
191 | |||
192 | /** |
||
193 | * Set the subject of this message. |
||
194 | * |
||
195 | * @param $subject |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setSubject($subject) { |
||
202 | |||
203 | /** |
||
204 | * Get the from subject of this message. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getSubject() { |
||
211 | |||
212 | /** |
||
213 | * Set the plain-text body of this message. |
||
214 | * |
||
215 | * @param string $body |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setPlainBody($body) { |
||
222 | |||
223 | /** |
||
224 | * Get the plain body of this message. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getPlainBody() { |
||
231 | |||
232 | /** |
||
233 | * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
||
234 | * |
||
235 | * @param string $body |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function setHtmlBody($body) { |
||
242 | |||
243 | /** |
||
244 | * Get's the underlying SwiftMessage |
||
245 | * @return Swift_Message |
||
246 | */ |
||
247 | public function getSwiftMessage() { |
||
250 | |||
251 | /** |
||
252 | * @param string $body |
||
253 | * @param string $contentType |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function setBody($body, $contentType) { |
||
260 | } |
||
261 |
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.