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 |
||
25 | class MimePart extends MessagePart |
||
26 | { |
||
27 | /** |
||
28 | * @var \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory |
||
29 | * object used for created headers |
||
30 | */ |
||
31 | protected $headerFactory; |
||
32 | |||
33 | /** |
||
34 | * @var \ZBateson\MailMimeParser\Message\PartFilterFactory factory object |
||
35 | * responsible for create PartFilters |
||
36 | */ |
||
37 | protected $partFilterFactory; |
||
38 | |||
39 | /** |
||
40 | * @var \ZBateson\MailMimeParser\Message\Part\MessagePart[] array of child |
||
41 | * parts |
||
42 | */ |
||
43 | protected $children = []; |
||
44 | |||
45 | /** |
||
46 | * @var string[][] array of headers, with keys set to lower-cased, |
||
47 | * alphanumeric characters of the header's name, and values set to an |
||
48 | * array of 2 elements, the first being the header's original name with |
||
49 | * non-alphanumeric characters and original case, and the second set to |
||
50 | * the header's value. |
||
51 | */ |
||
52 | protected $rawHeaders; |
||
53 | |||
54 | /** |
||
55 | * @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of parsed |
||
56 | * header objects populated on-demand, the key is set to the header's name |
||
57 | * lower-cased, and with non-alphanumeric characters removed. |
||
58 | */ |
||
59 | protected $headers; |
||
60 | |||
61 | /** |
||
62 | * Sets up class dependencies. |
||
63 | * |
||
64 | * @param HeaderFactory $headerFactory |
||
65 | * @param PartFilterFactory $partFilterFactory |
||
66 | * @param string $messageObjectId |
||
67 | * @param PartBuilder $partBuilder |
||
68 | * @param PartStreamFilterManager $partStreamFilterManager |
||
69 | */ |
||
70 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Returns the part at the given 0-based index, or null if none is set. |
||
95 | * |
||
96 | * Note that the first part returned is the current part itself. This is |
||
97 | * often desirable for queries with a PartFilter, e.g. looking for a |
||
98 | * MimePart with a specific Content-Type that may be satisfied by the |
||
99 | * current part. |
||
100 | * |
||
101 | * @param int $index |
||
102 | * @param PartFilter $filter |
||
103 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart |
||
104 | */ |
||
105 | View Code Duplication | public function getPart($index, PartFilter $filter = null) |
|
113 | |||
114 | /** |
||
115 | * Returns the current part, all child parts, and child parts of all |
||
116 | * children optionally filtering them with the provided PartFilter. |
||
117 | * |
||
118 | * The first part returned is always the current MimePart. This is often |
||
119 | * desirable as it may be a valid MimePart for the provided PartFilter. |
||
120 | * |
||
121 | * @param PartFilter $filter an optional filter |
||
122 | * @return \ZBateson\MailMimeParser\Message\Part\MessagePart[] |
||
123 | */ |
||
124 | public function getAllParts(PartFilter $filter = null) |
||
142 | |||
143 | /** |
||
144 | * Returns the total number of parts in this and all children. |
||
145 | * |
||
146 | * Note that the current part is considered, so the minimum getPartCount is |
||
147 | * 1 without a filter. |
||
148 | * |
||
149 | * @param PartFilter $filter |
||
150 | * @return int |
||
151 | */ |
||
152 | public function getPartCount(PartFilter $filter = null) |
||
156 | |||
157 | /** |
||
158 | * Returns the direct child at the given 0-based index, or null if none is |
||
159 | * set. |
||
160 | * |
||
161 | * @param int $index |
||
162 | * @param PartFilter $filter |
||
163 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart |
||
164 | */ |
||
165 | View Code Duplication | public function getChild($index, PartFilter $filter = null) |
|
173 | |||
174 | /** |
||
175 | * Returns all direct child parts. |
||
176 | * |
||
177 | * If a PartFilter is provided, the PartFilter is applied before returning. |
||
178 | * |
||
179 | * @param PartFilter $filter |
||
180 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart[] |
||
181 | */ |
||
182 | public function getChildParts(PartFilter $filter = null) |
||
189 | |||
190 | /** |
||
191 | * Returns the number of direct children under this part. |
||
192 | * |
||
193 | * @param PartFilter $filter |
||
194 | * @return int |
||
195 | */ |
||
196 | public function getChildCount(PartFilter $filter = null) |
||
200 | |||
201 | /** |
||
202 | * Returns the part associated with the passed mime type if it exists. |
||
203 | * |
||
204 | * @param string $mimeType |
||
205 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart or null |
||
206 | */ |
||
207 | public function getPartByMimeType($mimeType, $index = 0) |
||
212 | |||
213 | /** |
||
214 | * Returns an array of all parts associated with the passed mime type if any |
||
215 | * exist or null otherwise. |
||
216 | * |
||
217 | * @param string $mimeType |
||
218 | * @return \ZBateson\MailMimeParser\Message\Part\MimePart[] or null |
||
219 | */ |
||
220 | public function getAllPartsByMimeType($mimeType) |
||
225 | |||
226 | /** |
||
227 | * Returns the number of parts matching the passed $mimeType |
||
228 | * |
||
229 | * @param string $mimeType |
||
230 | * @return int |
||
231 | */ |
||
232 | public function getCountOfPartsByMimeType($mimeType) |
||
237 | |||
238 | /** |
||
239 | * Returns true if this part's mime type is multipart/* |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function isMultiPart() |
||
251 | |||
252 | /** |
||
253 | * Returns a filename for the part if one is defined, or null otherwise. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getFilename() |
||
261 | |||
262 | /** |
||
263 | * Returns true. |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function isMime() |
||
271 | |||
272 | /** |
||
273 | * Returns true if this part's mime type is text/plain, text/html or if the |
||
274 | * Content-Type header defines a charset. |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function isTextPart() |
||
282 | |||
283 | /** |
||
284 | * Returns the lower-cased, trimmed value of the Content-Type header. |
||
285 | * |
||
286 | * Parses the Content-Type header, defaults to returning text/plain if not |
||
287 | * defined. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getContentType($default = 'text/plain') |
||
295 | |||
296 | /** |
||
297 | * Returns the upper-cased charset of the Content-Type header's charset |
||
298 | * parameter if set, US-ASCII if the Content-Type is text/plain or text/html |
||
299 | * and the charset parameter isn't set, or null otherwise. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getCharset() |
||
315 | |||
316 | /** |
||
317 | * Returns the content's disposition, defaulting to 'inline' if not set. |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getContentDisposition($default = 'inline') |
||
325 | |||
326 | /** |
||
327 | * Returns the content-transfer-encoding used for this part, defaulting to |
||
328 | * '7bit' if not set. |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getContentTransferEncoding($default = '7bit') |
||
336 | |||
337 | /** |
||
338 | * Returns the AbstractHeader object for the header with the given $name |
||
339 | * |
||
340 | * Note that mime headers aren't case sensitive. |
||
341 | * |
||
342 | * @param string $name |
||
343 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader |
||
344 | */ |
||
345 | public function getHeader($name) |
||
359 | |||
360 | /** |
||
361 | * Returns the string value for the header with the given $name. |
||
362 | * |
||
363 | * Note that mime headers aren't case sensitive. |
||
364 | * |
||
365 | * @param string $name |
||
366 | * @param string $defaultValue |
||
367 | * @return string |
||
368 | */ |
||
369 | public function getHeaderValue($name, $defaultValue = null) |
||
377 | |||
378 | /** |
||
379 | * Returns a parameter of the header $header, given the parameter named |
||
380 | * $param. |
||
381 | * |
||
382 | * Only headers of type |
||
383 | * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
||
384 | * Content-Type and Content-Disposition are examples of headers with |
||
385 | * parameters. "Charset" is a common parameter of Content-Type. |
||
386 | * |
||
387 | * @param string $header |
||
388 | * @param string $param |
||
389 | * @param string $defaultValue |
||
390 | * @return string |
||
391 | */ |
||
392 | public function getHeaderParameter($header, $param, $defaultValue = null) |
||
400 | } |
||
401 |
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.