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_Resource_Manager 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_Resource_Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class EE_Message_Resource_Manager { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @type EE_Messenger_Collection $_messenger_collection_loader |
||
| 24 | */ |
||
| 25 | protected $_messenger_collection_loader; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @type EE_Message_Type_Collection $_message_type_collection_loader |
||
| 29 | */ |
||
| 30 | protected $_message_type_collection_loader; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @type EEM_Message_Template_Group $_message_template_group_model |
||
| 34 | */ |
||
| 35 | protected $_message_template_group_model; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @type EE_Messenger[] |
||
| 39 | */ |
||
| 40 | protected $_installed_messengers = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @type EE_message_type[] |
||
| 44 | */ |
||
| 45 | protected $_installed_message_types = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Array of active messengers. |
||
| 49 | * Format is this: |
||
| 50 | * array( |
||
| 51 | * 'messenger_name' => EE_messenger |
||
| 52 | * ) |
||
| 53 | * |
||
| 54 | * @type EE_Messenger[] |
||
| 55 | */ |
||
| 56 | protected $_active_messengers = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Formatted array of active message types grouped per messenger. |
||
| 60 | * Format is this: |
||
| 61 | * array( |
||
| 62 | * 'messenger_name' => array( |
||
| 63 | * 'settings' => array( |
||
| 64 | * '{messenger_name}-message_types' => array( |
||
| 65 | * 'message_type_name' => array() //variable array of settings corresponding to message type. |
||
| 66 | * ) |
||
| 67 | * ) |
||
| 68 | * ) |
||
| 69 | * ) |
||
| 70 | * |
||
| 71 | * @type array |
||
| 72 | */ |
||
| 73 | protected $_active_message_types = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * An array of unique message type contexts across all active message types. |
||
| 77 | * |
||
| 78 | * The array will be indexed by either 'slugs' or 'all'. |
||
| 79 | * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug. |
||
| 80 | * array( |
||
| 81 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
| 82 | * ); |
||
| 83 | * |
||
| 84 | * The all index returns an array in this format: |
||
| 85 | * array( |
||
| 86 | * 'message_type_name' => array( |
||
| 87 | * 'context_slug' => array( |
||
| 88 | * 'label' => 'localized label for context', |
||
| 89 | * 'description' => 'localized description for context' |
||
| 90 | * ) |
||
| 91 | * ) |
||
| 92 | * ); |
||
| 93 | * |
||
| 94 | * @type array |
||
| 95 | */ |
||
| 96 | protected $_contexts = array(); |
||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * EE_Message_Resource_Manager constructor. |
||
| 102 | * |
||
| 103 | * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
||
| 104 | * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
||
| 105 | * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
||
| 106 | */ |
||
| 107 | function __construct( |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @return EE_Messenger_Collection |
||
| 124 | */ |
||
| 125 | public function messenger_collection() { |
||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @return EE_Messenger[] |
||
| 133 | */ |
||
| 134 | public function active_messengers() { |
||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $messenger_name |
||
| 142 | * @return \EE_Messenger |
||
| 143 | */ |
||
| 144 | public function get_messenger( $messenger_name ) { |
||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * This returns the corresponding EE_Messenger object for the given string if it is active. |
||
| 152 | * |
||
| 153 | * @param string $messenger |
||
| 154 | * @return EE_Messenger | null |
||
| 155 | */ |
||
| 156 | public function get_active_messenger( $messenger ) { |
||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @return \EE_Messenger[] |
||
| 164 | */ |
||
| 165 | public function installed_messengers() { |
||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $messenger_name |
||
| 182 | * @return \EE_Messenger |
||
| 183 | * @throws \EE_Error |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function valid_messenger( $messenger_name ) { |
|
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @return EE_Message_Type_Collection |
||
| 202 | */ |
||
| 203 | public function message_type_collection() { |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function active_message_types() { |
||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $message_type_name |
||
| 220 | * @return \EE_Message_Type |
||
| 221 | */ |
||
| 222 | public function get_message_type( $message_type_name ) { |
||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * This returns the EE_message_type from the active message types array ( if present ); |
||
| 230 | * |
||
| 231 | * @param string $messenger_name |
||
| 232 | * @param string $message_type_name |
||
| 233 | * @return \EE_Message_Type|null |
||
| 234 | */ |
||
| 235 | public function get_active_message_type_for_messenger( $messenger_name, $message_type_name ) { |
||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * This checks the _active_message_types property for any active message types |
||
| 245 | * that are present for the given messenger and returns them. |
||
| 246 | * |
||
| 247 | * @since 4.9.0 |
||
| 248 | * @param string $messenger_name The messenger being checked |
||
| 249 | * @return EE_message_type[] (or empty array if none present) |
||
| 250 | */ |
||
| 251 | public function get_active_message_types_for_messenger( $messenger_name ) { |
||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * This does NOT return the _active_message_types property but |
||
| 270 | * simply returns an array of active message type names from that property. |
||
| 271 | * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
||
| 272 | * |
||
| 273 | * @return array message_type references (string) |
||
| 274 | */ |
||
| 275 | public function list_of_active_message_types() { |
||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
||
| 291 | * |
||
| 292 | * @since 4.9.0 |
||
| 293 | * @return \EE_message_type[] |
||
| 294 | */ |
||
| 295 | public function get_active_message_type_objects() { |
||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @return \EE_Message_Type[] |
||
| 312 | */ |
||
| 313 | public function installed_message_types() { |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $message_type_name |
||
| 327 | * @return \EE_message_type |
||
| 328 | * @throws \EE_Error |
||
| 329 | */ |
||
| 330 | View Code Duplication | public function valid_message_type( $message_type_name ) { |
|
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * valid_message_type_for_messenger |
||
| 347 | * |
||
| 348 | * @param EE_Messenger $messenger |
||
| 349 | * @param string $message_type_name |
||
| 350 | * @return array |
||
| 351 | * @throws \EE_Error |
||
| 352 | */ |
||
| 353 | public function valid_message_type_for_messenger( EE_Messenger $messenger, $message_type_name ) { |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * Used to return active messengers array stored in the wp options table. |
||
| 374 | * If no value is present in the option then an empty array is returned. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function get_active_messengers_option() { |
||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * Used to update the active messengers array stored in the wp options table. |
||
| 387 | * |
||
| 388 | * @param array $active_messengers Incoming data to save. |
||
| 389 | * @return bool FALSE if not updated, TRUE if updated. |
||
| 390 | */ |
||
| 391 | public function update_active_messengers_option( $active_messengers ) { |
||
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Used to return active messengers array stored in the wp options table. |
||
| 399 | * If no value is present in the option then an empty array is returned. |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function get_has_activated_messengers_option() { |
||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Used to update the active messengers array stored in the wp options table. |
||
| 411 | * |
||
| 412 | * @param array $has_activated_messengers Incoming data to save. |
||
| 413 | * @return bool FALSE if not updated, TRUE if updated. |
||
| 414 | */ |
||
| 415 | public function update_has_activated_messengers_option( $has_activated_messengers ) { |
||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Generate list of active messengers and message types from collection. |
||
| 423 | * This sets up the active messengers from what is present in the database. |
||
| 424 | */ |
||
| 425 | protected function _set_active_messengers_and_message_types() { |
||
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Ensures that the specified messenger is currently active. |
||
| 461 | * If not, activates it and its default message types. |
||
| 462 | * |
||
| 463 | * @param string $messenger_name |
||
| 464 | * @return boolean TRUE if it was PREVIOUSLY active, and FALSE if it was previously inactive |
||
| 465 | */ |
||
| 466 | public function ensure_messenger_is_active( $messenger_name ) { |
||
| 473 | |||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
||
| 478 | * This ALSO ensures that the given messenger is active as well! |
||
| 479 | * |
||
| 480 | * @param string $message_type message type name |
||
| 481 | * @param $messenger_name |
||
| 482 | * @return array |
||
| 483 | * @throws \EE_Error |
||
| 484 | */ |
||
| 485 | public function ensure_message_type_is_active( $message_type, $messenger_name ) { |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Activates the specified messenger. |
||
| 496 | * |
||
| 497 | * @param string $messenger_name |
||
| 498 | * @param array $message_type_names An array of message type names to activate with this messenger. |
||
| 499 | * If included we do NOT setup the default message types |
||
| 500 | * (assuming they are already setup.) |
||
| 501 | * @return array of generated templates |
||
| 502 | */ |
||
| 503 | public function activate_messenger( $messenger_name, $message_type_names = array() ) { |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Activates given message types for the given EE_Messenger object. |
||
| 523 | * |
||
| 524 | * @param \EE_Messenger $messenger |
||
| 525 | * @param array $message_type_names |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | protected function _activate_message_types( EE_Messenger $messenger, $message_type_names = array() ) { |
||
| 551 | |||
| 552 | |||
| 553 | |||
| 554 | /** |
||
| 555 | * _get_settings_for_message_type |
||
| 556 | * |
||
| 557 | * @access protected |
||
| 558 | * @param \EE_Messenger $messenger |
||
| 559 | * @param string $message_type_name |
||
| 560 | */ |
||
| 561 | protected function _add_settings_for_message_type( EE_Messenger $messenger, $message_type_name ) { |
||
| 574 | |||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * _set_messenger_has_activated_message_type |
||
| 579 | * |
||
| 580 | * @access protected |
||
| 581 | * @param \EE_Messenger $messenger |
||
| 582 | * @param array $has_activated |
||
| 583 | * @param string $message_type_name |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | protected function _set_messenger_has_activated_message_type( EE_Messenger $messenger, $has_activated, $message_type_name ) { |
||
| 597 | |||
| 598 | |||
| 599 | |||
| 600 | /** |
||
| 601 | * _add_settings_for_messenger |
||
| 602 | * |
||
| 603 | * @access protected |
||
| 604 | * @param \EE_Messenger $messenger |
||
| 605 | */ |
||
| 606 | protected function _add_settings_for_messenger( EE_Messenger $messenger ) { |
||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * _deactivate_messenger |
||
| 619 | * |
||
| 620 | * @access protected |
||
| 621 | * @param string $messenger name of messenger |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | protected function _deactivate_messenger( $messenger ) { |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
||
| 634 | * |
||
| 635 | * @param string $message_type_name name of message type being deactivated |
||
| 636 | */ |
||
| 637 | protected function _deactivate_message_type( $message_type_name ) { |
||
| 646 | |||
| 647 | |||
| 648 | |||
| 649 | /** |
||
| 650 | * Used to verify if a message can be sent for the given messenger and message type |
||
| 651 | * and that it is a generating messenger (used for generating message templates). |
||
| 652 | * |
||
| 653 | * @param EE_Messenger $messenger messenger used in trigger |
||
| 654 | * @param EE_message_type $message_type message type used in trigger |
||
| 655 | * |
||
| 656 | * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
||
| 657 | */ |
||
| 658 | public function is_generating_messenger_and_active( EE_Messenger $messenger, EE_message_type $message_type ) { |
||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | /** |
||
| 674 | * This returns all the contexts that are registered by all message types. |
||
| 675 | * |
||
| 676 | * If $slugs_only is true, then just an array indexed by unique context slugs with the latest label representation for that slug. |
||
| 677 | * array( |
||
| 678 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
| 679 | * ); |
||
| 680 | * |
||
| 681 | * If $slugs_only is false, then the format is: |
||
| 682 | * array( |
||
| 683 | * 'message_type_name' => array( |
||
| 684 | * 'context_slug' => array( |
||
| 685 | * 'label' => 'localized label for context', |
||
| 686 | * 'description' => 'localized description for context' |
||
| 687 | * ) |
||
| 688 | * ) |
||
| 689 | * ); |
||
| 690 | * |
||
| 691 | * Keep in mind that although different message types may share the same context slugs, it is possible that the context |
||
| 692 | * is described differently by the message type. |
||
| 693 | * |
||
| 694 | * >>>>>>>>>>>> 1 usage in \EE_Message_List_Table::_get_table_filters() |
||
| 695 | * >>>>>>>>>>>> 1 usage in \EE_Message::context_label() |
||
| 696 | * >>>>>>>>>>>> 1 usage in \EE_messages_Test::test_get_all_contexts() |
||
| 697 | * |
||
| 698 | * @since 4.9.0 |
||
| 699 | * @param bool $slugs_only Whether to return an array of just slugs and labels (true) or all contexts indexed by message type. |
||
| 700 | * @return array |
||
| 701 | */ |
||
| 702 | public function get_all_contexts( $slugs_only = true ) { |
||
| 722 | |||
| 723 | |||
| 724 | } |
||
| 725 | // End of file EE_Message_Resource_Manager.lib.php |
||
| 726 | // Location: /EE_Message_Resource_Manager.lib.php |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..