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 boolean $_initialized |
||
24 | */ |
||
25 | protected $_initialized = false; |
||
26 | |||
27 | /** |
||
28 | * @type EE_Messenger_Collection $_messenger_collection_loader |
||
29 | */ |
||
30 | protected $_messenger_collection_loader; |
||
31 | |||
32 | /** |
||
33 | * @type EE_Message_Type_Collection $_message_type_collection_loader |
||
34 | */ |
||
35 | protected $_message_type_collection_loader; |
||
36 | |||
37 | /** |
||
38 | * @type EEM_Message_Template_Group $_message_template_group_model |
||
39 | */ |
||
40 | protected $_message_template_group_model; |
||
41 | |||
42 | /** |
||
43 | * @type EE_messenger[] |
||
44 | */ |
||
45 | protected $_installed_messengers = array(); |
||
46 | |||
47 | /** |
||
48 | * @type EE_message_type[] |
||
49 | */ |
||
50 | protected $_installed_message_types = array(); |
||
51 | |||
52 | /** |
||
53 | * Array of active messengers. |
||
54 | * Format is this: |
||
55 | * array( |
||
56 | * 'messenger_name' => EE_messenger |
||
57 | * ) |
||
58 | * |
||
59 | * @type EE_messenger[] |
||
60 | */ |
||
61 | protected $_active_messengers = array(); |
||
62 | |||
63 | /** |
||
64 | * Formatted array of active message types grouped per messenger. |
||
65 | * Format is this: |
||
66 | * array( |
||
67 | * 'messenger_name' => array( |
||
68 | * 'settings' => array( |
||
69 | * '{messenger_name}-message_types' => array( |
||
70 | * 'message_type_name' => array() //variable array of settings corresponding to message type. |
||
71 | * ) |
||
72 | * ) |
||
73 | * ) |
||
74 | * ) |
||
75 | * |
||
76 | * @type array |
||
77 | */ |
||
78 | protected $_active_message_types = array(); |
||
79 | |||
80 | |||
81 | /** |
||
82 | * This holds the array of messengers and their corresponding message types that have |
||
83 | * been activated on a site at some point. This is an important record that helps the messages system |
||
84 | * not accidentally reactivate something that was intentionally deactivated by a user. |
||
85 | * @type array |
||
86 | */ |
||
87 | protected $_has_activated_messengers_and_message_types = array(); |
||
88 | |||
89 | /** |
||
90 | * An array of unique message type contexts across all active message types. |
||
91 | * |
||
92 | * The array will be indexed by either 'slugs' or 'all'. |
||
93 | * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug. |
||
94 | * array( |
||
95 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
96 | * ); |
||
97 | * |
||
98 | * The all index returns an array in this format: |
||
99 | * array( |
||
100 | * 'message_type_name' => array( |
||
101 | * 'context_slug' => array( |
||
102 | * 'label' => 'localized label for context', |
||
103 | * 'description' => 'localized description for context' |
||
104 | * ) |
||
105 | * ) |
||
106 | * ); |
||
107 | * |
||
108 | * @type array |
||
109 | */ |
||
110 | protected $_contexts = array(); |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * EE_Message_Resource_Manager constructor. |
||
116 | * |
||
117 | * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
||
118 | * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
||
119 | * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
||
120 | */ |
||
121 | function __construct( |
||
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * @return EE_Messenger_Collection |
||
135 | */ |
||
136 | protected function _initialize_collections() { |
||
146 | |||
147 | |||
148 | |||
149 | /** |
||
150 | * @return EE_Messenger_Collection |
||
151 | */ |
||
152 | public function messenger_collection() { |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * @return EE_messenger[] |
||
161 | */ |
||
162 | public function active_messengers() { |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * @param string $messenger_name |
||
171 | * @return \EE_messenger |
||
172 | */ |
||
173 | public function get_messenger( $messenger_name ) { |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * This returns the corresponding EE_messenger object for the given string if it is active. |
||
181 | * |
||
182 | * @param string $messenger |
||
183 | * @return EE_messenger | null |
||
184 | */ |
||
185 | public function get_active_messenger( $messenger ) { |
||
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * @return \EE_messenger[] |
||
194 | */ |
||
195 | public function installed_messengers() { |
||
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $messenger_name |
||
211 | * @return \EE_messenger |
||
212 | * @throws \EE_Error |
||
213 | */ |
||
214 | View Code Duplication | public function valid_messenger( $messenger_name ) { |
|
226 | |||
227 | |||
228 | |||
229 | /** |
||
230 | * @return EE_Message_Type_Collection |
||
231 | */ |
||
232 | public function message_type_collection() { |
||
236 | |||
237 | |||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public function active_message_types() { |
||
246 | |||
247 | |||
248 | |||
249 | /** |
||
250 | * @param string $message_type_name |
||
251 | * @return \EE_message_type |
||
252 | */ |
||
253 | public function get_message_type( $message_type_name ) { |
||
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * This returns the EE_message_type from the active message types array ( if present ); |
||
261 | * |
||
262 | * @param string $messenger_name |
||
263 | * @param string $message_type_name |
||
264 | * @return \EE_message_type|null |
||
265 | */ |
||
266 | public function get_active_message_type_for_messenger( $messenger_name, $message_type_name ) { |
||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * Returns whether the given message type is active for the given messenger. |
||
276 | * |
||
277 | * @param string $messenger_name |
||
278 | * @param string $message_type_name |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | public function is_message_type_active_for_messenger( $messenger_name, $message_type_name ) { |
||
286 | |||
287 | |||
288 | |||
289 | /** |
||
290 | * Returns whether the given messenger is active. |
||
291 | * |
||
292 | * @param string $messenger_name the name of the messenger to check if active. |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function is_messenger_active( $messenger_name ) { |
||
299 | |||
300 | |||
301 | /** |
||
302 | * This returns any settings that might be on a message type for a messenger |
||
303 | * |
||
304 | * @param string $messenger_name The slug of the messenger |
||
305 | * @param string $message_type_name The slug of the message type getting the settings for. |
||
306 | * @return array |
||
307 | */ |
||
308 | public function get_message_type_settings_for_messenger( $messenger_name, $message_type_name ) { |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Returns whether the given messenger name has active message types on it. |
||
322 | * Infers whether the messenger is active or not as well. |
||
323 | * |
||
324 | * @param string $messenger_name |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function messenger_has_active_message_types( $messenger_name ) { |
||
333 | |||
334 | |||
335 | |||
336 | /** |
||
337 | * This checks the _active_message_types property for any active message types |
||
338 | * that are present for the given messenger and returns them. |
||
339 | * |
||
340 | * @since 4.9.0 |
||
341 | * @param string $messenger_name The messenger being checked |
||
342 | * @return EE_message_type[] (or empty array if none present) |
||
343 | */ |
||
344 | public function get_active_message_types_for_messenger( $messenger_name ) { |
||
357 | |||
358 | |||
359 | |||
360 | /** |
||
361 | * This does NOT return the _active_message_types property but |
||
362 | * simply returns an array of active message type names from that property. |
||
363 | * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
||
364 | * |
||
365 | * @return array message_type references (string) |
||
366 | */ |
||
367 | public function list_of_active_message_types() { |
||
382 | |||
383 | |||
384 | |||
385 | /** |
||
386 | * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
||
387 | * |
||
388 | * @since 4.9.0 |
||
389 | * @return \EE_message_type[] |
||
390 | */ |
||
391 | public function get_active_message_type_objects() { |
||
402 | |||
403 | |||
404 | |||
405 | /** |
||
406 | * @return \EE_message_type[] |
||
407 | */ |
||
408 | public function installed_message_types() { |
||
418 | |||
419 | |||
420 | /** |
||
421 | * @param string $message_type_name |
||
422 | * @return \EE_message_type |
||
423 | * @throws \EE_Error |
||
424 | */ |
||
425 | View Code Duplication | public function valid_message_type( $message_type_name ) { |
|
437 | |||
438 | |||
439 | |||
440 | /** |
||
441 | * valid_message_type_for_messenger |
||
442 | * |
||
443 | * @param EE_messenger $messenger |
||
444 | * @param string $message_type_name |
||
445 | * @return boolean |
||
446 | * @throws \EE_Error |
||
447 | */ |
||
448 | public function valid_message_type_for_messenger( EE_messenger $messenger, $message_type_name ) { |
||
465 | |||
466 | |||
467 | /** |
||
468 | * Used to return active messengers array stored in the wp options table. |
||
469 | * If no value is present in the option then an empty array is returned. |
||
470 | * |
||
471 | * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
||
472 | * property and pull directly from the db. Otherwise whatever is currently on the |
||
473 | * $_active_message_types property is pulled. |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | public function get_active_messengers_option( $reset = false) { |
||
483 | |||
484 | |||
485 | |||
486 | /** |
||
487 | * Used to update the active messengers array stored in the wp options table. |
||
488 | * |
||
489 | * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
||
490 | * representing this data is used. |
||
491 | * @return bool FALSE if not updated, TRUE if updated. |
||
492 | */ |
||
493 | public function update_active_messengers_option( $active_messenger_settings = array() ) { |
||
499 | |||
500 | |||
501 | |||
502 | /** |
||
503 | * Used to return active messengers array stored in the wp options table. |
||
504 | * If no value is present in the option then an empty array is returned. |
||
505 | * |
||
506 | * The value is cached on the $_has_activated_messengers_and_message_types property for future calls. |
||
507 | * |
||
508 | * @param bool $reset Used to indicate that any cached value should be ignored. |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | public function get_has_activated_messengers_option( $reset = false ) { |
||
518 | |||
519 | |||
520 | |||
521 | /** |
||
522 | * Used to update the active messengers array stored in the wp options table. |
||
523 | * |
||
524 | * @param array $has_activated_messengers Incoming data to save. If empty, then the internal cached property |
||
525 | * representing this data is used. |
||
526 | * |
||
527 | * @return bool FALSE if not updated, TRUE if updated. |
||
528 | */ |
||
529 | public function update_has_activated_messengers_option( $has_activated_messengers = array() ) { |
||
535 | |||
536 | |||
537 | |||
538 | /** |
||
539 | * wrapper for _set_active_messengers_and_message_types() |
||
540 | */ |
||
541 | public function reset_active_messengers_and_message_types() { |
||
544 | |||
545 | |||
546 | |||
547 | /** |
||
548 | * Generate list of active messengers and message types from collection. |
||
549 | * This sets up the active messengers from what is present in the database. |
||
550 | */ |
||
551 | protected function _set_active_messengers_and_message_types() { |
||
560 | |||
561 | |||
562 | |||
563 | |||
564 | |||
565 | |||
566 | /** |
||
567 | * Ensures that the specified messenger is currently active. |
||
568 | * If not, activates it and its default message types. |
||
569 | * |
||
570 | * @param string $messenger_name |
||
571 | * @param bool $update_option Whether to update the option in the db or not. |
||
572 | * @return boolean true if either already active or successfully activated. |
||
573 | */ |
||
574 | public function ensure_messenger_is_active( $messenger_name, $update_option = true ) { |
||
590 | |||
591 | |||
592 | /** |
||
593 | * This ensures the given array of messenger names is active in the system. |
||
594 | * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
||
595 | * it will automatically activate the default message types for the messenger if its not active. |
||
596 | * |
||
597 | * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array (default) |
||
598 | * then will attempt to set the active messengers from the activated_messengers option |
||
599 | * (stored in $_active_message_types property). |
||
600 | * @param bool $update_option Whether to update the related active messengers option. |
||
601 | * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is set to true |
||
602 | * and a messenger is indicated as active, but is NOT installed, then it will automatically be |
||
603 | * deactivated. |
||
604 | */ |
||
605 | public function ensure_messengers_are_active( $messenger_names = array(), $update_option = true, $verify = false ) { |
||
629 | |||
630 | |||
631 | |||
632 | /** |
||
633 | * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
||
634 | * This ALSO ensures that the given messenger is active as well! |
||
635 | * |
||
636 | * @param string $message_type_name message type name. |
||
637 | * @param $messenger_name |
||
638 | * @param bool $update_option Whether to update the option in the db or not. |
||
639 | * @return bool Returns true if already is active or if was activated successfully. |
||
640 | * @throws \EE_Error |
||
641 | */ |
||
642 | public function ensure_message_type_is_active( $message_type_name, $messenger_name, $update_option = true ) { |
||
665 | |||
666 | |||
667 | |||
668 | |||
669 | /** |
||
670 | * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
||
671 | * messenger are active in one go. |
||
672 | * |
||
673 | * @param array $message_type_names Array of message type names to ensure are active. |
||
674 | * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
||
675 | * @param bool $update_option Whether to persist the activation to the database or not (default true). |
||
676 | */ |
||
677 | public function ensure_message_types_are_active( $message_type_names, $messenger_name, $update_option = true ) { |
||
689 | |||
690 | |||
691 | |||
692 | /** |
||
693 | * Activates the specified messenger. |
||
694 | * |
||
695 | * @param string $messenger_name |
||
696 | * @param array $message_type_names An array of message type names to activate with this messenger. |
||
697 | * If included we do NOT setup the default message types |
||
698 | * (assuming they are already setup.) |
||
699 | * @param bool $update_active_messengers_option |
||
700 | * |
||
701 | * @return array of generated templates |
||
702 | * @throws \EE_Error |
||
703 | */ |
||
704 | public function activate_messenger( |
||
733 | |||
734 | |||
735 | |||
736 | /** |
||
737 | * Activates given message types for the given EE_messenger object. |
||
738 | * |
||
739 | * Note: (very important) This method does not persist the activation to the database. |
||
740 | * See code implementing this method in this class for examples of how to persist. |
||
741 | * |
||
742 | * @param \EE_messenger $messenger |
||
743 | * @param array $message_type_names |
||
744 | * |
||
745 | * @return array |
||
746 | */ |
||
747 | protected function _activate_message_types( EE_messenger $messenger, $message_type_names = array() ) { |
||
779 | |||
780 | |||
781 | |||
782 | /** |
||
783 | * add_settings_for_message_type |
||
784 | * |
||
785 | * NOTE This does NOT automatically persist any settings to the db. Client code should call $this->update_active_messengers_option |
||
786 | * to persist. |
||
787 | * |
||
788 | * @param string $messenger_name The name of the messenger adding the settings for |
||
789 | * @param string $message_type_name The name of the message type adding the settings for |
||
790 | * @param array $new_settings Any new settings being set for the message type and messenger |
||
791 | */ |
||
792 | public function add_settings_for_message_type( $messenger_name, $message_type_name, $new_settings = array() ) { |
||
811 | |||
812 | |||
813 | |||
814 | /** |
||
815 | * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
||
816 | * and message type. |
||
817 | * |
||
818 | * @access protected |
||
819 | * @param \EE_messenger $messenger |
||
820 | * @param string $message_type_name |
||
821 | */ |
||
822 | protected function _set_messenger_has_activated_message_type( EE_messenger $messenger, $message_type_name ) { |
||
832 | |||
833 | |||
834 | |||
835 | /** |
||
836 | * add_settings_for_messenger |
||
837 | * |
||
838 | * NOTE This does NOT automatically persist any settings to the db. Client code should call $this->update_active_messengers_option |
||
839 | * to persist. |
||
840 | * |
||
841 | * @param string $messenger_name The name of the messenger the settings is being added for. |
||
842 | * @param array $new_settings An array of settings to update the existing settings. |
||
843 | */ |
||
844 | public function add_settings_for_messenger( $messenger_name, $new_settings = array() ) { |
||
863 | |||
864 | |||
865 | |||
866 | /** |
||
867 | * deactivate_messenger |
||
868 | * |
||
869 | * @param string|EE_messenger $messenger_name name of messenger |
||
870 | * @return void |
||
871 | */ |
||
872 | public function deactivate_messenger( $messenger_name ) { |
||
881 | |||
882 | |||
883 | /** |
||
884 | * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
||
885 | * |
||
886 | * @param string $message_type_name name of message type being deactivated |
||
887 | */ |
||
888 | public function deactivate_message_type( $message_type_name ) { |
||
900 | |||
901 | |||
902 | |||
903 | |||
904 | |||
905 | /** |
||
906 | * Deactivates a message type for a specific messenger as opposed to all messengers. |
||
907 | * |
||
908 | * @param string $message_type_name Name of message type being deactivated. |
||
909 | * @param string $messenger_name Name of messenger the message type is being deactivated for. |
||
910 | */ |
||
911 | public function deactivate_message_type_for_messenger( $message_type_name, $messenger_name ) { |
||
918 | |||
919 | |||
920 | |||
921 | |||
922 | |||
923 | /** |
||
924 | * Used to verify if a message can be sent for the given messenger and message type |
||
925 | * and that it is a generating messenger (used for generating message templates). |
||
926 | * |
||
927 | * @param EE_messenger $messenger messenger used in trigger |
||
928 | * @param EE_message_type $message_type message type used in trigger |
||
929 | * |
||
930 | * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
||
931 | */ |
||
932 | public function is_generating_messenger_and_active( EE_messenger $messenger, EE_message_type $message_type ) { |
||
944 | |||
945 | |||
946 | |||
947 | /** |
||
948 | * This returns all the contexts that are registered by all message types. |
||
949 | * |
||
950 | * If $slugs_only is true, |
||
951 | * then just an array indexed by unique context slugs with the latest label representation for that slug. |
||
952 | * array( |
||
953 | * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
||
954 | * ); |
||
955 | * |
||
956 | * If $slugs_only is false, then the format is: |
||
957 | * array( |
||
958 | * 'message_type_name' => array( |
||
959 | * 'context_slug' => array( |
||
960 | * 'label' => 'localized label for context', |
||
961 | * 'description' => 'localized description for context' |
||
962 | * ) |
||
963 | * ) |
||
964 | * ); |
||
965 | * |
||
966 | * Keep in mind that although different message types may share the same context slugs, |
||
967 | * it is possible that the context is described differently by the message type. |
||
968 | * |
||
969 | * @since 4.9.0 |
||
970 | * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
||
971 | * or all contexts indexed by message type. |
||
972 | * @return array |
||
973 | */ |
||
974 | public function get_all_contexts( $slugs_only = true ) { |
||
994 | |||
995 | |||
996 | |||
997 | |||
998 | /** |
||
999 | * This checks the internal record of what message types are considered "active" and verifies that |
||
1000 | * there is an installed class definition for that message type. If the active message type does not have a corresponding |
||
1001 | * accessible message type class then it will be deactivated from all messengers it is active on and any related |
||
1002 | * message templates will be inactivated as well. |
||
1003 | * |
||
1004 | * @return bool true means all active message types are valid, false means at least one message type was deactivated. |
||
1005 | */ |
||
1006 | public function validate_active_message_types_are_installed() { |
||
1019 | |||
1020 | |||
1021 | |||
1022 | |||
1023 | /** |
||
1024 | * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
||
1025 | * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
||
1026 | * to attempt automatically reactivating message types that should be activated by default or not. |
||
1027 | * |
||
1028 | * @param $message_type_name |
||
1029 | * @param $messenger_name |
||
1030 | * @return bool |
||
1031 | */ |
||
1032 | public function has_message_type_been_activated_for_messenger( $message_type_name, $messenger_name ) { |
||
1037 | } |
||
1038 | // End of file EE_Message_Resource_Manager.lib.php |
||
1039 | // 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..