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_Addon 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_Addon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
||
19 | { |
||
20 | |||
21 | |||
22 | /** |
||
23 | * prefix to be added onto an addon's plugin slug to make a wp option name |
||
24 | * which will be used to store the plugin's activation history |
||
25 | */ |
||
26 | const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
||
27 | |||
28 | /** |
||
29 | * @var $_version |
||
30 | * @type string |
||
31 | */ |
||
32 | protected $_version = ''; |
||
33 | |||
34 | /** |
||
35 | * @var $_min_core_version |
||
36 | * @type string |
||
37 | */ |
||
38 | protected $_min_core_version = ''; |
||
39 | |||
40 | /** |
||
41 | * derived from plugin 'main_file_path using plugin_basename() |
||
42 | * |
||
43 | * @type string $_plugin_basename |
||
44 | */ |
||
45 | protected $_plugin_basename = ''; |
||
46 | |||
47 | /** |
||
48 | * A non-internationalized name to identify this addon for use in URLs, etc |
||
49 | * |
||
50 | * @type string $_plugin_slug |
||
51 | */ |
||
52 | protected $_plugin_slug = ''; |
||
53 | |||
54 | /** |
||
55 | * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
||
56 | * |
||
57 | * @type string _addon_name |
||
58 | */ |
||
59 | protected $_addon_name = ''; |
||
60 | |||
61 | /** |
||
62 | * one of the EE_System::req_type_* constants |
||
63 | * |
||
64 | * @type int $_req_type |
||
65 | */ |
||
66 | protected $_req_type; |
||
67 | |||
68 | /** |
||
69 | * page slug to be used when generating the "Settings" link on the WP plugin page |
||
70 | * |
||
71 | * @type string $_plugin_action_slug |
||
72 | */ |
||
73 | protected $_plugin_action_slug = ''; |
||
74 | |||
75 | /** |
||
76 | * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
||
77 | * that can be used for adding upgrading/marketing info |
||
78 | * |
||
79 | * @type array $_plugins_page_row |
||
80 | */ |
||
81 | protected $_plugins_page_row = array(); |
||
82 | |||
83 | |||
84 | /** |
||
85 | * filepath to the main file, which can be used for register_activation_hook, register_deactivation_hook, etc. |
||
86 | * |
||
87 | * @type string |
||
88 | */ |
||
89 | protected $_main_plugin_file; |
||
90 | |||
91 | /** |
||
92 | * This is the slug used to identify this add-on within the plugin update engine. |
||
93 | * |
||
94 | * @type string |
||
95 | */ |
||
96 | protected $pue_slug; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @var EE_Dependency_Map $dependency_map |
||
101 | */ |
||
102 | private $dependency_map; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @var DomainInterface $domain |
||
107 | */ |
||
108 | private $domain; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @param EE_Dependency_Map $dependency_map [optional] |
||
113 | * @param DomainInterface $domain [optional] |
||
114 | */ |
||
115 | public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
||
125 | |||
126 | |||
127 | /** |
||
128 | * @param EE_Dependency_Map $dependency_map |
||
129 | */ |
||
130 | public function setDependencyMap($dependency_map) |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @return EE_Dependency_Map |
||
138 | */ |
||
139 | public function dependencyMap() |
||
143 | |||
144 | |||
145 | /** |
||
146 | * @param DomainInterface $domain |
||
147 | */ |
||
148 | public function setDomain(DomainInterface $domain) |
||
152 | |||
153 | /** |
||
154 | * @return DomainInterface |
||
155 | */ |
||
156 | public function domain() |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @param mixed $version |
||
164 | */ |
||
165 | public function set_version($version = null) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * get__version |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function version() |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @param mixed $min_core_version |
||
184 | */ |
||
185 | public function set_min_core_version($min_core_version = null) |
||
189 | |||
190 | |||
191 | /** |
||
192 | * get__min_core_version |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function min_core_version() |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Sets addon_name |
||
204 | * |
||
205 | * @param string $addon_name |
||
206 | * @return boolean |
||
207 | */ |
||
208 | public function set_name($addon_name) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Gets addon_name |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function name() |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function plugin_basename() |
||
233 | |||
234 | |||
235 | /** |
||
236 | * @param string $plugin_basename |
||
237 | */ |
||
238 | public function set_plugin_basename($plugin_basename) |
||
243 | |||
244 | |||
245 | /** |
||
246 | * @return string |
||
247 | */ |
||
248 | public function plugin_slug() |
||
253 | |||
254 | |||
255 | /** |
||
256 | * @param string $plugin_slug |
||
257 | */ |
||
258 | public function set_plugin_slug($plugin_slug) |
||
263 | |||
264 | |||
265 | /** |
||
266 | * @return string |
||
267 | */ |
||
268 | public function plugin_action_slug() |
||
273 | |||
274 | |||
275 | /** |
||
276 | * @param string $plugin_action_slug |
||
277 | */ |
||
278 | public function set_plugin_action_slug($plugin_action_slug) |
||
283 | |||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | public function get_plugins_page_row() |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param array $plugins_page_row |
||
297 | */ |
||
298 | public function set_plugins_page_row($plugins_page_row = array()) |
||
308 | |||
309 | |||
310 | /** |
||
311 | * Called when EE core detects this addon has been activated for the first time. |
||
312 | * If the site isn't in maintenance mode, should setup the addon's database |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | View Code Duplication | public function new_install() |
|
327 | |||
328 | |||
329 | /** |
||
330 | * Called when EE core detects this addon has been reactivated. When this happens, |
||
331 | * it's good to just check that your data is still intact |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | View Code Duplication | public function reactivation() |
|
346 | |||
347 | |||
348 | /** |
||
349 | * Called when the registered deactivation hook for this addon fires. |
||
350 | * |
||
351 | * @throws EE_Error |
||
352 | */ |
||
353 | public function deactivation() |
||
362 | |||
363 | |||
364 | /** |
||
365 | * Takes care of double-checking that we're not in maintenance mode, and then |
||
366 | * initializing this addon's necessary initial data. This is called by default on new activations |
||
367 | * and reactivations. |
||
368 | * |
||
369 | * @param boolean $verify_schema whether to verify the database's schema for this addon, or just its data. |
||
370 | * This is a resource-intensive job so we prefer to only do it when necessary |
||
371 | * @return void |
||
372 | * @throws \EE_Error |
||
373 | * @throws InvalidInterfaceException |
||
374 | * @throws InvalidDataTypeException |
||
375 | * @throws InvalidArgumentException |
||
376 | */ |
||
377 | public function initialize_db_if_no_migrations_required($verify_schema = true) |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Used to setup this addon's database tables, but not necessarily any default |
||
424 | * data in them. The default is to actually use the most up-to-date data migration script |
||
425 | * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
||
426 | * methods to setup the db. |
||
427 | */ |
||
428 | public function initialize_db() |
||
451 | |||
452 | |||
453 | /** |
||
454 | * If you want to setup default data for the addon, override this method, and call |
||
455 | * parent::initialize_default_data() from within it. This is normally called |
||
456 | * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
||
457 | * and should verify default data is present (but this is also called |
||
458 | * on reactivations and just after migrations, so please verify you actually want |
||
459 | * to ADD default data, because it may already be present). |
||
460 | * However, please call this parent (currently it just fires a hook which other |
||
461 | * addons may be depending on) |
||
462 | */ |
||
463 | public function initialize_default_data() |
||
476 | |||
477 | |||
478 | /** |
||
479 | * EE Core detected that this addon has been upgraded. We should check if there |
||
480 | * are any new migration scripts, and if so put the site into maintenance mode until |
||
481 | * they're ran |
||
482 | * |
||
483 | * @return void |
||
484 | */ |
||
485 | View Code Duplication | public function upgrade() |
|
497 | |||
498 | |||
499 | /** |
||
500 | * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
||
501 | */ |
||
502 | public function downgrade() |
||
513 | |||
514 | |||
515 | /** |
||
516 | * set_db_update_option_name |
||
517 | * Until we do something better, we'll just check for migration scripts upon |
||
518 | * plugin activation only. In the future, we'll want to do it on plugin updates too |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | public function set_db_update_option_name() |
||
535 | |||
536 | |||
537 | /** |
||
538 | * Returns the name of the activation indicator option |
||
539 | * (an option which is set temporarily to indicate that this addon was just activated) |
||
540 | * |
||
541 | * @deprecated since version 4.3.0.alpha.016 |
||
542 | * @return string |
||
543 | */ |
||
544 | public function get_db_update_option_name() |
||
556 | |||
557 | |||
558 | /** |
||
559 | * When the addon is activated, this should be called to set a wordpress option that |
||
560 | * indicates it was activated. This is especially useful for detecting reactivations. |
||
561 | * |
||
562 | * @return bool |
||
563 | */ |
||
564 | public function set_activation_indicator_option() |
||
569 | |||
570 | |||
571 | /** |
||
572 | * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function get_activation_indicator_option_name() |
||
580 | |||
581 | |||
582 | /** |
||
583 | * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
||
584 | * |
||
585 | * @param int $req_type |
||
586 | */ |
||
587 | public function set_req_type($req_type) |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
||
595 | * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
||
596 | * EE_System when it is checking for new install or upgrades of addons |
||
597 | */ |
||
598 | public function detect_req_type() |
||
605 | |||
606 | |||
607 | /** |
||
608 | * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
||
609 | * Should only be called once per request |
||
610 | * |
||
611 | * @return void |
||
612 | */ |
||
613 | public function detect_activation_or_upgrade() |
||
655 | |||
656 | /** |
||
657 | * Updates the version history for this addon |
||
658 | * |
||
659 | * @param array $version_history |
||
660 | * @param string $current_version_to_add |
||
661 | * @return boolean success |
||
662 | */ |
||
663 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
675 | |||
676 | /** |
||
677 | * Gets the name of the wp option that stores the activation history |
||
678 | * of this addon |
||
679 | * |
||
680 | * @return string |
||
681 | */ |
||
682 | public function get_activation_history_option_name() |
||
686 | |||
687 | |||
688 | /** |
||
689 | * Gets the wp option which stores the activation history for this addon |
||
690 | * |
||
691 | * @return array |
||
692 | */ |
||
693 | public function get_activation_history() |
||
697 | |||
698 | |||
699 | /** |
||
700 | * @param string $config_section |
||
701 | */ |
||
702 | public function set_config_section($config_section = '') |
||
706 | |||
707 | /** |
||
708 | * Sets the filepath to the main plugin file |
||
709 | * |
||
710 | * @param string $filepath |
||
711 | */ |
||
712 | public function set_main_plugin_file($filepath) |
||
716 | |||
717 | /** |
||
718 | * gets the filepath to teh main file |
||
719 | * |
||
720 | * @return string |
||
721 | */ |
||
722 | public function get_main_plugin_file() |
||
726 | |||
727 | /** |
||
728 | * Gets the filename (no path) of the main file (the main file loaded |
||
729 | * by WP) |
||
730 | * |
||
731 | * @return string |
||
732 | */ |
||
733 | public function get_main_plugin_file_basename() |
||
737 | |||
738 | /** |
||
739 | * Gets the folder name which contains the main plugin file |
||
740 | * |
||
741 | * @return string |
||
742 | */ |
||
743 | public function get_main_plugin_file_dirname() |
||
747 | |||
748 | |||
749 | /** |
||
750 | * sets hooks used in the admin |
||
751 | * |
||
752 | * @return void |
||
753 | */ |
||
754 | public function admin_init() |
||
762 | |||
763 | |||
764 | /** |
||
765 | * plugin_actions |
||
766 | * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
||
767 | * |
||
768 | * @param $links |
||
769 | * @param $file |
||
770 | * @return array |
||
771 | */ |
||
772 | public function plugin_action_links($links, $file) |
||
785 | |||
786 | |||
787 | /** |
||
788 | * after_plugin_row |
||
789 | * Add additional content to the plugins page plugin row |
||
790 | * Inserts another row |
||
791 | * |
||
792 | * @param $plugin_file |
||
793 | * @param $plugin_data |
||
794 | * @param $status |
||
795 | * @return void |
||
796 | */ |
||
797 | public function after_plugin_row($plugin_file, $plugin_data, $status) |
||
868 | |||
869 | |||
870 | /** |
||
871 | * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
||
872 | * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
||
873 | * for back compat reasons. |
||
874 | * |
||
875 | * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
||
876 | * |
||
877 | * It is recommended, if client code is `de-registering` an add-on, then do it on the |
||
878 | * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
||
879 | * callback does not get run/set in that request. |
||
880 | * |
||
881 | * Also, keep in mind that if a registered add-on happens to be deactivated via |
||
882 | * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
||
883 | * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
||
884 | * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
||
885 | * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
||
886 | * to call `parent::deactivation`. |
||
887 | * |
||
888 | * @since 4.9.26 |
||
889 | */ |
||
890 | public function after_registration() |
||
894 | |||
895 | /** |
||
896 | * @return string |
||
897 | */ |
||
898 | public function getPueSlug() |
||
902 | /** |
||
903 | * @param string $pue_slug |
||
904 | */ |
||
905 | public function setPueSlug($pue_slug) |
||
909 | } |
||
910 |