Complex classes like FrmEmail 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 FrmEmail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmEmail { |
||
| 7 | |||
| 8 | private $email_key = ''; |
||
| 9 | private $to = array(); |
||
| 10 | private $cc = array(); |
||
| 11 | private $bcc = array(); |
||
| 12 | private $from = ''; |
||
| 13 | private $reply_to = ''; |
||
| 14 | private $subject = ''; |
||
| 15 | private $message = ''; |
||
| 16 | private $attachments = array(); |
||
| 17 | |||
| 18 | private $is_plain_text = false; |
||
| 19 | private $is_single_recipient = false; |
||
| 20 | private $include_user_info = false; |
||
| 21 | |||
| 22 | private $charset = ''; |
||
| 23 | private $content_type = 'text/html'; |
||
| 24 | |||
| 25 | private $settings = array(); |
||
| 26 | private $entry; |
||
| 27 | private $form; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * FrmEmail constructor |
||
| 31 | * |
||
| 32 | * @param object $action |
||
| 33 | * @param object $entry |
||
| 34 | * @param object $form |
||
| 35 | */ |
||
| 36 | public function __construct( $action, $entry, $form ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set the email key property |
||
| 68 | * |
||
| 69 | * @since 2.03.04 |
||
| 70 | * |
||
| 71 | * @param object $action |
||
| 72 | */ |
||
| 73 | private function set_email_key( $action ) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set the to addresses |
||
| 79 | * |
||
| 80 | * @since 2.03.04 |
||
| 81 | * |
||
| 82 | * @param array $user_id_args |
||
| 83 | */ |
||
| 84 | private function set_to( $user_id_args ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set the CC addresses |
||
| 113 | * |
||
| 114 | * @since 2.03.04 |
||
| 115 | * |
||
| 116 | * @param array $user_id_args |
||
| 117 | */ |
||
| 118 | private function set_cc( $user_id_args ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the BCC addresses |
||
| 124 | * |
||
| 125 | * @since 2.03.04 |
||
| 126 | * |
||
| 127 | * @param array $user_id_args |
||
| 128 | */ |
||
| 129 | private function set_bcc( $user_id_args ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Prepare CC and BCC recipients |
||
| 135 | * |
||
| 136 | * @since 2.03.04 |
||
| 137 | * |
||
| 138 | * @param string $recipients |
||
| 139 | * @param array $user_id_args |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | private function prepare_additional_recipients( $recipients, $user_id_args ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the From addresses |
||
| 155 | * |
||
| 156 | * @since 2.03.04 |
||
| 157 | * |
||
| 158 | * @param array $user_id_args |
||
| 159 | */ |
||
| 160 | private function set_from( $user_id_args ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the Reply To addresses |
||
| 172 | * |
||
| 173 | * @since 2.03.04 |
||
| 174 | * |
||
| 175 | * @param array $user_id_args |
||
| 176 | */ |
||
| 177 | private function set_reply_to( $user_id_args ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set the is_plain_text property |
||
| 190 | * This should be set before the message |
||
| 191 | * |
||
| 192 | * @since 2.03.04 |
||
| 193 | */ |
||
| 194 | private function set_is_plain_text() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set the include_user_info property |
||
| 202 | * This should be set before the message |
||
| 203 | * |
||
| 204 | * @since 2.03.04 |
||
| 205 | */ |
||
| 206 | private function set_include_user_info() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set the is_single_recipient property |
||
| 214 | * |
||
| 215 | * @since 2.03.04 |
||
| 216 | * |
||
| 217 | * @param $action |
||
| 218 | */ |
||
| 219 | private function set_is_single_recipient( $action ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the charset |
||
| 236 | * |
||
| 237 | * @since 2.03.04 |
||
| 238 | */ |
||
| 239 | private function set_charset() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Set the content type |
||
| 245 | * |
||
| 246 | * @since 2.03.04 |
||
| 247 | */ |
||
| 248 | private function set_content_type() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set the subject |
||
| 256 | * |
||
| 257 | * @since 2.03.04 |
||
| 258 | */ |
||
| 259 | private function set_subject() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Set the email message |
||
| 281 | * |
||
| 282 | * @since 2.03.04 |
||
| 283 | */ |
||
| 284 | private function set_message() { |
||
| 315 | |||
| 316 | private function maybe_add_ip( &$mail_body ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the attachments for an email message |
||
| 324 | * |
||
| 325 | * @since 2.03.04 |
||
| 326 | */ |
||
| 327 | private function set_attachments() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Check if an email should send |
||
| 338 | * |
||
| 339 | * @since 2.03.04 |
||
| 340 | * |
||
| 341 | * @return bool|mixed|void |
||
| 342 | */ |
||
| 343 | public function should_send() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if an email has any recipients |
||
| 367 | * |
||
| 368 | * @since 2.03.04 |
||
| 369 | * |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | private function has_recipients() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Send an email |
||
| 382 | * |
||
| 383 | * @since 2.03.04 |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function send() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Send a single email |
||
| 407 | * |
||
| 408 | * @since 2.03.04 |
||
| 409 | * |
||
| 410 | * @param array|string $recipient |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | private function send_single( $recipient ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Package the email header |
||
| 437 | * |
||
| 438 | * @since 2.03.04 |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | private function package_header() { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the userID field ID and key for email settings |
||
| 462 | * |
||
| 463 | * @since 2.03.04 |
||
| 464 | * |
||
| 465 | * @param $form_id |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | private function get_user_id_args( $form_id ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Prepare the to, cc, bcc, reply_to, and from setting |
||
| 485 | * |
||
| 486 | * @since 2.03.04 |
||
| 487 | * |
||
| 488 | * @param string $value |
||
| 489 | * @param array $user_id_args |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | private function prepare_email_setting( $value, $user_id_args ) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Extract the emails from cc and bcc. Allow separation by , or ;. |
||
| 511 | * Trim the emails here as well |
||
| 512 | * |
||
| 513 | * @since 2.03.04 |
||
| 514 | * |
||
| 515 | * @param string $emails |
||
| 516 | * @return array|string $emails |
||
| 517 | */ |
||
| 518 | private function explode_emails( $emails ) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Format the recipients( to, cc, bcc) |
||
| 531 | * |
||
| 532 | * @param array $recipients |
||
| 533 | * |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | private function format_recipients( $recipients ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Format the From header |
||
| 569 | * |
||
| 570 | * @param string $from |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | private function format_from( $from ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Format the Reply To property |
||
| 602 | * |
||
| 603 | * @since 2.03.04 |
||
| 604 | * |
||
| 605 | * @param string $reply_to |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | private function format_reply_to( $reply_to ) { |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Get only the email if the name and email have been combined |
||
| 622 | * |
||
| 623 | * @since 3.0.06 |
||
| 624 | */ |
||
| 625 | private function get_email_from_name( $name ) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Get the name and email for the From or Reply To header |
||
| 636 | * |
||
| 637 | * @since 2.03.04 |
||
| 638 | * |
||
| 639 | * @param string $sender |
||
| 640 | * |
||
| 641 | * @return array |
||
| 642 | */ |
||
| 643 | private function get_name_and_email_for_sender( $sender ) { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @since 3.0.06 |
||
| 660 | */ |
||
| 661 | private function format_from_email( $name, $email ) { |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Remove phone numbers from To addresses |
||
| 670 | * Send the phone numbers to the frm_send_to_not_email hook |
||
| 671 | * |
||
| 672 | * @since 2.03.04 |
||
| 673 | */ |
||
| 674 | private function handle_phone_numbers() { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Package an array of FrmEmail properties |
||
| 704 | * |
||
| 705 | * @since 2.03.04 |
||
| 706 | * |
||
| 707 | * @return array |
||
| 708 | */ |
||
| 709 | public function package_atts() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Remove the Buddypress email filters |
||
| 727 | * |
||
| 728 | * @since 2.03.04 |
||
| 729 | */ |
||
| 730 | private function remove_buddypress_filters() { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Add Mandrill line break filter |
||
| 737 | * Remove line breaks in HTML emails to prevent conflicts with Mandrill |
||
| 738 | * |
||
| 739 | * @since 2.03.04 |
||
| 740 | */ |
||
| 741 | private function add_mandrill_filter() { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Remove Mandrill line break filter |
||
| 749 | * |
||
| 750 | * @since 2.03.04 |
||
| 751 | */ |
||
| 752 | private function remove_mandrill_filter() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Encode the email subject |
||
| 758 | * |
||
| 759 | * @param string $subject |
||
| 760 | * |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | private function encode_subject( $subject ) { |
||
| 770 | } |
||
| 771 |