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_Messages 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_Messages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class EE_Messages { |
||
18 | |||
19 | |||
20 | /** |
||
21 | * Array of active messengers. |
||
22 | * Format is this: |
||
23 | * array( |
||
24 | * 'messenger_name' => EE_messenger |
||
25 | * ) |
||
26 | * |
||
27 | * @type EE_Messenger[] |
||
28 | */ |
||
29 | protected $_active_messengers = array(); |
||
30 | |||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * Formatted array of active message types grouped per messenger. |
||
36 | * Format is this: |
||
37 | * array( |
||
38 | * 'messenger_name' => array( |
||
39 | * 'settings' => array( |
||
40 | * '{messenger_name}-message_types' => array( |
||
41 | * 'message_type_name' => array() //variable array of settings corresponding to message type. |
||
42 | * ) |
||
43 | * ) |
||
44 | * ) |
||
45 | * ) |
||
46 | * @type array |
||
47 | */ |
||
48 | protected $_active_message_types = array(); |
||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | /** |
||
55 | * @type EE_message_type[] |
||
56 | */ |
||
57 | protected $_installed_message_types = array(); |
||
58 | |||
59 | |||
60 | |||
61 | |||
62 | |||
63 | /** |
||
64 | * When setting up values this is a temporary holder of the current EE_messenger object. |
||
65 | * |
||
66 | *@type EE_Messenger |
||
67 | */ |
||
68 | protected $_messenger; |
||
69 | |||
70 | |||
71 | |||
72 | |||
73 | |||
74 | /** |
||
75 | * When setting up values this is a temporary holder of the current EE_message_type object. |
||
76 | * @type EE_message_type |
||
77 | */ |
||
78 | protected $_message_type; |
||
79 | |||
80 | |||
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * An array of unique message type contexts across all active message types. |
||
86 | * |
||
87 | * The array will be indexed by either 'slugs' or 'all'. |
||
88 | * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug. |
||
89 | * array( |
||
90 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
91 | * ); |
||
92 | * |
||
93 | * The all index returns an array in this format: |
||
94 | * array( |
||
95 | * 'message_type_name' => array( |
||
96 | * 'context_slug' => array( |
||
97 | * 'label' => 'localized label for context', |
||
98 | * 'description' => 'localized description for context' |
||
99 | * ) |
||
100 | * ) |
||
101 | * ); |
||
102 | * @type array |
||
103 | */ |
||
104 | protected $_contexts = array(); |
||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * holds the EEM_message_templates model for interacting with the database and retrieving active templates for the messenger |
||
112 | * @var object |
||
113 | */ |
||
114 | //private $_EEM_data; |
||
115 | |||
116 | |||
117 | |||
118 | /** |
||
119 | * EE_Messages constructor. |
||
120 | */ |
||
121 | function __construct() { |
||
131 | |||
132 | |||
133 | |||
134 | |||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * get active messengers from db and instantiate them. |
||
140 | */ |
||
141 | private function _set_active_messengers_and_message_types() { |
||
168 | |||
169 | /** |
||
170 | * Ensures that the specified messenger is currently active. |
||
171 | * If not, activates it and its default message types. |
||
172 | * |
||
173 | * >>>>>>>>>>>> 1 usage in \EE_Payment_Method_Manager::activate_a_payment_method_of_type() |
||
174 | * >>>>>>>>>>>> 2 usages in \EE_messages_Test::test_ensure_messenger_is_active() |
||
175 | * |
||
176 | * @param string $messenger_name |
||
177 | * @return boolean TRUE if it was PREVIOUSLY active, and FALSE if it was previously inactive |
||
178 | */ |
||
179 | public function ensure_messenger_is_active( $messenger_name ){ |
||
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
||
194 | * This ALSO ensures that the given messenger is active as well!. |
||
195 | * |
||
196 | * >>>>>>>>>>>> 1 usage in \EE_Payment_Method_Manager::activate_a_payment_method_of_type() |
||
197 | * >>>>>>>>>>>> 1 usage in \EE_Register_Message_Type::set_defaults() |
||
198 | * |
||
199 | * @param string $message_type message type name |
||
200 | * @param $messenger |
||
201 | * @return bool true if it got activated (or was active) and false if not. |
||
202 | * @throws \EE_Error |
||
203 | */ |
||
204 | public function ensure_message_type_is_active( $message_type, $messenger ) { |
||
227 | |||
228 | /** |
||
229 | * Activates the specified messenger |
||
230 | * |
||
231 | * >>>>>>>>>>>> 2 usages found in this file: ensure_message_type_is_active() and ensure_messenger_is_active() |
||
232 | * |
||
233 | * @param string $messenger_name |
||
234 | * @param array $mts_to_activate (optional) An array of message types to activate with this messenger. If |
||
235 | * included we do NOT setup the default message types (assuming |
||
236 | * they are already setup.) |
||
237 | * @return boolean an array of generated templates or false if nothing generated/activated. |
||
238 | */ |
||
239 | public function activate_messenger( $messenger_name, $mts_to_activate = array() ){ |
||
301 | |||
302 | |||
303 | |||
304 | /** |
||
305 | * load the active files needed (key word... NEEDED) |
||
306 | * |
||
307 | * >>>>>>>>> 1 usage in \EE_Messages::_set_active_messengers_and_message_types() |
||
308 | * |
||
309 | * @param string $kind indicates what kind of files we are loading. |
||
310 | * @param array $actives indicates what active types of the $kind are actually to be loaded. |
||
311 | * @return array|void |
||
312 | */ |
||
313 | private function _load_files($kind, $actives) { |
||
348 | |||
349 | |||
350 | |||
351 | |||
352 | /** |
||
353 | * unsets the active if we can't find the file (fail-safe) |
||
354 | * |
||
355 | * @access private |
||
356 | * @param string $active_name name of messenger or message type |
||
357 | * @param string $kind messenger or message_type? |
||
358 | * @return void |
||
359 | */ |
||
360 | private function _unset_active( $active_name, $kind ) { |
||
384 | |||
385 | |||
386 | |||
387 | |||
388 | /** |
||
389 | * Used to verify if a message can be sent for the given messenger and message type and that it is a generating messenger (used for generating message templates). |
||
390 | * |
||
391 | * >>>>>>>>>>>> NO usages found |
||
392 | * |
||
393 | * @param EE_Messenger $messenger messenger used in trigger |
||
394 | * @param EE_message_type $message_type message type used in trigger |
||
395 | * |
||
396 | * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
||
397 | */ |
||
398 | public function is_generating_messenger_and_active( EE_Messenger $messenger, EE_message_type $message_type ) { |
||
410 | |||
411 | |||
412 | /** |
||
413 | * This returns the corresponding EE_Messenger object for the given string if it is active. |
||
414 | * |
||
415 | * >>>>>>>>>>>> 1 usage in \EE_Message_Generated_From_Token::get_EE_Message() |
||
416 | * >>>>>>>>>>>> 1 usage in \EE_Message_To_Generate_From_Request::__construct() |
||
417 | * >>>>>>>>>>>> 1 usage in \EE_Messages::validate_for_use() |
||
418 | * >>>>>>>>>>>> 2 usages in \EE_Messages_Queue::execute() |
||
419 | * |
||
420 | * @param string $messenger |
||
421 | * @return EE_Messenger | null |
||
422 | */ |
||
423 | public function get_messenger_if_active( $messenger ) { |
||
426 | |||
427 | |||
428 | /** |
||
429 | * This validates whether the given EE_Message object can be used for either sending or generation. |
||
430 | * This is done by grabbing the messenger and message type on the EE_Message and verifying that both are installed |
||
431 | * and active. |
||
432 | * |
||
433 | * >>>>>>>>>>>> 1 usage in \EE_Message_To_Generate::_set_valid() |
||
434 | * >>>>>>>>>>>> 1 usage in \EE_Messages_Generator::_validate_messenger_and_message_type() |
||
435 | * |
||
436 | * @param EE_Message $message |
||
437 | * |
||
438 | * @return array An array with 'messenger' and 'message_type' as the index and the corresponding valid object if |
||
439 | * available. |
||
440 | * Eg. Valid Messenger and Message Type: |
||
441 | * array( |
||
442 | * 'messenger' => new EE_Email_Messenger(), |
||
443 | * 'message_type' => new EE_Registration_Approved_message_type() |
||
444 | * ) |
||
445 | * Valid Messenger and Invalid Message Type: |
||
446 | * array( |
||
447 | * 'messenger' => new EE_Email_Messenger(), |
||
448 | * 'message_type' => null |
||
449 | * ) |
||
450 | */ |
||
451 | public function validate_for_use( EE_Message $message ) { |
||
456 | |||
457 | |||
458 | |||
459 | /** |
||
460 | * Delegates message sending to messengers |
||
461 | * |
||
462 | * >>>>>>>>>>>> 1 usage in \EED_Ticketing::_generate_tickets() |
||
463 | * >>>>>>>>>>>> 1 usage in \EED_Ticketing::maybe_ticket_notice() |
||
464 | * >>>>>>>>>>>> 1 usage in \EED_Ticketing::process_resend_ticket_notice() |
||
465 | * |
||
466 | * @deprecated 4.9.0 |
||
467 | * @param string $type What type of message are we sending (corresponds to message types) |
||
468 | * @param mixed $vars Data being sent for parsing in the message |
||
469 | * @param string $sending_messenger if included then we ONLY use the specified messenger for delivery. Otherwise we cycle through all active messengers. |
||
470 | * @param string $generating_messenger if included then this messenger is used for generating the message templates (but not for sending). |
||
471 | * @param string $context If included then only a message type for a specific context will be generated. |
||
472 | * @param bool $send Default TRUE. If false, then this will just return the generated EE_Messages objects which might be used by the trigger to setup a batch message (typically html messenger uses it). |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function send_message( $type, $vars, $sending_messenger = '', $generating_messenger='', $context='', $send = TRUE ) { |
||
583 | |||
584 | |||
585 | |||
586 | |||
587 | /** |
||
588 | * Use to generate and return a message preview! |
||
589 | * |
||
590 | * >>>>>>>>>>>> NO usages found |
||
591 | * |
||
592 | * @deprecated 4.9.0 |
||
593 | * @param string $type This should correspond with a valid message type |
||
594 | * @param string $context This should correspond with a valid context for the message type |
||
595 | * @param string $messenger This should correspond with a valid messenger. |
||
596 | * @param bool $send true we will do a test send using the messenger delivery, false we just do a regular preview |
||
597 | * @return string The body of the message. |
||
598 | */ |
||
599 | public function preview_message( $type, $context, $messenger, $send = FALSE ) { |
||
602 | |||
603 | |||
604 | /** |
||
605 | * This is a method that allows for sending a message using a messenger matching the string given and the provided EE_Message stdClass objects. |
||
606 | * |
||
607 | * >>>>>>>>>>>> 1 usage in \EED_Ticketing::_generate_tickets() |
||
608 | * |
||
609 | * @since 4.5.0 |
||
610 | * @deprecated 4.9.0 Moved to EED_Messages Module |
||
611 | * @param string $messenger a string matching a valid active messenger in the system |
||
612 | * @param string $message_type Although it seems contrary to the name of the method, a message type name is still required to send along the message type to the messenger because this is used for determining what specific variations might be loaded for the generated message. |
||
613 | * @param stdClass $message a stdClass object in the format expected by the messenger. |
||
614 | * |
||
615 | * @return bool success or fail. |
||
616 | */ |
||
617 | public function send_message_with_messenger_only( $messenger, $message_type, $message ) { |
||
644 | |||
645 | |||
646 | |||
647 | |||
648 | |||
649 | /** |
||
650 | * _validate_setup |
||
651 | * |
||
652 | * >>>>>>>>>>>> 1 usage in \EE_Messages::create_new_templates() |
||
653 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_fields() |
||
654 | * |
||
655 | * @param string $messenger EE_Messenger |
||
656 | * @param string $message_type EE_message_type |
||
657 | * @param bool $is_global whether this is a global template or not. |
||
658 | * @throws EE_Error |
||
659 | * @return bool(true)|WP_Error |
||
660 | */ |
||
661 | private function _validate_setup($messenger, $message_type, $is_global = FALSE) { |
||
716 | |||
717 | |||
718 | |||
719 | /** |
||
720 | * This is a wrapper for the protected _create_new_templates function |
||
721 | * |
||
722 | * >>>>>>>>>>>> 1 usage in \EEH_MSG_Template::generate_new_templates() |
||
723 | * |
||
724 | * @param $messenger |
||
725 | * @param string $message_type message type that the templates are being created for |
||
726 | * @param int $GRP_ID |
||
727 | * @param bool $is_global |
||
728 | * @return array|object if creation is successful then we return an array of info, otherwise an error_object is returned. |
||
729 | * @throws \EE_Error |
||
730 | */ |
||
731 | public function create_new_templates( $messenger, $message_type, $GRP_ID = 0, $is_global = false ) { |
||
748 | |||
749 | |||
750 | |||
751 | /** |
||
752 | * @param $GRP_ID |
||
753 | * @param $is_global |
||
754 | * @return array|mixed |
||
755 | */ |
||
756 | protected function _create_new_templates( $GRP_ID, $is_global) { |
||
776 | |||
777 | |||
778 | |||
779 | /** |
||
780 | * This creates a custom template using the incoming GRP_ID |
||
781 | * |
||
782 | * @param int $GRP_ID GRP_ID for the template_group being used as the base |
||
783 | * @return array $success This will be an array in the format: |
||
784 | * array( |
||
785 | * 'GRP_ID' => $new_grp_id, |
||
786 | * 'MTP_context' => $first_context_in_created_template |
||
787 | * ) |
||
788 | * @access private |
||
789 | */ |
||
790 | private function _create_custom_template_group( $GRP_ID ) { |
||
838 | |||
839 | |||
840 | |||
841 | |||
842 | /** |
||
843 | * get_fields |
||
844 | * This takes a given messenger and message type and returns all the template fields indexed by context (and with field type). |
||
845 | * |
||
846 | * >>>>>>>>>>>> 1 usage in \Messages_Admin_Page::_edit_message_template() |
||
847 | * |
||
848 | * @param string $messenger EE_Messenger |
||
849 | * @param string $message_type EE_message_type |
||
850 | * @return array|WP_Error template fields indexed by context. |
||
851 | */ |
||
852 | public function get_fields($messenger, $message_type) { |
||
872 | |||
873 | /** |
||
874 | * gets an array of installed messengers and message types objects. |
||
875 | * |
||
876 | * >>>>>>>>>>>> 1 usage in \EEH_MSG_Template::get_installed_message_objects() |
||
877 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_installed_message_types() |
||
878 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_installed_messengers() |
||
879 | * |
||
880 | * @access public |
||
881 | * @param string $type we can indicate just returning installed message types |
||
882 | * or messengers (or both) via this parameter. |
||
883 | * @param bool $skip_cache if true then we skip the cache and retrieve via files. |
||
884 | * @return array multidimensional array of messenger and message_type objects |
||
885 | * (messengers index, and message_type index); |
||
886 | */ |
||
887 | public function get_installed( $type = 'all', $skip_cache = false ) { |
||
916 | |||
917 | |||
918 | /** |
||
919 | * _get_installed |
||
920 | * takes an array of filenames and returns an array of objects instantiated from the class name found in the filename. |
||
921 | * @param array $filenames and array of filenames |
||
922 | * @return array array of objects |
||
923 | */ |
||
924 | private function _get_installed($filenames) { |
||
941 | |||
942 | public function get_active_messengers() { |
||
945 | |||
946 | |||
947 | /** |
||
948 | * This does NOT return the _active_message_types property but |
||
949 | * simply returns an array of active message types from that property. |
||
950 | * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
||
951 | * |
||
952 | * >>>>>>>>>>>> 1 usage in \Registrations_Admin_Page::_set_list_table_views_default() |
||
953 | * >>>>>>>>>>>> 1 usage in \EEH_MSG_Template::is_mt_active() |
||
954 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_active_message_type_objects() |
||
955 | * >>>>>>>>>>>> 1 usage in \EED_Ticketing::process_resend_ticket_notice() |
||
956 | * |
||
957 | * |
||
958 | * @return array array of message_type references (string) |
||
959 | */ |
||
960 | public function get_active_message_types() { |
||
971 | |||
972 | |||
973 | /** |
||
974 | * Same as get_active_message_types() except this returns actual EE_message_type objects |
||
975 | * |
||
976 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_all_contexts() |
||
977 | * |
||
978 | * @since 4.9.0 |
||
979 | * @return EE_message_type[] |
||
980 | */ |
||
981 | public function get_active_message_type_objects() { |
||
992 | |||
993 | |||
994 | |||
995 | |||
996 | /** |
||
997 | * This checks the _active_message_types property for any active message types that are present for the given messenger and returns them. |
||
998 | * |
||
999 | * >>>>>>>>>>>> 1 usage in \espresso_events_Messages_Hooks_Extend::messages_metabox() |
||
1000 | * |
||
1001 | * @since 4.5.0 |
||
1002 | * |
||
1003 | * @param string $messenger The messenger being checked |
||
1004 | * |
||
1005 | * @return EE_message_type[] (or empty array if none present) |
||
1006 | */ |
||
1007 | public function get_active_message_types_per_messenger( $messenger ) { |
||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * This returns the EE_message_type from the active message types array ( if present ); |
||
1027 | * |
||
1028 | * >>>>>>>>>>>> 1 usage in \EE_Messages::validate_for_use() |
||
1029 | * >>>>>>>>>>>> 1 usage in \EE_Messages_Queue::execute() |
||
1030 | * |
||
1031 | * @param string $messenger The string should correspond to the messenger (message types are |
||
1032 | * assigned to a messenger in the messages settings) |
||
1033 | * @param string $message_type The string should correspond to a message type. |
||
1034 | * |
||
1035 | * @return EE_Message_Type|null |
||
1036 | */ |
||
1037 | public function get_active_message_type( $messenger, $message_type ) { |
||
1044 | |||
1045 | |||
1046 | |||
1047 | /** |
||
1048 | * |
||
1049 | * >>>>>>>>>>>> 1 usage in \Messages_Admin_Page::_get_installed_message_objects() |
||
1050 | * >>>>>>>>>>>> 1 usage in \EEH_MSG_Template::message_type_obj() |
||
1051 | * >>>>>>>>>>>> 1 usage in \EE_Message_Template_Defaults::_init() |
||
1052 | * >>>>>>>>>>>> 1 usage in \EE_Messages::_validate_setup() |
||
1053 | * >>>>>>>>>>>> 1 usage in \EE_Messages::activate_messenger() |
||
1054 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_active_message_type() |
||
1055 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_active_message_type_objects() |
||
1056 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_active_message_types_per_messenger() |
||
1057 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_installed() |
||
1058 | * |
||
1059 | * @return array|\EE_message_type[] |
||
1060 | */ |
||
1061 | public function get_installed_message_types() { |
||
1067 | |||
1068 | |||
1069 | |||
1070 | /** |
||
1071 | * >>>>>>>>>>>> 1 usage in \Messages_Admin_Page::_get_installed_message_objects() |
||
1072 | * >>>>>>>>>>>> 1 usage in \EEH_MSG_Template::messenger_obj() |
||
1073 | * >>>>>>>>>>>> 1 usage in \EE_Messages::activate_messenger() |
||
1074 | * >>>>>>>>>>>> 1 usage in \EE_Messages::ensure_message_type_is_active() |
||
1075 | * >>>>>>>>>>>> 1 usage in \EE_Messages::get_installed() |
||
1076 | * |
||
1077 | * @return array |
||
1078 | */ |
||
1079 | public function get_installed_messengers() { |
||
1085 | |||
1086 | |||
1087 | |||
1088 | /** |
||
1089 | * This returns all the contexts that are registered by all message types. |
||
1090 | * |
||
1091 | * If $slugs_only is true, then just an array indexed by unique context slugs with the latest label representation for that slug. |
||
1092 | * array( |
||
1093 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
1094 | * ); |
||
1095 | * |
||
1096 | * If $slugs_only is false, then the format is: |
||
1097 | * array( |
||
1098 | * 'message_type_name' => array( |
||
1099 | * 'context_slug' => array( |
||
1100 | * 'label' => 'localized label for context', |
||
1101 | * 'description' => 'localized description for context' |
||
1102 | * ) |
||
1103 | * ) |
||
1104 | * ); |
||
1105 | * |
||
1106 | * Keep in mind that although different message types may share the same context slugs, it is possible that the context |
||
1107 | * is described differently by the message type. |
||
1108 | * |
||
1109 | * >>>>>>>>>>>> 1 usage in \EE_Message_List_Table::_get_table_filters() |
||
1110 | * >>>>>>>>>>>> 1 usage in \EE_Message::context_label() |
||
1111 | * >>>>>>>>>>>> 1 usage in \EE_messages_Test::test_get_all_contexts() |
||
1112 | * |
||
1113 | * @since 4.9.0 |
||
1114 | * @param bool $slugs_only Whether to return an array of just slugs and labels (true) or all contexts indexed by message type. |
||
1115 | * @return array |
||
1116 | */ |
||
1117 | public function get_all_contexts( $slugs_only = true ) { |
||
1142 | |||
1143 | |||
1144 | |||
1145 | |||
1146 | /** |
||
1147 | * Validates the given string as a reference for an existing, accessible data handler and returns the class name |
||
1148 | * For the handler the reference matches. |
||
1149 | * |
||
1150 | * >>>>>>>>>>>> No usages found |
||
1151 | * |
||
1152 | * @param string $data_handler_reference |
||
1153 | * @return string |
||
1154 | */ |
||
1155 | public function verify_and_retrieve_class_name_for_data_handler_reference( $data_handler_reference ) { |
||
1160 | |||
1161 | |||
1162 | |||
1163 | } |
||
1164 | //end EE_Messages class |
||
1166 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.