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 EEM_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 EEM_Message, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
||
12 | class EEM_Message extends EEM_Base implements EEI_Query_Filter |
||
13 | { |
||
14 | |||
15 | // private instance of the Message object |
||
16 | protected static $_instance = null; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * This priority indicates a message should be generated and sent ASAP |
||
21 | * |
||
22 | * @type int |
||
23 | */ |
||
24 | const priority_high = 10; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * This priority indicates a message should be generated ASAP and queued for sending. |
||
29 | * |
||
30 | * @type |
||
31 | */ |
||
32 | const priority_medium = 20; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * This priority indicates a message should be queued for generating. |
||
37 | * |
||
38 | * @type int |
||
39 | */ |
||
40 | const priority_low = 30; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * indicates this message was sent at the time modified |
||
45 | */ |
||
46 | const status_sent = 'MSN'; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * indicates this message is waiting to be sent |
||
51 | */ |
||
52 | const status_idle = 'MID'; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * indicates an attempt was a made to send this message |
||
57 | * at the scheduled time, but it failed at the time modified. This differs from MDO status in that it will ALWAYS |
||
58 | * appear to the end user. |
||
59 | */ |
||
60 | const status_failed = 'MFL'; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * indicates the message has been flagged for resending (at the time modified). |
||
65 | */ |
||
66 | const status_resend = 'MRS'; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * indicates the message has been flagged for generation but has not been generated yet. Messages always start as |
||
71 | * this status when added to the queue. |
||
72 | */ |
||
73 | const status_incomplete = 'MIC'; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Indicates everything was generated fine for the message, however, the messenger was unable to send. |
||
78 | * This status means that its possible to retry sending the message. |
||
79 | */ |
||
80 | const status_retry = 'MRT'; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * This is used for more informational messages that may not indicate anything is broken but still cannot be |
||
85 | * generated or sent correctly. An example of a message that would get flagged this way would be when a not |
||
86 | * approved message was queued for generation, but at time of generation, the attached registration(s) are |
||
87 | * approved. So the message queued for generation is no longer valid. Messages for this status will only persist |
||
88 | * in the db and be viewable in the message activity list table when the messages system is in debug mode. |
||
89 | * |
||
90 | * @see EEM_Message::debug() |
||
91 | */ |
||
92 | const status_debug_only = 'MDO'; |
||
93 | |||
94 | |||
95 | /** |
||
96 | * This status is given to messages it is processed by the messenger send method. |
||
97 | * Messages with this status should rarely be seen in the Message List table, but if they are, that's usually |
||
98 | * indicative of a PHP timeout or memory limit issue. |
||
99 | */ |
||
100 | const status_messenger_executing = 'MEX'; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Private constructor to prevent direct creation. |
||
105 | * |
||
106 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and |
||
107 | * any incoming timezone data that gets saved). Note this just sends the timezone info to |
||
108 | * the date time model field objects. Default is null (and will be assumed using the set |
||
109 | * timezone in the 'timezone_string' wp option) |
||
110 | * @return EEM_Message |
||
|
|||
111 | */ |
||
112 | protected function __construct($timezone = null) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return \EE_Message |
||
184 | */ |
||
185 | public function create_default_object() |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @param mixed $cols_n_values |
||
198 | * @return \EE_Message |
||
199 | */ |
||
200 | public function instantiate_class_from_array_or_object($cols_n_values) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Returns whether or not a message of that type was sent for a given attendee. |
||
213 | * |
||
214 | * @param EE_Attendee|int $attendee |
||
215 | * @param string $message_type the message type slug |
||
216 | * @return boolean |
||
217 | */ |
||
218 | View Code Duplication | public function message_sent_for_attendee($attendee, $message_type) |
|
229 | |||
230 | |||
231 | /** |
||
232 | * Returns whether or not a message of that type was sent for a given registration |
||
233 | * |
||
234 | * @param EE_Registration|int $registration |
||
235 | * @param string $message_type the message type slug |
||
236 | * @return boolean |
||
237 | */ |
||
238 | View Code Duplication | public function message_sent_for_registration($registration, $message_type) |
|
249 | |||
250 | |||
251 | /** |
||
252 | * This retrieves an EE_Message object from the db matching the given token string. |
||
253 | * |
||
254 | * @param string $token |
||
255 | * @return EE_Message |
||
256 | */ |
||
257 | public function get_one_by_token($token) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Returns stati that indicate the message HAS been sent |
||
269 | * |
||
270 | * @return array of strings for possible stati |
||
271 | */ |
||
272 | public function stati_indicating_sent() |
||
276 | |||
277 | |||
278 | /** |
||
279 | * Returns stati that indicate the message is waiting to be sent. |
||
280 | * |
||
281 | * @return array of strings for possible stati. |
||
282 | */ |
||
283 | public function stati_indicating_to_send() |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Returns stati that indicate the message has failed sending |
||
292 | * |
||
293 | * @return array array of strings for possible stati. |
||
294 | */ |
||
295 | public function stati_indicating_failed_sending() |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Returns filterable array of all EEM_Message statuses. |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function all_statuses() |
||
331 | |||
332 | /** |
||
333 | * Detects any specific query variables in the request and uses those to setup appropriate |
||
334 | * filter for any queries. |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | public function filter_by_query_params() |
||
367 | |||
368 | |||
369 | /** |
||
370 | * @return string |
||
371 | */ |
||
372 | public function get_pretty_label_for_results() |
||
437 | |||
438 | |||
439 | /** |
||
440 | * This returns the array of expected variables for the EEI_Query_Filter methods being implemented |
||
441 | * The array is in the format: |
||
442 | * array( |
||
443 | * {$field_name} => {$model_name} |
||
444 | * ); |
||
445 | * |
||
446 | * @since 4.9.0 |
||
447 | * @return array |
||
448 | */ |
||
449 | protected function _expected_vars_for_query_inject() |
||
459 | |||
460 | |||
461 | /** |
||
462 | * This returns whether EEM_Message is in debug mode or not. |
||
463 | * Currently "debug mode" is used to control the handling of the EEM_Message::debug_only status when |
||
464 | * generating/sending messages. Debug mode can be set by either: |
||
465 | * 1. Sending in a value for the $set_debug argument |
||
466 | * 2. Defining `EE_DEBUG_MESSAGES` constant in wp-config.php |
||
467 | * 3. Overriding the above via the provided filter. |
||
468 | * |
||
469 | * @param bool|null $set_debug If provided, then the debug mode will be set internally until reset via the |
||
470 | * provided boolean. When no argument is provided (default null) then the debug |
||
471 | * mode will be returned. |
||
472 | * @return bool true means Messages is in debug mode. false means messages system is not in debug mode. |
||
473 | */ |
||
474 | public static function debug($set_debug = null) |
||
490 | |||
491 | |||
492 | /** |
||
493 | * Deletes old messages meeting certain criteria for removal from the database. |
||
494 | * By default, this will delete messages that: |
||
495 | * - are older than the value of the delete_threshold in months. |
||
496 | * - have a STS_ID other than EEM_Message::status_idle |
||
497 | * |
||
498 | * @param int $delete_threshold This integer will be used to set the boundary for what messages are deleted in months. |
||
499 | * @return bool|false|int Either the number of records affected or false if there was an error (you can call |
||
500 | * $wpdb->last_error to find out what the error was. |
||
501 | */ |
||
502 | public function delete_old_messages($delete_threshold = 6) |
||
565 | |||
566 | } |
||
567 | // End of file EEM_Message.model.php |
||
569 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.