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() { |
||
172 | return $this->_messenger; |
||
173 | //EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
174 | //return EEH_MSG_Template::messenger_obj( $this->messenger() ); |
||
175 | } |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Sets messenger |
||
181 | * |
||
182 | * @param EE_Messenger $messenger |
||
183 | */ |
||
184 | public function set_messenger_object( EE_Messenger $messenger ) { |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * validates messenger |
||
192 | * |
||
193 | * @param bool $throw_exceptions |
||
194 | * @return bool |
||
195 | * @throws \EE_Error |
||
196 | */ |
||
197 | public function valid_messenger( $throw_exceptions = false ) { |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * This returns the set localized label for the messenger on this message. |
||
219 | * Note, if unable to retrieve the EE_Messenger object then will just return the messenger slug saved |
||
220 | * with this message. |
||
221 | * |
||
222 | * @param bool $plural whether to return the plural label or not. |
||
223 | * @return string |
||
224 | */ |
||
225 | public function messenger_label( $plural = false ) { |
||
230 | |||
231 | |||
232 | |||
233 | /** |
||
234 | * Gets message_type |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function message_type() { |
||
241 | |||
242 | |||
243 | |||
244 | /** |
||
245 | * Sets message_type |
||
246 | * |
||
247 | * @param string $message_type |
||
248 | */ |
||
249 | public function set_message_type( $message_type ) { |
||
252 | |||
253 | |||
254 | |||
255 | /** |
||
256 | * Returns the message type object for the set message type on this message |
||
257 | * |
||
258 | * @return EE_message_type | null |
||
259 | */ |
||
260 | public function message_type_object() { |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * Sets message_type |
||
270 | * |
||
271 | * @param EE_Message_Type $message_type |
||
272 | */ |
||
273 | public function set_message_type_object( EE_Message_Type $message_type ) { |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * validates message_type |
||
281 | * |
||
282 | * @param bool $throw_exceptions |
||
283 | * @return bool |
||
284 | * @throws \EE_Error |
||
285 | */ |
||
286 | public function valid_message_type( $throw_exceptions = false ) { |
||
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * This returns the set localized label for the message type on this message. |
||
308 | * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved |
||
309 | * with this message. |
||
310 | * |
||
311 | * @param bool $plural whether to return the plural label or not. |
||
312 | * @return string |
||
313 | */ |
||
314 | public function message_type_label( $plural = false ) { |
||
319 | |||
320 | |||
321 | |||
322 | /** |
||
323 | * Gets context |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function context() { |
||
330 | |||
331 | |||
332 | |||
333 | |||
334 | /** |
||
335 | * This returns the corresponding localized label for the given context slug, if possible from installed message types. |
||
336 | * Otherwise, this will just return the set context slug on this object. |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | public function context_label() { |
||
346 | |||
347 | |||
348 | |||
349 | /** |
||
350 | * Sets context |
||
351 | * |
||
352 | * @param string $context |
||
353 | */ |
||
354 | public function set_context( $context ) { |
||
357 | |||
358 | |||
359 | |||
360 | /** |
||
361 | * Gets recipient_ID |
||
362 | * |
||
363 | * @return int |
||
364 | */ |
||
365 | public function recipient_ID() { |
||
368 | |||
369 | |||
370 | |||
371 | /** |
||
372 | * Sets recipient_ID |
||
373 | * |
||
374 | * @param string $recipient_ID |
||
375 | */ |
||
376 | public function set_recipient_ID( $recipient_ID ) { |
||
379 | |||
380 | |||
381 | |||
382 | /** |
||
383 | * Gets recipient_type |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | public function recipient_type() { |
||
390 | |||
391 | |||
392 | |||
393 | |||
394 | /** |
||
395 | * Return the related object matching the recipient type and ID. |
||
396 | * |
||
397 | * @return EE_Base_Class | null |
||
398 | */ |
||
399 | public function recipient_object() { |
||
406 | |||
407 | |||
408 | |||
409 | /** |
||
410 | * Sets recipient_type |
||
411 | * |
||
412 | * @param string $recipient_type |
||
413 | */ |
||
414 | public function set_recipient_type( $recipient_type ) { |
||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * Gets content |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | public function content() { |
||
428 | |||
429 | |||
430 | |||
431 | /** |
||
432 | * Sets content |
||
433 | * |
||
434 | * @param string $content |
||
435 | */ |
||
436 | public function set_content( $content ) { |
||
439 | |||
440 | |||
441 | |||
442 | /** |
||
443 | * Gets subject |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function subject() { |
||
450 | |||
451 | |||
452 | |||
453 | /** |
||
454 | * Sets subject |
||
455 | * |
||
456 | * @param string $subject |
||
457 | */ |
||
458 | public function set_subject( $subject ) { |
||
461 | |||
462 | |||
463 | |||
464 | /** |
||
465 | * Gets to |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | public function to() { |
||
473 | |||
474 | |||
475 | |||
476 | /** |
||
477 | * Sets to |
||
478 | * |
||
479 | * @param string $to |
||
480 | */ |
||
481 | public function set_to( $to ) { |
||
484 | |||
485 | |||
486 | |||
487 | /** |
||
488 | * Gets from |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | public function from() { |
||
495 | |||
496 | |||
497 | |||
498 | /** |
||
499 | * Sets from |
||
500 | * |
||
501 | * @param string $from |
||
502 | */ |
||
503 | public function set_from( $from ) { |
||
506 | |||
507 | |||
508 | |||
509 | |||
510 | |||
511 | /** |
||
512 | * Gets priority |
||
513 | * |
||
514 | * @return int |
||
515 | */ |
||
516 | public function priority() { |
||
519 | |||
520 | |||
521 | |||
522 | /** |
||
523 | * Sets priority |
||
524 | * |
||
525 | * @param int $priority |
||
526 | */ |
||
527 | public function set_priority( $priority ) { |
||
530 | |||
531 | |||
532 | |||
533 | /** |
||
534 | * Gets STS_ID |
||
535 | * |
||
536 | * @return string |
||
537 | */ |
||
538 | public function STS_ID() { |
||
541 | |||
542 | |||
543 | |||
544 | /** |
||
545 | * Sets STS_ID |
||
546 | * |
||
547 | * @param string $STS_ID |
||
548 | */ |
||
549 | public function set_STS_ID( $STS_ID ) { |
||
552 | |||
553 | |||
554 | |||
555 | /** |
||
556 | * Gets created |
||
557 | * |
||
558 | * @return string |
||
559 | */ |
||
560 | public function created() { |
||
563 | |||
564 | |||
565 | |||
566 | /** |
||
567 | * Sets created |
||
568 | * |
||
569 | * @param string $created |
||
570 | */ |
||
571 | public function set_created( $created ) { |
||
574 | |||
575 | |||
576 | |||
577 | /** |
||
578 | * Gets modified |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | public function modified() { |
||
585 | |||
586 | |||
587 | |||
588 | /** |
||
589 | * Sets modified |
||
590 | * |
||
591 | * @param string $modified |
||
592 | */ |
||
593 | public function set_modified( $modified ) { |
||
596 | |||
597 | |||
598 | |||
599 | |||
600 | /** |
||
601 | * Sets generation data for this message. |
||
602 | * @param mixed $data |
||
603 | */ |
||
604 | public function set_generation_data( $data ) { |
||
607 | |||
608 | |||
609 | |||
610 | |||
611 | |||
612 | /** |
||
613 | * Returns any set generation data for this message. |
||
614 | * @return mixed|null |
||
615 | */ |
||
616 | public function get_generation_data() { |
||
619 | |||
620 | |||
621 | |||
622 | |||
623 | /** |
||
624 | * Gets any error message. |
||
625 | * @return mixed|null |
||
626 | */ |
||
627 | public function error_message() { |
||
630 | |||
631 | |||
632 | /** |
||
633 | * Sets an error message. |
||
634 | * @param $message |
||
635 | * @return bool|int |
||
636 | */ |
||
637 | public function set_error_message( $message ) { |
||
640 | |||
641 | |||
642 | |||
643 | |||
644 | /** |
||
645 | * This retrieves the associated template pack with this message. |
||
646 | * @return EE_Messages_Template_Pack | null |
||
647 | */ |
||
648 | View Code Duplication | public function get_template_pack() { |
|
668 | |||
669 | |||
670 | |||
671 | |||
672 | /** |
||
673 | * Retrieves the variation used for generating this message. |
||
674 | * @return string |
||
675 | */ |
||
676 | View Code Duplication | public function get_template_pack_variation() { |
|
698 | |||
699 | /** |
||
700 | * Return the link to the admin details for the object. |
||
701 | * @return string |
||
702 | */ |
||
703 | public function get_admin_details_link() { |
||
719 | |||
720 | /** |
||
721 | * Returns the link to the editor for the object. Sometimes this is the same as the details. |
||
722 | * @return string |
||
723 | */ |
||
724 | public function get_admin_edit_link() { |
||
727 | |||
728 | /** |
||
729 | * Returns the link to a settings page for the object. |
||
730 | * @return string |
||
731 | */ |
||
732 | View Code Duplication | public function get_admin_settings_link() { |
|
742 | |||
743 | /** |
||
744 | * Returns the link to the "overview" for the object (typically the "list table" view). |
||
745 | * @return string |
||
746 | */ |
||
747 | View Code Duplication | public function get_admin_overview_link() { |
|
757 | |||
758 | |||
759 | } |
||
760 | /* End of file EE_Message.class.php */ |
||
761 | /* 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 theSon
calls the wrong method in the parent class.