Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Message 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 EE_Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
| 11 | class EE_Message extends EE_Base_Class implements EEI_Admin_Links { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @deprecated 4.9.0 Added for backward compat with add-on's |
||
| 15 | * @type null |
||
| 16 | */ |
||
| 17 | public $template_pack; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @deprecated 4.9.0 Added for backward compat with add-on's |
||
| 21 | * @type null |
||
| 22 | */ |
||
| 23 | public $template_variation; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @deprecated 4.9.0 Added for backward compat with add-on's |
||
| 27 | * @type string |
||
| 28 | */ |
||
| 29 | public $content = ''; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @type EE_messenger $_messenger |
||
| 34 | */ |
||
| 35 | protected $_messenger = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @type EE_message_type $_message_type |
||
| 39 | */ |
||
| 40 | protected $_message_type = null; |
||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * |
||
| 46 | * @param array $props_n_values |
||
| 47 | * @param string $timezone |
||
| 48 | * @param array $date_formats incoming date formats in an array. First value is the date_format, second is time format. |
||
| 49 | * @return EE_Message |
||
| 50 | */ |
||
| 51 | public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) { |
||
| 52 | $has_object = parent::_check_for_object( $props_n_values, __CLASS__ ); |
||
|
|
|||
| 53 | //if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db. |
||
| 54 | if ( ! $has_object ) { |
||
| 55 | EE_Registry::instance()->load_helper( 'URL' ); |
||
| 56 | $props_n_values['MSG_token'] = EEH_URL::generate_unique_token(); |
||
| 57 | } |
||
| 58 | return $has_object ? $has_object : new self( $props_n_values, false, $timezone, $date_formats ); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * @param array $props_n_values |
||
| 66 | * @param string $timezone |
||
| 67 | * @return EE_Message |
||
| 68 | */ |
||
| 69 | public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) { |
||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets MSG_token |
||
| 77 | * |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | public function MSG_token() { |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Sets MSG_token |
||
| 88 | * |
||
| 89 | * @param int $MSG_token |
||
| 90 | */ |
||
| 91 | public function set_MSG_token( $MSG_token) { |
||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Gets GRP_ID |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function GRP_ID() { |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Sets GRP_ID |
||
| 111 | * |
||
| 112 | * @param int $GRP_ID |
||
| 113 | */ |
||
| 114 | public function set_GRP_ID( $GRP_ID ) { |
||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets TXN_ID |
||
| 123 | * |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function TXN_ID() { |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets TXN_ID |
||
| 134 | * |
||
| 135 | * @param int $TXN_ID |
||
| 136 | */ |
||
| 137 | public function set_TXN_ID( $TXN_ID) { |
||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Gets messenger |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function messenger() { |
||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets messenger |
||
| 157 | * |
||
| 158 | * @param string $messenger |
||
| 159 | */ |
||
| 160 | public function set_messenger( $messenger ) { |
||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns corresponding messenger object for the set messenger on this message |
||
| 168 | * |
||
| 169 | * @return EE_messenger | null |
||
| 170 | */ |
||
| 171 | public function messenger_object() { |
||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Sets messenger |
||
| 179 | * |
||
| 180 | * @param EE_messenger $messenger |
||
| 181 | */ |
||
| 182 | public function set_messenger_object( EE_messenger $messenger ) { |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * validates messenger |
||
| 190 | * |
||
| 191 | * @param bool $throw_exceptions |
||
| 192 | * @return bool |
||
| 193 | * @throws \EE_Error |
||
| 194 | */ |
||
| 195 | public function valid_messenger( $throw_exceptions = false ) { |
||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * This returns the set localized label for the messenger on this message. |
||
| 217 | * Note, if unable to retrieve the EE_messenger object then will just return the messenger slug saved |
||
| 218 | * with this message. |
||
| 219 | * |
||
| 220 | * @param bool $plural whether to return the plural label or not. |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function messenger_label( $plural = false ) { |
||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets message_type |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function message_type() { |
||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Sets message_type |
||
| 244 | * |
||
| 245 | * @param string $message_type |
||
| 246 | */ |
||
| 247 | public function set_message_type( $message_type ) { |
||
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns the message type object for the set message type on this message |
||
| 255 | * |
||
| 256 | * @return EE_message_type | null |
||
| 257 | */ |
||
| 258 | public function message_type_object() { |
||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Sets message_type |
||
| 266 | * |
||
| 267 | * @param EE_message_type $message_type |
||
| 268 | * @param bool $set_priority This indicates whether to set the priority to whatever the priority is on |
||
| 269 | * the message type or not. |
||
| 270 | */ |
||
| 271 | public function set_message_type_object( EE_message_type $message_type, $set_priority = false ) { |
||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * validates message_type |
||
| 282 | * |
||
| 283 | * @param bool $throw_exceptions |
||
| 284 | * @return bool |
||
| 285 | * @throws \EE_Error |
||
| 286 | */ |
||
| 287 | public function valid_message_type( $throw_exceptions = false ) { |
||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * validates messenger and message_type (that they are valid EE_messenger and EE_message_type objects). |
||
| 309 | * |
||
| 310 | * @param bool $throw_exceptions |
||
| 311 | * @return bool |
||
| 312 | * @throws \EE_Error |
||
| 313 | */ |
||
| 314 | public function is_valid( $throw_exceptions = false ) { |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * This validates whether the internal messenger and message type objects are valid for sending. |
||
| 324 | * |
||
| 325 | * Three checks are done: |
||
| 326 | * |
||
| 327 | * 1. There is a valid messenger object. |
||
| 328 | * 2. There is a valid message type object. |
||
| 329 | * 3. The message type object is active for the messenger. |
||
| 330 | * |
||
| 331 | * @throws EE_Error But only if $throw_exceptions is set to true. |
||
| 332 | * |
||
| 333 | * @param bool $throw_exceptions |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function is_valid_for_sending_or_generation( $throw_exceptions = false ) { |
||
| 337 | $valid = false; |
||
| 338 | if ( $this->is_valid( $throw_exceptions ) ) { |
||
| 339 | /** @var EE_Message_Resource_Manager $message_resource_manager */ |
||
| 340 | $message_resource_manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' ); |
||
| 341 | $valid = $message_resource_manager->is_message_type_active_for_messenger( $this->messenger(), $this->message_type() ); |
||
| 342 | if ( ! $valid && $throw_exceptions ) { |
||
| 343 | throw new EE_Error( |
||
| 344 | sprintf( |
||
| 345 | __( 'The %1$s message type is not a valid message type for the %2$s messenger so it will not be sent.', 'event_espresso' ), |
||
| 346 | $this->message_type(), |
||
| 347 | $this->messenger() |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | return $valid; |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * This returns the set localized label for the message type on this message. |
||
| 359 | * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved |
||
| 360 | * with this message. |
||
| 361 | * |
||
| 362 | * @param bool $plural whether to return the plural label or not. |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function message_type_label( $plural = false ) { |
||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets context |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function context() { |
||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * This returns the corresponding localized label for the given context slug, if possible from installed message types. |
||
| 387 | * Otherwise, this will just return the set context slug on this object. |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function context_label() { |
||
| 397 | |||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Sets context |
||
| 402 | * |
||
| 403 | * @param string $context |
||
| 404 | */ |
||
| 405 | public function set_context( $context ) { |
||
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Gets recipient_ID |
||
| 413 | * |
||
| 414 | * @return int |
||
| 415 | */ |
||
| 416 | public function recipient_ID() { |
||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * Sets recipient_ID |
||
| 424 | * |
||
| 425 | * @param string $recipient_ID |
||
| 426 | */ |
||
| 427 | public function set_recipient_ID( $recipient_ID ) { |
||
| 430 | |||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Gets recipient_type |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function recipient_type() { |
||
| 441 | |||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * Return the related object matching the recipient type and ID. |
||
| 447 | * |
||
| 448 | * @return EE_Base_Class | null |
||
| 449 | */ |
||
| 450 | public function recipient_object() { |
||
| 457 | |||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Sets recipient_type |
||
| 462 | * |
||
| 463 | * @param string $recipient_type |
||
| 464 | */ |
||
| 465 | public function set_recipient_type( $recipient_type ) { |
||
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * Gets content |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function content() { |
||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * Sets content |
||
| 484 | * |
||
| 485 | * @param string $content |
||
| 486 | */ |
||
| 487 | public function set_content( $content ) { |
||
| 490 | |||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets subject |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function subject() { |
||
| 501 | |||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * Sets subject |
||
| 506 | * |
||
| 507 | * @param string $subject |
||
| 508 | */ |
||
| 509 | public function set_subject( $subject ) { |
||
| 512 | |||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets to |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function to() { |
||
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Sets to |
||
| 529 | * |
||
| 530 | * @param string $to |
||
| 531 | */ |
||
| 532 | public function set_to( $to ) { |
||
| 535 | |||
| 536 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * Gets from |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function from() { |
||
| 546 | |||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * Sets from |
||
| 551 | * |
||
| 552 | * @param string $from |
||
| 553 | */ |
||
| 554 | public function set_from( $from ) { |
||
| 557 | |||
| 558 | |||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * Gets priority |
||
| 564 | * |
||
| 565 | * @return int |
||
| 566 | */ |
||
| 567 | public function priority() { |
||
| 570 | |||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Sets priority |
||
| 575 | * |
||
| 576 | * Note. Send Now Messengers always override any priority that may be set on a Message. So |
||
| 577 | * this method calls the send_now method to verify that. |
||
| 578 | * |
||
| 579 | * @param int $priority |
||
| 580 | */ |
||
| 581 | public function set_priority( $priority ) { |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Overrides parent::set method so we can capture any sets for priority. |
||
| 589 | * @see parent::set() for phpdocs |
||
| 590 | * @param string $field_name |
||
| 591 | * @param mixed $field_value |
||
| 592 | * @param bool $use_default |
||
| 593 | * @throws EE_Error |
||
| 594 | */ |
||
| 595 | public function set( $field_name, $field_value, $use_default = false ) { |
||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | /** |
||
| 605 | * @return bool |
||
| 606 | * @throws \EE_Error |
||
| 607 | */ |
||
| 608 | public function send_now() { |
||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * Gets STS_ID |
||
| 617 | * |
||
| 618 | * @return string |
||
| 619 | */ |
||
| 620 | public function STS_ID() { |
||
| 623 | |||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * Sets STS_ID |
||
| 628 | * |
||
| 629 | * @param string $STS_ID |
||
| 630 | */ |
||
| 631 | public function set_STS_ID( $STS_ID ) { |
||
| 634 | |||
| 635 | |||
| 636 | |||
| 637 | /** |
||
| 638 | * Gets created |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public function created() { |
||
| 645 | |||
| 646 | |||
| 647 | |||
| 648 | /** |
||
| 649 | * Sets created |
||
| 650 | * |
||
| 651 | * @param string $created |
||
| 652 | */ |
||
| 653 | public function set_created( $created ) { |
||
| 656 | |||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * Gets modified |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | public function modified() { |
||
| 667 | |||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * Sets modified |
||
| 672 | * |
||
| 673 | * @param string $modified |
||
| 674 | */ |
||
| 675 | public function set_modified( $modified ) { |
||
| 678 | |||
| 679 | |||
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * Sets generation data for this message. |
||
| 684 | * @param mixed $data |
||
| 685 | */ |
||
| 686 | public function set_generation_data( $data ) { |
||
| 689 | |||
| 690 | |||
| 691 | |||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns any set generation data for this message. |
||
| 696 | * @return mixed|null |
||
| 697 | */ |
||
| 698 | public function get_generation_data() { |
||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * Gets any error message. |
||
| 707 | * @return mixed|null |
||
| 708 | */ |
||
| 709 | public function error_message() { |
||
| 712 | |||
| 713 | |||
| 714 | /** |
||
| 715 | * Sets an error message. |
||
| 716 | * @param $message |
||
| 717 | * @return bool|int |
||
| 718 | */ |
||
| 719 | public function set_error_message( $message ) { |
||
| 722 | |||
| 723 | |||
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * This retrieves the associated template pack with this message. |
||
| 728 | * @return EE_Messages_Template_Pack | null |
||
| 729 | */ |
||
| 730 | View Code Duplication | public function get_template_pack() { |
|
| 754 | |||
| 755 | |||
| 756 | |||
| 757 | |||
| 758 | /** |
||
| 759 | * Retrieves the variation used for generating this message. |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | View Code Duplication | public function get_template_pack_variation() { |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Return the link to the admin details for the object. |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | public function get_admin_details_link() { |
||
| 794 | EE_Registry::instance()->load_helper( 'URL' ); |
||
| 795 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 796 | switch ( $this->STS_ID() ) { |
||
| 797 | case EEM_Message::status_failed : |
||
| 798 | case EEM_Message::status_debug_only : |
||
| 799 | return EEH_MSG_Template::generate_error_display_trigger( $this ); |
||
| 800 | break; |
||
| 801 | |||
| 802 | case EEM_Message::status_sent : |
||
| 803 | return EEH_MSG_Template::generate_browser_trigger( $this ); |
||
| 804 | break; |
||
| 805 | |||
| 806 | default : |
||
| 807 | return ''; |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns the link to the editor for the object. Sometimes this is the same as the details. |
||
| 813 | * @return string |
||
| 814 | */ |
||
| 815 | public function get_admin_edit_link() { |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Returns the link to a settings page for the object. |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | View Code Duplication | public function get_admin_settings_link() { |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Returns the link to the "overview" for the object (typically the "list table" view). |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | public function get_admin_overview_link() { |
||
| 848 | |||
| 849 | |||
| 850 | } |
||
| 851 | /* End of file EE_Message.class.php */ |
||
| 852 | /* Location: /core/db_classes/EE_Message.class.php */ |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.