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 |
||
15 | class Mail implements MailInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $bodyText; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $bodyHtml; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $title; |
||
31 | |||
32 | /** |
||
33 | * @var MailAddressInterface |
||
34 | */ |
||
35 | protected $from; |
||
36 | |||
37 | /** |
||
38 | * @var MailAddressInterface[] |
||
39 | */ |
||
40 | protected $toRecipients = array(); |
||
41 | |||
42 | /** |
||
43 | * @var MailAddressInterface[] |
||
44 | */ |
||
45 | protected $ccRecipients = array(); |
||
46 | |||
47 | /** |
||
48 | * @var MailAddressInterface[] |
||
49 | */ |
||
50 | protected $bccRecipients = array(); |
||
51 | |||
52 | /** |
||
53 | * @var MailAttachmentInterface[] |
||
54 | */ |
||
55 | protected $attachements = array(); |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $encoding = 'utf-8'; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $autocreateMissingText = true; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $css; |
||
71 | |||
72 | public function __construct(string $title, string $bodyText = null) |
||
77 | |||
78 | /** |
||
79 | * Returns the mail text body. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getBodyText() :string |
||
91 | |||
92 | /** |
||
93 | * The mail text body. |
||
94 | * |
||
95 | * @Property |
||
96 | * |
||
97 | * @param string $bodyText |
||
98 | */ |
||
99 | public function setBodyText($bodyText) :string |
||
103 | |||
104 | /** |
||
105 | * Returns the HTML text before "emogrification". |
||
106 | * This method can be overwritten by subclasses to overwrite the mail body and still applying "emogrification". |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | protected function getBodyHtmlBeforeEmogrify() :string |
||
114 | |||
115 | /** |
||
116 | * Returns the mail html body. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | View Code Duplication | public function getBodyHtml():string |
|
121 | { |
||
122 | if ($this->css) { |
||
123 | $emogrifier = new Emogrifier($this->getBodyHtmlBeforeEmogrify(), $this->css); |
||
124 | $finalHtml = $emogrifier->emogrify(); |
||
125 | } else { |
||
126 | $finalHtml = $this->getBodyHtmlBeforeEmogrify(); |
||
127 | } |
||
128 | |||
129 | return $finalHtml; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * The mail html body. |
||
134 | * |
||
135 | * @param string $bodyHtml |
||
136 | */ |
||
137 | public function setBodyHtml($bodyHtml):string |
||
141 | |||
142 | /** |
||
143 | * Returns the mail title. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getTitle():string |
||
151 | |||
152 | /** |
||
153 | * The mail title. |
||
154 | * |
||
155 | * @param string $title |
||
156 | */ |
||
157 | public function setTitle($title) :string |
||
161 | |||
162 | /** |
||
163 | * Returns the "From" email address. |
||
164 | * |
||
165 | * @return MailAddressInterface The first element is the email address, the second the name to display. |
||
166 | */ |
||
167 | public function getFrom():MailAddressInterface |
||
171 | |||
172 | /** |
||
173 | * The mail from address. |
||
174 | * |
||
175 | * @param MailAddressInterface $from |
||
176 | */ |
||
177 | public function setFrom(MailAddressInterface $from) |
||
181 | |||
182 | /** |
||
183 | * Returns an array containing the recipients. |
||
184 | * |
||
185 | * @return MailAddressInterface[] |
||
186 | */ |
||
187 | public function getToRecipients(): array |
||
191 | |||
192 | /** |
||
193 | * An array containing the recipients. |
||
194 | * |
||
195 | * @param MailAddressInterface[] $toRecipients |
||
196 | */ |
||
197 | public function setToRecipients(array $toRecipients) |
||
201 | |||
202 | /** |
||
203 | * Adss a recipient. |
||
204 | * |
||
205 | * @param MailAddressInterface $toRecipient |
||
206 | */ |
||
207 | public function addToRecipient(MailAddressInterface $toRecipient) |
||
211 | |||
212 | /** |
||
213 | * Returns an array containing the recipients in Cc. |
||
214 | * |
||
215 | * @return MailAddressInterface[] |
||
216 | */ |
||
217 | public function getCcRecipients(): array |
||
221 | |||
222 | /** |
||
223 | * An array containing the recipients. |
||
224 | * |
||
225 | * @Property |
||
226 | * |
||
227 | * @param MailAddressInterface[]$ccRecipients |
||
228 | */ |
||
229 | public function setCcRecipients(array $ccRecipients) |
||
233 | |||
234 | /** |
||
235 | * Adds a recipient. |
||
236 | * |
||
237 | * @param MailAddressInterface $ccRecipient |
||
238 | */ |
||
239 | public function addCcRecipient(MailAddressInterface $ccRecipient) |
||
243 | |||
244 | /** |
||
245 | * Returns an array containing the recipients in Bcc. |
||
246 | * |
||
247 | * @return MailAddressInterface[] |
||
248 | */ |
||
249 | public function getBccRecipients():array |
||
253 | |||
254 | /** |
||
255 | * An array containing the recipients. |
||
256 | * |
||
257 | * @param MailAddressInterface[] $bccRecipients |
||
258 | */ |
||
259 | public function setBccRecipients(array $bccRecipients) |
||
263 | |||
264 | /** |
||
265 | * Adds a recipient. |
||
266 | * |
||
267 | * @param MailAddressInterface $bccRecipient |
||
268 | */ |
||
269 | public function addBccRecipient(MailAddressInterface $bccRecipient) |
||
273 | |||
274 | /** |
||
275 | * Returns an array of attachements for that mail. |
||
276 | * |
||
277 | * @return MailAttachmentInterface[] |
||
278 | */ |
||
279 | public function getAttachements():array |
||
283 | |||
284 | /** |
||
285 | * An array containing the attachments. |
||
286 | * |
||
287 | * @param array<MailAttachmentInterface> $attachements |
||
288 | */ |
||
289 | public function setAttachements(array $attachements) |
||
293 | |||
294 | /** |
||
295 | * Adds an attachment. |
||
296 | * |
||
297 | * @param MailAttachmentInterface $attachement |
||
298 | */ |
||
299 | public function addAttachement(MailAttachmentInterface $attachement) |
||
303 | |||
304 | /** |
||
305 | * Returns the encoding of the mail. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getEncoding():string |
||
313 | |||
314 | /** |
||
315 | * The mail encoding. Defaults to utf-8. |
||
316 | * |
||
317 | * @param string $encoding |
||
318 | */ |
||
319 | public function setEncoding($encoding) |
||
323 | |||
324 | /** |
||
325 | * If no body text is set for that mail, and if autoCreateBodyText is set to true, this object will create the body text from the body HTML text, |
||
326 | * by removing any tags. |
||
327 | * |
||
328 | * @param bool $autoCreate |
||
329 | */ |
||
330 | public function autoCreateBodyText($autoCreate) |
||
334 | |||
335 | /** |
||
336 | * Removes the HTML tags from the text. |
||
337 | * |
||
338 | * @param string $s |
||
339 | * @param string $keep The list of tags to keep |
||
340 | * @param string $expand The list of tags to remove completely, along their content |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | private function removeHtml($s, $keep = '', $expand = 'script|style|noframes|select|option'):string |
||
405 | |||
406 | /** |
||
407 | * Registers some CSS to be applied to the HTML. |
||
408 | * When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS. |
||
409 | * |
||
410 | * CSS is inlined using the Emogrifier library. |
||
411 | * |
||
412 | * @param string $css The CSS to apply. |
||
413 | */ |
||
414 | public function addCssText($css) |
||
418 | |||
419 | /** |
||
420 | * Registers a CSS file to be applied to the HTML. |
||
421 | * When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS. |
||
422 | * |
||
423 | * CSS is inlined using the Emogrifier library. |
||
424 | * |
||
425 | * @param string $file The CSS file to apply. |
||
426 | */ |
||
427 | public function addCssFile($file) |
||
431 | } |
||
432 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.