Complex classes like MimePart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MimePart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class MimePart |
||
25 | { |
||
26 | /** |
||
27 | * @var \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory |
||
28 | * object used for created headers |
||
29 | */ |
||
30 | protected $headerFactory; |
||
31 | |||
32 | /** |
||
33 | * @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of header |
||
34 | * objects |
||
35 | */ |
||
36 | protected $headers; |
||
37 | |||
38 | /** |
||
39 | * @var \ZBateson\MailMimeParser\MimePart parent part |
||
40 | */ |
||
41 | protected $parent; |
||
42 | |||
43 | /** |
||
44 | * @var resource the content's resource handle |
||
45 | */ |
||
46 | protected $handle; |
||
47 | |||
48 | /** |
||
49 | * @var \ZBateson\MailMimeParser\MimePart[] array of parts in this message |
||
50 | */ |
||
51 | protected $parts = []; |
||
52 | |||
53 | /** |
||
54 | * @var \ZBateson\MailMimeParser\MimePart[] Maps mime types to parts for |
||
55 | * looking up in getPartByMimeType |
||
56 | */ |
||
57 | protected $mimeToPart = []; |
||
58 | |||
59 | /** |
||
60 | * Sets up class dependencies. |
||
61 | * |
||
62 | * @param HeaderFactory $headerFactory |
||
63 | */ |
||
64 | 82 | public function __construct(HeaderFactory $headerFactory) |
|
68 | |||
69 | /** |
||
70 | * Closes the attached resource handle. |
||
71 | */ |
||
72 | 82 | public function __destruct() |
|
78 | |||
79 | /** |
||
80 | * Adds the passed part to the parts array, and registers non-attachment/ |
||
81 | * non-multipart parts by their content type. |
||
82 | * |
||
83 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
84 | */ |
||
85 | 74 | public function addPart(MimePart $part) |
|
93 | |||
94 | /** |
||
95 | * Unregisters the child part from this part. |
||
96 | * |
||
97 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
98 | */ |
||
99 | 7 | public function removePart(MimePart $part) |
|
111 | |||
112 | /** |
||
113 | * Returns the non-text, non-HTML part at the given 0-based index, or null |
||
114 | * if none is set. |
||
115 | * |
||
116 | * @param int $index |
||
117 | * @return \ZBateson\MailMimeParser\MimePart |
||
118 | */ |
||
119 | 2 | public function getPart($index) |
|
126 | |||
127 | /** |
||
128 | * Returns all attachment parts. |
||
129 | * |
||
130 | * @return \ZBateson\MailMimeParser\MimePart[] |
||
131 | */ |
||
132 | 22 | public function getAllParts() |
|
136 | |||
137 | /** |
||
138 | * Returns the number of attachments available. |
||
139 | * |
||
140 | * @return int |
||
141 | */ |
||
142 | 1 | public function getPartCount() |
|
146 | |||
147 | /** |
||
148 | * Returns the part associated with the passed mime type if it exists. |
||
149 | * |
||
150 | * @param string $mimeType |
||
151 | * @return \ZBateson\MailMimeParser\MimePart or null |
||
152 | */ |
||
153 | 18 | public function getPartByMimeType($mimeType) |
|
161 | |||
162 | /** |
||
163 | * Returns true if there's a content stream associated with the part. |
||
164 | * |
||
165 | * @return boolean |
||
166 | */ |
||
167 | 72 | public function hasContent() |
|
174 | |||
175 | /** |
||
176 | * Returns true if this part's mime type is multipart/* |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 74 | public function isMultiPart() |
|
187 | |||
188 | /** |
||
189 | * Returns true if this part's mime type is text/* |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | 71 | public function isTextPart() |
|
200 | |||
201 | /** |
||
202 | * Attaches the resource handle for the part's content. The attached handle |
||
203 | * is closed when the MimePart object is destroyed. |
||
204 | * |
||
205 | * @param resource $contentHandle |
||
206 | */ |
||
207 | 76 | public function attachContentResourceHandle($contentHandle) |
|
214 | |||
215 | /** |
||
216 | * |
||
217 | */ |
||
218 | 8 | protected function detachContentResourceHandle() |
|
222 | |||
223 | /** |
||
224 | * Sets the content of the part to the passed string (effectively creates |
||
225 | * a php://temp stream with the passed content and calls |
||
226 | * attachContentResourceHandle with the opened stream). |
||
227 | * |
||
228 | * @param string $string |
||
229 | */ |
||
230 | 6 | public function setContent($string) |
|
237 | |||
238 | /** |
||
239 | * Returns the resource stream handle for the part's content. |
||
240 | * |
||
241 | * The resource is automatically closed by MimePart's destructor and should |
||
242 | * not be closed otherwise. |
||
243 | * |
||
244 | * @return resource |
||
245 | */ |
||
246 | 72 | public function getContentResourceHandle() |
|
250 | |||
251 | /** |
||
252 | * Shortcut to reading stream content and assigning it to a string. Returns |
||
253 | * null if the part doesn't have a content stream. |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 2 | public function getContent() |
|
264 | |||
265 | /** |
||
266 | * Adds a header with the given $name and $value. |
||
267 | * |
||
268 | * Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
||
269 | * registers it as a header. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * @param string $value |
||
273 | */ |
||
274 | 77 | public function setRawHeader($name, $value) |
|
278 | |||
279 | /** |
||
280 | * Removes the header with the given name |
||
281 | * |
||
282 | * @param string $name |
||
283 | */ |
||
284 | 5 | public function removeHeader($name) |
|
288 | |||
289 | /** |
||
290 | * Returns the AbstractHeader object for the header with the given $name |
||
291 | * |
||
292 | * Note that mime headers aren't case sensitive. |
||
293 | * |
||
294 | * @param string $name |
||
295 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader |
||
296 | */ |
||
297 | 79 | public function getHeader($name) |
|
304 | |||
305 | /** |
||
306 | * Returns the string value for the header with the given $name. |
||
307 | * |
||
308 | * Note that mime headers aren't case sensitive. |
||
309 | * |
||
310 | * @param string $name |
||
311 | * @param string $defaultValue |
||
312 | * @return string |
||
313 | */ |
||
314 | 76 | public function getHeaderValue($name, $defaultValue = null) |
|
322 | |||
323 | /** |
||
324 | * Returns the full array of headers for this part. |
||
325 | * |
||
326 | * @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
||
327 | */ |
||
328 | 1 | public function getHeaders() |
|
332 | |||
333 | /** |
||
334 | * Returns a parameter of the header $header, given the parameter named |
||
335 | * $param. |
||
336 | * |
||
337 | * Only headers of type |
||
338 | * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
||
339 | * Content-Type and Content-Disposition are examples of headers with |
||
340 | * parameters. "Charset" is a common parameter of Content-Type. |
||
341 | * |
||
342 | * @param string $header |
||
343 | * @param string $param |
||
344 | * @param string $defaultValue |
||
345 | * @return string |
||
346 | */ |
||
347 | 76 | public function getHeaderParameter($header, $param, $defaultValue = null) |
|
355 | |||
356 | /** |
||
357 | * Sets the parent part. |
||
358 | * |
||
359 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
360 | */ |
||
361 | 73 | public function setParent(MimePart $part) |
|
365 | |||
366 | /** |
||
367 | * Returns this part's parent. |
||
368 | * |
||
369 | * @return \ZBateson\MailMimeParser\MimePart |
||
370 | */ |
||
371 | 73 | public function getParent() |
|
375 | |||
376 | /** |
||
377 | * Sets up a mailmimeparser-encode stream filter on the passed stream |
||
378 | * resource handle if applicable and returns a reference to the filter. |
||
379 | * |
||
380 | * @param resource $handle |
||
381 | * @return resource a reference to the appended stream filter or null |
||
382 | */ |
||
383 | 71 | private function setCharsetStreamFilterOnStream($handle) |
|
399 | |||
400 | /** |
||
401 | * Appends a stream filter the passed resource handle based on the type of |
||
402 | * encoding for the current mime part. |
||
403 | * |
||
404 | * Unfortunately PHP seems to error out allocating memory for |
||
405 | * stream_filter_make_writable in Base64EncodeStreamFilter using |
||
406 | * STREAM_FILTER_WRITE, and HHVM doesn't seem to remove the filter properly |
||
407 | * for STREAM_FILTER_READ, so the function appends a read filter on |
||
408 | * $fromHandle if running through 'php', and a write filter on $toHandle if |
||
409 | * using HHVM. |
||
410 | * |
||
411 | * @param resource $fromHandle |
||
412 | * @param resource $toHandle |
||
413 | * @param \ZBateson\MailMimeParser\Stream\StreamLeftover $leftovers |
||
414 | * @return resource the stream filter |
||
415 | */ |
||
416 | 71 | private function setTransferEncodingFilterOnStream($fromHandle, $toHandle, StreamLeftover $leftovers) |
|
453 | |||
454 | /** |
||
455 | * Returns true if the content-transfer-encoding header of the current part |
||
456 | * is set to 'x-uuencode'. |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | private function isUUEncoded() |
||
465 | |||
466 | /** |
||
467 | * Filters out single line feed (CR or LF) characters from text input and |
||
468 | * replaces them with CRLF, assigning the result to $read. Also trims out |
||
469 | * any starting and ending CRLF characters in the stream. |
||
470 | * |
||
471 | * @param string $read the read string, and where the result will be written |
||
472 | * to |
||
473 | * @param bool $first set to true if this is the first set of read |
||
474 | * characters from the stream (ltrims CRLF) |
||
475 | * @param string $lastChars contains any CRLF characters from the last $read |
||
476 | * line if it ended with a CRLF (because they're trimmed from the |
||
477 | * end, and get prepended to $read). |
||
478 | */ |
||
479 | 69 | private function filterTextBeforeCopying(&$read, &$first, &$lastChars) |
|
494 | |||
495 | /** |
||
496 | * Copies the content of the $fromHandle stream into the $toHandle stream, |
||
497 | * maintaining the current read position in $fromHandle and writing |
||
498 | * uuencode headers. |
||
499 | * |
||
500 | * @param resource $fromHandle |
||
501 | * @param resource $toHandle |
||
502 | */ |
||
503 | 71 | private function copyContentStream($fromHandle, $toHandle) |
|
519 | |||
520 | /** |
||
521 | * Writes out headers and follows them with an empty line. |
||
522 | * |
||
523 | * @param resource $handle |
||
524 | */ |
||
525 | 71 | protected function writeHeadersTo($handle) |
|
532 | |||
533 | /** |
||
534 | * Writes out the content portion of the mime part based on the headers that |
||
535 | * are set on the part, taking care of character/content-transfer encoding. |
||
536 | * |
||
537 | * @param resource $handle |
||
538 | */ |
||
539 | 71 | protected function writeContentTo($handle) |
|
556 | |||
557 | /** |
||
558 | * Writes out the MimePart to the passed resource. |
||
559 | * |
||
560 | * Takes care of character and content transfer encoding on the output based |
||
561 | * on what headers are set. |
||
562 | * |
||
563 | * @param resource $handle |
||
564 | */ |
||
565 | 48 | protected function writeTo($handle) |
|
570 | } |
||
571 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.