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() { |
||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * Sets messenger |
||
179 | * |
||
180 | * @param EE_Messenger $messenger |
||
181 | */ |
||
182 | public function set_messenger_object( EE_Messenger $messenger ) { |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * validates messenger |
||
190 | * |
||
191 | * @param bool $throw_exceptions |
||
192 | * @return bool |
||
193 | * @throws \EE_Error |
||
194 | */ |
||
195 | public function valid_messenger( $throw_exceptions = false ) { |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * This returns the set localized label for the messenger on this message. |
||
217 | * Note, if unable to retrieve the EE_Messenger object then will just return the messenger slug saved |
||
218 | * with this message. |
||
219 | * |
||
220 | * @param bool $plural whether to return the plural label or not. |
||
221 | * @return string |
||
222 | */ |
||
223 | public function messenger_label( $plural = false ) { |
||
228 | |||
229 | |||
230 | |||
231 | /** |
||
232 | * Gets message_type |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function message_type() { |
||
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * Sets message_type |
||
244 | * |
||
245 | * @param string $message_type |
||
246 | */ |
||
247 | public function set_message_type( $message_type ) { |
||
250 | |||
251 | |||
252 | |||
253 | /** |
||
254 | * Returns the message type object for the set message type on this message |
||
255 | * |
||
256 | * @return EE_message_type | null |
||
257 | */ |
||
258 | public function message_type_object() { |
||
261 | |||
262 | |||
263 | |||
264 | /** |
||
265 | * Sets message_type |
||
266 | * |
||
267 | * @param EE_Message_Type $message_type |
||
268 | */ |
||
269 | public function set_message_type_object( EE_Message_Type $message_type ) { |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * validates message_type |
||
277 | * |
||
278 | * @param bool $throw_exceptions |
||
279 | * @return bool |
||
280 | * @throws \EE_Error |
||
281 | */ |
||
282 | public function valid_message_type( $throw_exceptions = false ) { |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * validates messenger and message_type |
||
304 | * |
||
305 | * @param bool $throw_exceptions |
||
306 | * @return bool |
||
307 | * @throws \EE_Error |
||
308 | */ |
||
309 | public function is_valid( $throw_exceptions = false ) { |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * This returns the set localized label for the message type on this message. |
||
320 | * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved |
||
321 | * with this message. |
||
322 | * |
||
323 | * @param bool $plural whether to return the plural label or not. |
||
324 | * @return string |
||
325 | */ |
||
326 | public function message_type_label( $plural = false ) { |
||
331 | |||
332 | |||
333 | |||
334 | /** |
||
335 | * Gets context |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function context() { |
||
342 | |||
343 | |||
344 | |||
345 | |||
346 | /** |
||
347 | * This returns the corresponding localized label for the given context slug, if possible from installed message types. |
||
348 | * Otherwise, this will just return the set context slug on this object. |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | public function context_label() { |
||
358 | |||
359 | |||
360 | |||
361 | /** |
||
362 | * Sets context |
||
363 | * |
||
364 | * @param string $context |
||
365 | */ |
||
366 | public function set_context( $context ) { |
||
369 | |||
370 | |||
371 | |||
372 | /** |
||
373 | * Gets recipient_ID |
||
374 | * |
||
375 | * @return int |
||
376 | */ |
||
377 | public function recipient_ID() { |
||
380 | |||
381 | |||
382 | |||
383 | /** |
||
384 | * Sets recipient_ID |
||
385 | * |
||
386 | * @param string $recipient_ID |
||
387 | */ |
||
388 | public function set_recipient_ID( $recipient_ID ) { |
||
391 | |||
392 | |||
393 | |||
394 | /** |
||
395 | * Gets recipient_type |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function recipient_type() { |
||
402 | |||
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * Return the related object matching the recipient type and ID. |
||
408 | * |
||
409 | * @return EE_Base_Class | null |
||
410 | */ |
||
411 | public function recipient_object() { |
||
418 | |||
419 | |||
420 | |||
421 | /** |
||
422 | * Sets recipient_type |
||
423 | * |
||
424 | * @param string $recipient_type |
||
425 | */ |
||
426 | public function set_recipient_type( $recipient_type ) { |
||
429 | |||
430 | |||
431 | |||
432 | /** |
||
433 | * Gets content |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | public function content() { |
||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * Sets content |
||
445 | * |
||
446 | * @param string $content |
||
447 | */ |
||
448 | public function set_content( $content ) { |
||
451 | |||
452 | |||
453 | |||
454 | /** |
||
455 | * Gets subject |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | public function subject() { |
||
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * Sets subject |
||
467 | * |
||
468 | * @param string $subject |
||
469 | */ |
||
470 | public function set_subject( $subject ) { |
||
473 | |||
474 | |||
475 | |||
476 | /** |
||
477 | * Gets to |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | public function to() { |
||
485 | |||
486 | |||
487 | |||
488 | /** |
||
489 | * Sets to |
||
490 | * |
||
491 | * @param string $to |
||
492 | */ |
||
493 | public function set_to( $to ) { |
||
496 | |||
497 | |||
498 | |||
499 | /** |
||
500 | * Gets from |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | public function from() { |
||
507 | |||
508 | |||
509 | |||
510 | /** |
||
511 | * Sets from |
||
512 | * |
||
513 | * @param string $from |
||
514 | */ |
||
515 | public function set_from( $from ) { |
||
518 | |||
519 | |||
520 | |||
521 | |||
522 | |||
523 | /** |
||
524 | * Gets priority |
||
525 | * |
||
526 | * @return int |
||
527 | */ |
||
528 | public function priority() { |
||
531 | |||
532 | |||
533 | |||
534 | /** |
||
535 | * Sets priority |
||
536 | * |
||
537 | * @param int $priority |
||
538 | */ |
||
539 | public function set_priority( $priority ) { |
||
542 | |||
543 | |||
544 | |||
545 | /** |
||
546 | * Gets STS_ID |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | public function STS_ID() { |
||
553 | |||
554 | |||
555 | |||
556 | /** |
||
557 | * Sets STS_ID |
||
558 | * |
||
559 | * @param string $STS_ID |
||
560 | */ |
||
561 | public function set_STS_ID( $STS_ID ) { |
||
564 | |||
565 | |||
566 | |||
567 | /** |
||
568 | * Gets created |
||
569 | * |
||
570 | * @return string |
||
571 | */ |
||
572 | public function created() { |
||
575 | |||
576 | |||
577 | |||
578 | /** |
||
579 | * Sets created |
||
580 | * |
||
581 | * @param string $created |
||
582 | */ |
||
583 | public function set_created( $created ) { |
||
586 | |||
587 | |||
588 | |||
589 | /** |
||
590 | * Gets modified |
||
591 | * |
||
592 | * @return string |
||
593 | */ |
||
594 | public function modified() { |
||
597 | |||
598 | |||
599 | |||
600 | /** |
||
601 | * Sets modified |
||
602 | * |
||
603 | * @param string $modified |
||
604 | */ |
||
605 | public function set_modified( $modified ) { |
||
608 | |||
609 | |||
610 | |||
611 | |||
612 | /** |
||
613 | * Sets generation data for this message. |
||
614 | * @param mixed $data |
||
615 | */ |
||
616 | public function set_generation_data( $data ) { |
||
619 | |||
620 | |||
621 | |||
622 | |||
623 | |||
624 | /** |
||
625 | * Returns any set generation data for this message. |
||
626 | * @return mixed|null |
||
627 | */ |
||
628 | public function get_generation_data() { |
||
631 | |||
632 | |||
633 | |||
634 | |||
635 | /** |
||
636 | * Gets any error message. |
||
637 | * @return mixed|null |
||
638 | */ |
||
639 | public function error_message() { |
||
642 | |||
643 | |||
644 | /** |
||
645 | * Sets an error message. |
||
646 | * @param $message |
||
647 | * @return bool|int |
||
648 | */ |
||
649 | public function set_error_message( $message ) { |
||
652 | |||
653 | |||
654 | |||
655 | |||
656 | /** |
||
657 | * This retrieves the associated template pack with this message. |
||
658 | * @return EE_Messages_Template_Pack | null |
||
659 | */ |
||
660 | View Code Duplication | public function get_template_pack() { |
|
680 | |||
681 | |||
682 | |||
683 | |||
684 | /** |
||
685 | * Retrieves the variation used for generating this message. |
||
686 | * @return string |
||
687 | */ |
||
688 | View Code Duplication | public function get_template_pack_variation() { |
|
710 | |||
711 | /** |
||
712 | * Return the link to the admin details for the object. |
||
713 | * @return string |
||
714 | */ |
||
715 | public function get_admin_details_link() { |
||
731 | |||
732 | /** |
||
733 | * Returns the link to the editor for the object. Sometimes this is the same as the details. |
||
734 | * @return string |
||
735 | */ |
||
736 | public function get_admin_edit_link() { |
||
739 | |||
740 | /** |
||
741 | * Returns the link to a settings page for the object. |
||
742 | * @return string |
||
743 | */ |
||
744 | View Code Duplication | public function get_admin_settings_link() { |
|
754 | |||
755 | /** |
||
756 | * Returns the link to the "overview" for the object (typically the "list table" view). |
||
757 | * @return string |
||
758 | */ |
||
759 | View Code Duplication | public function get_admin_overview_link() { |
|
769 | |||
770 | |||
771 | } |
||
772 | /* End of file EE_Message.class.php */ |
||
773 | /* 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.