Complex classes like SimpleMail 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 SimpleMail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class SimpleMail |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var int $_wrap |
||
| 37 | */ |
||
| 38 | protected $_wrap = 78; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array $_to |
||
| 42 | */ |
||
| 43 | protected $_to = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string $_subject |
||
| 47 | */ |
||
| 48 | protected $_subject; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string $_message |
||
| 52 | */ |
||
| 53 | protected $_message; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array $_headers |
||
| 57 | */ |
||
| 58 | protected $_headers = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string $_parameters |
||
| 62 | */ |
||
| 63 | protected $_params; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array $_attachments |
||
| 67 | */ |
||
| 68 | protected $_attachments = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string $_uid |
||
| 72 | */ |
||
| 73 | protected $_uid; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Named constructor. |
||
| 77 | * |
||
| 78 | * @return static |
||
| 79 | */ |
||
| 80 | public static function make() |
||
| 84 | 38 | ||
| 85 | /** |
||
| 86 | * __construct |
||
| 87 | * |
||
| 88 | * Resets the class properties. |
||
| 89 | */ |
||
| 90 | public function __construct() |
||
| 94 | |||
| 95 | 38 | /** |
|
| 96 | 38 | * reset |
|
| 97 | 38 | * |
|
| 98 | 38 | * Resets all properties to initial state. |
|
| 99 | 38 | * |
|
| 100 | 38 | * @return self |
|
| 101 | 38 | */ |
|
| 102 | 38 | public function reset() |
|
| 114 | 4 | ||
| 115 | /** |
||
| 116 | 4 | * setTo |
|
| 117 | 4 | * |
|
| 118 | * @param string $email The email address to send to. |
||
| 119 | * @param string $name The name of the person to send to. |
||
| 120 | * |
||
| 121 | * @return self |
||
| 122 | */ |
||
| 123 | public function setTo($email, $name) |
||
| 128 | |||
| 129 | 2 | /** |
|
| 130 | * getTo |
||
| 131 | * |
||
| 132 | * Return an array of formatted To addresses. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function getTo() |
||
| 140 | |||
| 141 | 3 | /** |
|
| 142 | 3 | * setFrom |
|
| 143 | 3 | * |
|
| 144 | 3 | * @param string $email The email to send as from. |
|
| 145 | * @param string $name The name to send as from. |
||
| 146 | * |
||
| 147 | * @return self |
||
| 148 | */ |
||
| 149 | public function setFrom($email, $name) |
||
| 154 | 1 | ||
| 155 | /** |
||
| 156 | * setCc |
||
| 157 | * |
||
| 158 | * @param array $pairs An array of name => email pairs. |
||
| 159 | * |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | public function setCc(array $pairs) |
||
| 166 | 3 | ||
| 167 | 3 | /** |
|
| 168 | * setBcc |
||
| 169 | * |
||
| 170 | * @param array $pairs An array of name => email pairs. |
||
| 171 | * |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | public function setBcc(array $pairs) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * setReplyTo |
||
| 181 | * |
||
| 182 | * @param string $email |
||
| 183 | * @param string $name |
||
| 184 | * |
||
| 185 | * @return self |
||
| 186 | */ |
||
| 187 | public function setReplyTo($email, $name = null) |
||
| 191 | 3 | ||
| 192 | 3 | /** |
|
| 193 | 3 | * setHtml |
|
| 194 | 3 | * |
|
| 195 | 3 | * @return self |
|
| 196 | 3 | */ |
|
| 197 | 3 | public function setHtml() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * setSubject |
||
| 206 | * |
||
| 207 | * @param string $subject The email subject |
||
| 208 | 3 | * |
|
| 209 | * @return self |
||
| 210 | 3 | */ |
|
| 211 | 3 | public function setSubject($subject) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * getSubject function. |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getSubject() |
||
| 228 | 3 | ||
| 229 | /** |
||
| 230 | * setMessage |
||
| 231 | * |
||
| 232 | * @param string $message The message to send. |
||
| 233 | * |
||
| 234 | * @return self |
||
| 235 | */ |
||
| 236 | public function setMessage($message) |
||
| 241 | |||
| 242 | 3 | /** |
|
| 243 | 3 | * getMessage |
|
| 244 | 3 | * |
|
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getMessage() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * addAttachment |
||
| 254 | * |
||
| 255 | 1 | * @param string $path The file path to the attachment. |
|
| 256 | * @param string $filename The filename of the attachment when emailed. |
||
| 257 | 1 | * @param null $data |
|
| 258 | 1 | * |
|
| 259 | 1 | * @return self |
|
| 260 | */ |
||
| 261 | 1 | public function addAttachment($path, $filename = null, $data = null) |
|
| 273 | |||
| 274 | 2 | /** |
|
| 275 | * getAttachmentData |
||
| 276 | * |
||
| 277 | * @param string $path The path to the attachment file. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getAttachmentData($path) |
||
| 289 | 1 | ||
| 290 | /** |
||
| 291 | * addMailHeader |
||
| 292 | * |
||
| 293 | * @param string $header The header to add. |
||
| 294 | * @param string $email The email to add. |
||
| 295 | * @param string $name The name to add. |
||
| 296 | * |
||
| 297 | 1 | * @return self |
|
| 298 | */ |
||
| 299 | 1 | public function addMailHeader($header, $email, $name = null) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * addMailHeaders |
||
| 308 | * |
||
| 309 | 2 | * @param string $header The header to add. |
|
| 310 | * @param array $pairs An array of name => email pairs. |
||
| 311 | 2 | * |
|
| 312 | 2 | * @return self |
|
| 313 | 1 | */ |
|
| 314 | 1 | public function addMailHeaders($header, array $pairs) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * addGenericHeader |
||
| 332 | * |
||
| 333 | * @param string $header The generic header to add. |
||
| 334 | * @param mixed $value The value of the header. |
||
| 335 | * |
||
| 336 | 4 | * @return self |
|
| 337 | */ |
||
| 338 | 4 | public function addGenericHeader($header, $value) |
|
| 347 | |||
| 348 | 2 | /** |
|
| 349 | 2 | * getHeaders |
|
| 350 | 2 | * |
|
| 351 | * Return the headers registered so far as an array. |
||
| 352 | 2 | * |
|
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function getHeaders() |
||
| 359 | |||
| 360 | 1 | /** |
|
| 361 | * setAdditionalParameters |
||
| 362 | 1 | * |
|
| 363 | 1 | * Such as "[email protected] |
|
| 364 | 1 | * |
|
| 365 | 1 | * @param string $additionalParameters The addition mail parameter. |
|
| 366 | 1 | * |
|
| 367 | 1 | * @return self |
|
| 368 | 1 | */ |
|
| 369 | 1 | public function setParameters($additionalParameters) |
|
| 374 | 1 | ||
| 375 | /** |
||
| 376 | 1 | * getAdditionalParameters |
|
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public function getParameters() |
||
| 384 | |||
| 385 | /** |
||
| 386 | 1 | * setWrap |
|
| 387 | * |
||
| 388 | 1 | * @param int $wrap The number of characters at which the message will wrap. |
|
| 389 | 1 | * |
|
| 390 | * @return self |
||
| 391 | 1 | */ |
|
| 392 | 1 | public function setWrap($wrap = 78) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * getWrap |
||
| 404 | * |
||
| 405 | * @return int |
||
| 406 | */ |
||
| 407 | public function getWrap() |
||
| 411 | 3 | ||
| 412 | 3 | /** |
|
| 413 | * hasAttachments |
||
| 414 | 3 | * |
|
| 415 | 1 | * Checks if the email has any registered attachments. |
|
| 416 | * |
||
| 417 | 1 | * @return bool |
|
| 418 | */ |
||
| 419 | public function hasAttachments() |
||
| 423 | 1 | ||
| 424 | 1 | /** |
|
| 425 | * assembleAttachment |
||
| 426 | * |
||
| 427 | 2 | * @return string |
|
| 428 | */ |
||
| 429 | public function assembleAttachmentHeaders() |
||
| 437 | 1 | ||
| 438 | /** |
||
| 439 | * assembleAttachmentBody |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function assembleAttachmentBody() |
||
| 461 | 6 | ||
| 462 | /** |
||
| 463 | 6 | * getAttachmentMimeTemplate |
|
| 464 | 6 | * |
|
| 465 | 1 | * @param array $attachment An array containing 'file' and 'data' keys. |
|
| 466 | * |
||
| 467 | 5 | * @return string |
|
| 468 | 5 | */ |
|
| 469 | public function getAttachmentMimeTemplate($attachment) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * send |
||
| 488 | * |
||
| 489 | * @return boolean |
||
| 490 | * @throws \RuntimeException on no 'To: ' address to send to. |
||
| 491 | */ |
||
| 492 | public function send() |
||
| 512 | 4 | ||
| 513 | 4 | /** |
|
| 514 | * debug |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function debug() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * magic __toString function |
||
| 525 | * |
||
| 526 | 12 | * @return string |
|
| 527 | */ |
||
| 528 | public function __toString() |
||
| 532 | 12 | ||
| 533 | 12 | /** |
|
| 534 | 12 | * formatHeader |
|
| 535 | * |
||
| 536 | 12 | * Formats a display address for emails according to RFC2822 e.g. |
|
| 537 | 12 | * Name <[email protected]> |
|
| 538 | 12 | * |
|
| 539 | 12 | * @param string $email The email address. |
|
| 540 | * @param string $name The display name. |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function formatHeader($email, $name = null) |
||
| 553 | 10 | ||
| 554 | /** |
||
| 555 | * encodeUtf8 |
||
| 556 | 10 | * |
|
| 557 | 10 | * @param string $value The value to encode. |
|
| 558 | 10 | * |
|
| 559 | 10 | * @return string |
|
| 560 | 10 | */ |
|
| 561 | 10 | public function encodeUtf8($value) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * encodeUtf8Word |
||
| 572 | * |
||
| 573 | * @param string $value The word to encode. |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function encodeUtf8Word($value) |
||
| 581 | 9 | ||
| 582 | /** |
||
| 583 | 9 | * encodeUtf8Words |
|
| 584 | * |
||
| 585 | * @param string $value The words to encode. |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function encodeUtf8Words($value) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * filterEmail |
||
| 601 | * |
||
| 602 | * Removes any carriage return, line feed, tab, double quote, comma |
||
| 603 | * and angle bracket characters before sanitizing the email address. |
||
| 604 | 3 | * |
|
| 605 | * @param string $email The email to filter. |
||
| 606 | 3 | * |
|
| 607 | 1 | * @return string |
|
| 608 | */ |
||
| 609 | 2 | public function filterEmail($email) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * filterName |
||
| 627 | 1 | * |
|
| 628 | * Removes any carriage return, line feed or tab characters. Replaces |
||
| 629 | 1 | * double quotes with single quotes and angle brackets with square |
|
| 630 | * brackets, before sanitizing the string and stripping out html tags. |
||
| 631 | * |
||
| 632 | * @param string $name The name to filter. |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function filterName($name) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * filterOther |
||
| 656 | * |
||
| 657 | * Removes ASCII control characters including any carriage return, line |
||
| 658 | * feed or tab characters. |
||
| 659 | * |
||
| 660 | * @param string $data The data to filter. |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | public function filterOther($data) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * getHeadersForSend |
||
| 671 | * |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | public function getHeadersForSend() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * getToForSend |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function getToForSend() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * getUniqueId |
||
| 697 | * |
||
| 698 | * @return string |
||
| 699 | */ |
||
| 700 | public function getUniqueId() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * getWrapMessage |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public function getWrapMessage() |
||
| 714 | } |
||
| 715 |