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 |
||
5 | class MIMEWriter extends Writer |
||
6 | { |
||
7 | 15 | public function writeMessage(Message $message) |
|
32 | |||
33 | /** |
||
34 | * @param Message $message |
||
35 | */ |
||
36 | 15 | public function writeMessageHeaders(Message $message) |
|
69 | |||
70 | /** |
||
71 | * Write a multipart Message with Attachments |
||
72 | * |
||
73 | * @param Message $message |
||
74 | */ |
||
75 | 15 | public function writeMessageWithAttachments(Message $message) |
|
102 | |||
103 | /** |
||
104 | * Write the text and/or HTML message body parts |
||
105 | * |
||
106 | * @param Message $message |
||
107 | */ |
||
108 | 15 | public function writeMessageBody(Message $message) |
|
134 | |||
135 | /** |
||
136 | * Write the "Content-Type" header and the plain-text body in quoted-printable format |
||
137 | * |
||
138 | * @param string $content |
||
139 | */ |
||
140 | 11 | public function writeTextPart($content) |
|
148 | |||
149 | /** |
||
150 | * Write the "Content-Type" header and the plain-text body in quoted-printable format |
||
151 | * |
||
152 | * @param string $content |
||
153 | */ |
||
154 | 9 | public function writeHTMLPart($content) |
|
162 | |||
163 | /** |
||
164 | * Write the "Content-Type" header and the Attachment Content in base-64 encoded format |
||
165 | * |
||
166 | * @param Attachment $attachment |
||
167 | * @param string|null Content ID (for inline Attachments) |
||
168 | */ |
||
169 | 9 | public function writeAttachmentPart(Attachment $attachment, $content_id = null) |
|
188 | |||
189 | /** |
||
190 | * @param string $boundary |
||
191 | */ |
||
192 | 10 | public function writeMultipartBoundary($boundary) |
|
196 | |||
197 | /** |
||
198 | * @param string $boundary |
||
199 | */ |
||
200 | 10 | public function writeMultipartBoundaryEnd($boundary) |
|
204 | |||
205 | /** |
||
206 | * @param string $name |
||
207 | * @param string $value |
||
208 | */ |
||
209 | 15 | public function writeHeader($name, $value) |
|
215 | |||
216 | /** |
||
217 | * @param string $name header name |
||
218 | * @param Address[] $addresses list of Address objects |
||
219 | */ |
||
220 | 15 | public function writeAddressHeader($name, $addresses) |
|
242 | |||
243 | /** |
||
244 | * @param string $type |
||
245 | */ |
||
246 | 15 | public function writeContentTypeHeader($type) |
|
250 | |||
251 | /** |
||
252 | * @param string $boundary |
||
253 | */ |
||
254 | 7 | public function writeMixedContentTypeHeader($boundary) |
|
258 | |||
259 | /** |
||
260 | * @param string $boundary |
||
261 | */ |
||
262 | 5 | public function writeAlternativeContentTypeHeader($boundary) |
|
266 | |||
267 | /** |
||
268 | * @param string $boundary |
||
269 | */ |
||
270 | 5 | public function writeRelatedContentTypeHeader($boundary) |
|
274 | |||
275 | /** |
||
276 | * Writes the "Content-Transfer-Encoding" header with value "quoted-printable" |
||
277 | */ |
||
278 | 15 | public function writeQuotedPrintableEncodingHeader() |
|
282 | |||
283 | /** |
||
284 | * Writes the "Content-Transfer-Encoding" header with value "base64" |
||
285 | */ |
||
286 | 9 | public function writeBase64EncodingHeader() |
|
290 | |||
291 | /** |
||
292 | * @param string $encoding encoding (e.g. "quoted-printable", "base64" or "8bit") |
||
293 | */ |
||
294 | 15 | protected function writeContentEncodingHeader($encoding) |
|
298 | |||
299 | /** |
||
300 | * Generates a unique MIME boundary name |
||
301 | * |
||
302 | * @param string $prefix static prefix (helps developers diagnose the output) |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 1 | protected function createMultipartBoundaryName($prefix) |
|
307 | { |
||
308 | 1 | static $boundary_index = 1; |
|
309 | |||
310 | 1 | return "++++{$prefix}-" . sha1(microtime(true) . $boundary_index++) . "++++"; |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * Escape UTF-8 string (if necessary) for use in a header-value |
||
315 | * |
||
316 | * @param string $value |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | 15 | protected function escapeHeaderValue($value) |
|
326 | |||
327 | /** |
||
328 | * Adjusts line-breaks, correcting CR or LF as CRLF, to improve quoted-printable encoding. |
||
329 | * |
||
330 | * @param string $value |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | 15 | protected function adjustLineBreaks($value) |
|
338 | } |
||
339 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.