Complex classes like Email 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 Email, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Email extends ViewableData |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | * @config |
||
| 24 | */ |
||
| 25 | private static $send_all_emails_to = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | * @config |
||
| 30 | */ |
||
| 31 | private static $cc_all_emails_to = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | * @config |
||
| 36 | */ |
||
| 37 | private static $bcc_all_emails_to = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | * @config |
||
| 42 | */ |
||
| 43 | private static $send_all_emails_from = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * This will be set in the config on a site-by-site basis |
||
| 47 | * |
||
| 48 | * @config |
||
| 49 | * @var string The default administrator email address. |
||
| 50 | */ |
||
| 51 | private static $admin_email = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Swift_Message |
||
| 55 | */ |
||
| 56 | private $swiftMessage; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string The name of the HTML template to render the email with (without *.ss extension) |
||
| 60 | */ |
||
| 61 | private $HTMLTemplate = self::class; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string The name of the plain text template to render the plain part of the email with |
||
| 65 | */ |
||
| 66 | private $plainTemplate = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Swift_MimePart |
||
| 70 | */ |
||
| 71 | private $plainPart; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array|ViewableData Additional data available in a template. |
||
| 75 | * Used in the same way than {@link ViewableData->customize()}. |
||
| 76 | */ |
||
| 77 | private $data = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $failedRecipients = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Checks for RFC822-valid email format. |
||
| 86 | * |
||
| 87 | * @param string $address |
||
| 88 | * @return boolean |
||
| 89 | * |
||
| 90 | * @copyright Cal Henderson <[email protected]> |
||
| 91 | * This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License |
||
| 92 | * http://creativecommons.org/licenses/by-sa/2.5/ |
||
| 93 | */ |
||
| 94 | public static function is_valid_address($address) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Encode an email-address to protect it from spambots. |
||
| 101 | * At the moment only simple string substitutions, |
||
| 102 | * which are not 100% safe from email harvesting. |
||
| 103 | * |
||
| 104 | * @param string $email Email-address |
||
| 105 | * @param string $method Method for obfuscating/encoding the address |
||
| 106 | * - 'direction': Reverse the text and then use CSS to put the text direction back to normal |
||
| 107 | * - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash]) |
||
| 108 | * - 'hex': Hexadecimal URL-Encoding - useful for mailto: links |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public static function obfuscate($email, $method = 'visible') |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Email constructor. |
||
| 138 | * @param string|array|null $from |
||
| 139 | * @param string|array|null $to |
||
| 140 | * @param string|null $subject |
||
| 141 | * @param string|null $body |
||
| 142 | * @param string|array|null $cc |
||
| 143 | * @param string|array|null $bcc |
||
| 144 | * @param string|null $returnPath |
||
| 145 | */ |
||
| 146 | public function __construct( |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return Swift_Message |
||
| 182 | */ |
||
| 183 | public function getSwiftMessage() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param Swift_Message $swiftMessage |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function setSwiftMessage($swiftMessage) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string[] |
||
| 210 | */ |
||
| 211 | public function getFrom() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string|array $address |
||
| 218 | * @param string|null $name |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function setFrom($address, $name = null) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string|array $address |
||
| 230 | * @param string|null $name |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function addFrom($address, $name = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function getSender() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $address |
||
| 250 | * @param string|null $name |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function setSender($address, $name = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getReturnPath() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * The bounce handler address |
||
| 270 | * |
||
| 271 | * @param string $address Email address where bounce notifications should be sent |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function setReturnPath($address) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function getTo() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set recipient(s) of the email |
||
| 290 | * |
||
| 291 | * To send to many, pass an array: |
||
| 292 | * array('[email protected]' => 'My Name', '[email protected]'); |
||
| 293 | * |
||
| 294 | * @param string|array $address The message recipient(s) - if sending to multiple, use an array of address => name |
||
| 295 | * @param string|null $name The name of the recipient (if one) |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setTo($address, $name = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string|array $address |
||
| 307 | * @param string|null $name |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function addTo($address, $name = null) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getCC() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string|array $address |
||
| 327 | * @param string|null $name |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function setCC($address, $name = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string|array $address |
||
| 339 | * @param string|null $name |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function addCC($address, $name = null) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getBCC() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string|array $address |
||
| 359 | * @param string|null $name |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | public function setBCC($address, $name = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param string|array $address |
||
| 371 | * @param string|null $name |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | public function addBCC($address, $name = null) |
||
| 380 | |||
| 381 | public function getReplyTo() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string|array $address |
||
| 388 | * @param string|null $name |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | public function setReplyTo($address, $name = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string|array $address |
||
| 400 | * @param string|null $name |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function addReplyTo($address, $name = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getSubject() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $subject The Subject line for the email |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | public function setSubject($subject) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return int |
||
| 431 | */ |
||
| 432 | public function getPriority() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param int $priority |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | public function setPriority($priority) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $path Path to file |
||
| 450 | * @param string $alias An override for the name of the file |
||
| 451 | * @param string $mime The mime type for the attachment |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | public function addAttachment($path, $alias = null, $mime = null) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $data |
||
| 470 | * @param string $name |
||
| 471 | * @param string $mime |
||
| 472 | * @return $this |
||
| 473 | */ |
||
| 474 | public function addAttachmentFromData($data, $name, $mime = null) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return array|ViewableData The template data |
||
| 487 | */ |
||
| 488 | public function getData() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param array|ViewableData $data The template data to set |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | public function setData($data) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string|array $name The data name to add or array to names => value |
||
| 506 | * @param string|null $value The value of the data to add |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | public function addData($name, $value = null) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Remove a datum from the message |
||
| 524 | * |
||
| 525 | * @param string $name |
||
| 526 | * @return $this |
||
| 527 | */ |
||
| 528 | public function removeData($name) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | public function getBody() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $body The email body |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | public function setBody($body) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return string The base URL for the email |
||
| 561 | */ |
||
| 562 | public function BaseURL() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Debugging help |
||
| 569 | * |
||
| 570 | * @return string Debug info |
||
| 571 | */ |
||
| 572 | public function debug() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public function getHTMLTemplate() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Set the template to render the email with |
||
| 589 | * |
||
| 590 | * @param string $template |
||
| 591 | * @return $this |
||
| 592 | */ |
||
| 593 | public function setHTMLTemplate($template) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get the template to render the plain part with |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | public function getPlainTemplate() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Set the template to render the plain part with |
||
| 615 | * |
||
| 616 | * @param string $template |
||
| 617 | * @return $this |
||
| 618 | */ |
||
| 619 | public function setPlainTemplate($template) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param array $recipients |
||
| 631 | * @return $this |
||
| 632 | */ |
||
| 633 | public function setFailedRecipients($recipients) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @return array |
||
| 642 | */ |
||
| 643 | public function getFailedRecipients() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Used by {@link SSViewer} templates to detect if we're rendering an email template rather than a page template |
||
| 650 | * |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | public function IsEmail() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Send the message to the recipients |
||
| 660 | * |
||
| 661 | * @return bool true if successful or array of failed recipients |
||
| 662 | */ |
||
| 663 | public function send() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return array|bool |
||
| 676 | */ |
||
| 677 | public function sendPlain() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Render the email |
||
| 687 | * @param bool $plainOnly Only render the message as plain text |
||
| 688 | * @return $this |
||
| 689 | */ |
||
| 690 | public function render($plainOnly = false) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @return Swift_MimePart|false |
||
| 732 | */ |
||
| 733 | public function findPlainPart() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @return bool |
||
| 745 | */ |
||
| 746 | public function hasPlainPart() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Automatically adds a plain part to the email generated from the current Body |
||
| 756 | * |
||
| 757 | * @return $this |
||
| 758 | */ |
||
| 759 | public function generatePlainPartFromBody() |
||
| 769 | } |
||
| 770 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.