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 | /** |
||
| 316 | * Set the attachments for an email message |
||
| 317 | * |
||
| 318 | * @since 2.03.04 |
||
| 319 | */ |
||
| 320 | private function set_attachments() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Check if an email should send |
||
| 331 | * |
||
| 332 | * @since 2.03.04 |
||
| 333 | * |
||
| 334 | * @return bool|mixed|void |
||
| 335 | */ |
||
| 336 | public function should_send() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if an email has any recipients |
||
| 360 | * |
||
| 361 | * @since 2.03.04 |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | private function has_recipients() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Send an email |
||
| 375 | * |
||
| 376 | * @since 2.03.04 |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function send() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Send a single email |
||
| 400 | * |
||
| 401 | * @since 2.03.04 |
||
| 402 | * |
||
| 403 | * @param array|string $recipient |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | private function send_single( $recipient ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Package the email header |
||
| 430 | * |
||
| 431 | * @since 2.03.04 |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | private function package_header() { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the userID field ID and key for email settings |
||
| 455 | * |
||
| 456 | * @since 2.03.04 |
||
| 457 | * |
||
| 458 | * @param $form_id |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | private function get_user_id_args( $form_id ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Prepare the to, cc, bcc, reply_to, and from setting |
||
| 478 | * |
||
| 479 | * @since 2.03.04 |
||
| 480 | * |
||
| 481 | * @param string $value |
||
| 482 | * @param array $user_id_args |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | private function prepare_email_setting( $value, $user_id_args ) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Extract the emails from cc and bcc. Allow separation by , or ;. |
||
| 504 | * Trim the emails here as well |
||
| 505 | * |
||
| 506 | * @since 2.03.04 |
||
| 507 | * |
||
| 508 | * @param string $emails |
||
| 509 | * @return array|string $emails |
||
| 510 | */ |
||
| 511 | private function explode_emails( $emails ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Format the recipients( to, cc, bcc) |
||
| 524 | * |
||
| 525 | * @param array $recipients |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | private function format_recipients( $recipients ) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Format the From header |
||
| 562 | * |
||
| 563 | * @param string $from |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | private function format_from( $from ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Format the Reply To property |
||
| 597 | * |
||
| 598 | * @since 2.03.04 |
||
| 599 | * |
||
| 600 | * @param string $reply_to |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | private function format_reply_to( $reply_to ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get the name and email for the From or Reply To header |
||
| 620 | * |
||
| 621 | * @since 2.03.04 |
||
| 622 | * |
||
| 623 | * @param string $sender |
||
| 624 | * |
||
| 625 | * @return array |
||
| 626 | */ |
||
| 627 | private function get_name_and_email_for_sender( $sender ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Remove phone numbers from To addresses |
||
| 644 | * Send the phone numbers to the frm_send_to_not_email hook |
||
| 645 | * |
||
| 646 | * @since 2.03.04 |
||
| 647 | */ |
||
| 648 | private function handle_phone_numbers() { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Package an array of FrmEmail properties |
||
| 678 | * |
||
| 679 | * @since 2.03.04 |
||
| 680 | * |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | public function package_atts() { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Remove the Buddypress email filters |
||
| 699 | * |
||
| 700 | * @since 2.03.04 |
||
| 701 | */ |
||
| 702 | private function remove_buddypress_filters() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Add Mandrill line break filter |
||
| 709 | * Remove line breaks in HTML emails to prevent conflicts with Mandrill |
||
| 710 | * |
||
| 711 | * @since 2.03.04 |
||
| 712 | */ |
||
| 713 | private function add_mandrill_filter() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Remove Mandrill line break filter |
||
| 721 | * |
||
| 722 | * @since 2.03.04 |
||
| 723 | */ |
||
| 724 | private function remove_mandrill_filter() { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Encode the email subject |
||
| 730 | * |
||
| 731 | * @param string $subject |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | private function encode_subject( $subject ) { |
||
| 742 | |||
| 743 | } |