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() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set the email message |
||
| 280 | * |
||
| 281 | * @since 2.03.04 |
||
| 282 | */ |
||
| 283 | private function set_message() { |
||
| 314 | |||
| 315 | private function maybe_add_ip( &$mail_body ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Set the attachments for an email message |
||
| 323 | * |
||
| 324 | * @since 2.03.04 |
||
| 325 | */ |
||
| 326 | private function set_attachments() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if an email should send |
||
| 337 | * |
||
| 338 | * @since 2.03.04 |
||
| 339 | * |
||
| 340 | * @return bool|mixed|void |
||
| 341 | */ |
||
| 342 | public function should_send() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Check if an email has any recipients |
||
| 366 | * |
||
| 367 | * @since 2.03.04 |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | private function has_recipients() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Send an email |
||
| 381 | * |
||
| 382 | * @since 2.03.04 |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function send() { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Send a single email |
||
| 406 | * |
||
| 407 | * @since 2.03.04 |
||
| 408 | * |
||
| 409 | * @param array|string $recipient |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | private function send_single( $recipient ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Package the email header |
||
| 436 | * |
||
| 437 | * @since 2.03.04 |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | private function package_header() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the userID field ID and key for email settings |
||
| 461 | * |
||
| 462 | * @since 2.03.04 |
||
| 463 | * |
||
| 464 | * @param $form_id |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | private function get_user_id_args( $form_id ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Prepare the to, cc, bcc, reply_to, and from setting |
||
| 484 | * |
||
| 485 | * @since 2.03.04 |
||
| 486 | * |
||
| 487 | * @param string $value |
||
| 488 | * @param array $user_id_args |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function prepare_email_setting( $value, $user_id_args ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Extract the emails from cc and bcc. Allow separation by , or ;. |
||
| 510 | * Trim the emails here as well |
||
| 511 | * |
||
| 512 | * @since 2.03.04 |
||
| 513 | * |
||
| 514 | * @param string $emails |
||
| 515 | * @return array|string $emails |
||
| 516 | */ |
||
| 517 | private function explode_emails( $emails ) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Format the recipients( to, cc, bcc) |
||
| 530 | * |
||
| 531 | * @param array $recipients |
||
| 532 | * |
||
| 533 | * @return array |
||
| 534 | */ |
||
| 535 | private function format_recipients( $recipients ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Format the From header |
||
| 568 | * |
||
| 569 | * @param string $from |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | private function format_from( $from ) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Format the Reply To property |
||
| 603 | * |
||
| 604 | * @since 2.03.04 |
||
| 605 | * |
||
| 606 | * @param string $reply_to |
||
| 607 | * |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | private function format_reply_to( $reply_to ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Get the name and email for the From or Reply To header |
||
| 626 | * |
||
| 627 | * @since 2.03.04 |
||
| 628 | * |
||
| 629 | * @param string $sender |
||
| 630 | * |
||
| 631 | * @return array |
||
| 632 | */ |
||
| 633 | private function get_name_and_email_for_sender( $sender ) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Remove phone numbers from To addresses |
||
| 650 | * Send the phone numbers to the frm_send_to_not_email hook |
||
| 651 | * |
||
| 652 | * @since 2.03.04 |
||
| 653 | */ |
||
| 654 | private function handle_phone_numbers() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Package an array of FrmEmail properties |
||
| 684 | * |
||
| 685 | * @since 2.03.04 |
||
| 686 | * |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | public function package_atts() { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Remove the Buddypress email filters |
||
| 707 | * |
||
| 708 | * @since 2.03.04 |
||
| 709 | */ |
||
| 710 | private function remove_buddypress_filters() { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Add Mandrill line break filter |
||
| 717 | * Remove line breaks in HTML emails to prevent conflicts with Mandrill |
||
| 718 | * |
||
| 719 | * @since 2.03.04 |
||
| 720 | */ |
||
| 721 | private function add_mandrill_filter() { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Remove Mandrill line break filter |
||
| 729 | * |
||
| 730 | * @since 2.03.04 |
||
| 731 | */ |
||
| 732 | private function remove_mandrill_filter() { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Encode the email subject |
||
| 738 | * |
||
| 739 | * @param string $subject |
||
| 740 | * |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | private function encode_subject( $subject ) { |
||
| 750 | |||
| 751 | } |