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 |
||
17 | abstract class EE_Addon extends EE_Configurable implements RequiresDependencyMapInterface, RequiresDomainInterface |
||
18 | { |
||
19 | |||
20 | |||
21 | /** |
||
22 | * prefix to be added onto an addon's plugin slug to make a wp option name |
||
23 | * which will be used to store the plugin's activation history |
||
24 | */ |
||
25 | const ee_addon_version_history_option_prefix = 'ee_version_history_'; |
||
26 | |||
27 | /** |
||
28 | * @var $_version |
||
29 | * @type string |
||
30 | */ |
||
31 | protected $_version = ''; |
||
32 | |||
33 | /** |
||
34 | * @var $_min_core_version |
||
35 | * @type string |
||
36 | */ |
||
37 | protected $_min_core_version = ''; |
||
38 | |||
39 | /** |
||
40 | * derived from plugin 'main_file_path using plugin_basename() |
||
41 | * |
||
42 | * @type string $_plugin_basename |
||
43 | */ |
||
44 | protected $_plugin_basename = ''; |
||
45 | |||
46 | /** |
||
47 | * A non-internationalized name to identify this addon for use in URLs, etc |
||
48 | * |
||
49 | * @type string $_plugin_slug |
||
50 | */ |
||
51 | protected $_plugin_slug = ''; |
||
52 | |||
53 | /** |
||
54 | * A non-internationalized name to identify this addon. Eg 'Calendar','MailChimp',etc/ |
||
55 | * |
||
56 | * @type string _addon_name |
||
57 | */ |
||
58 | protected $_addon_name = ''; |
||
59 | |||
60 | /** |
||
61 | * one of the EE_System::req_type_* constants |
||
62 | * |
||
63 | * @type int $_req_type |
||
64 | */ |
||
65 | protected $_req_type; |
||
66 | |||
67 | /** |
||
68 | * page slug to be used when generating the "Settings" link on the WP plugin page |
||
69 | * |
||
70 | * @type string $_plugin_action_slug |
||
71 | */ |
||
72 | protected $_plugin_action_slug = ''; |
||
73 | |||
74 | /** |
||
75 | * if not empty, inserts a new table row after this plugin's row on the WP Plugins page |
||
76 | * that can be used for adding upgrading/marketing info |
||
77 | * |
||
78 | * @type array $_plugins_page_row |
||
79 | */ |
||
80 | protected $_plugins_page_row = array(); |
||
81 | |||
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 | /** |
||
93 | * @var EE_Dependency_Map $dependency_map |
||
94 | */ |
||
95 | private $dependency_map; |
||
96 | |||
97 | |||
98 | /** |
||
99 | * @var DomainInterface $domain |
||
100 | */ |
||
101 | private $domain; |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @param EE_Dependency_Map $dependency_map [optional] |
||
106 | * @param DomainInterface $domain [optional] |
||
107 | */ |
||
108 | public function __construct(EE_Dependency_Map $dependency_map = null, DomainInterface $domain = null) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * @param EE_Dependency_Map $dependency_map |
||
122 | */ |
||
123 | public function setDependencyMap($dependency_map) |
||
127 | |||
128 | |||
129 | /** |
||
130 | * @return EE_Dependency_Map |
||
131 | */ |
||
132 | public function dependencyMap() |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @param DomainInterface $domain |
||
140 | */ |
||
141 | public function setDomain(DomainInterface $domain) |
||
145 | |||
146 | /** |
||
147 | * @return DomainInterface |
||
148 | */ |
||
149 | public function domain() |
||
153 | |||
154 | |||
155 | /** |
||
156 | * @param mixed $version |
||
157 | */ |
||
158 | public function set_version($version = null) |
||
162 | |||
163 | |||
164 | /** |
||
165 | * get__version |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function version() |
||
173 | |||
174 | |||
175 | /** |
||
176 | * @param mixed $min_core_version |
||
177 | */ |
||
178 | public function set_min_core_version($min_core_version = null) |
||
182 | |||
183 | |||
184 | /** |
||
185 | * get__min_core_version |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function min_core_version() |
||
193 | |||
194 | |||
195 | /** |
||
196 | * Sets addon_name |
||
197 | * |
||
198 | * @param string $addon_name |
||
199 | * @return boolean |
||
200 | */ |
||
201 | public function set_name($addon_name) |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Gets addon_name |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function name() |
||
216 | |||
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | public function plugin_basename() |
||
226 | |||
227 | |||
228 | /** |
||
229 | * @param string $plugin_basename |
||
230 | */ |
||
231 | public function set_plugin_basename($plugin_basename) |
||
236 | |||
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | public function plugin_slug() |
||
246 | |||
247 | |||
248 | /** |
||
249 | * @param string $plugin_slug |
||
250 | */ |
||
251 | public function set_plugin_slug($plugin_slug) |
||
256 | |||
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | public function plugin_action_slug() |
||
266 | |||
267 | |||
268 | /** |
||
269 | * @param string $plugin_action_slug |
||
270 | */ |
||
271 | public function set_plugin_action_slug($plugin_action_slug) |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @return array |
||
280 | */ |
||
281 | public function get_plugins_page_row() |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @param array $plugins_page_row |
||
290 | */ |
||
291 | public function set_plugins_page_row($plugins_page_row = array()) |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Called when EE core detects this addon has been activated for the first time. |
||
305 | * If the site isn't in maintenance mode, should setup the addon's database |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | View Code Duplication | public function new_install() |
|
320 | |||
321 | |||
322 | /** |
||
323 | * Called when EE core detects this addon has been reactivated. When this happens, |
||
324 | * it's good to just check that your data is still intact |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | View Code Duplication | public function reactivation() |
|
339 | |||
340 | |||
341 | /** |
||
342 | * Called when the registered deactivation hook for this addon fires. |
||
343 | * @throws EE_Error |
||
344 | */ |
||
345 | public function deactivation() |
||
354 | |||
355 | |||
356 | /** |
||
357 | * Takes care of double-checking that we're not in maintenance mode, and then |
||
358 | * initializing this addon's necessary initial data. This is called by default on new activations |
||
359 | * and reactivations. |
||
360 | * |
||
361 | * @param boolean $verify_schema whether to verify the database's schema for this addon, or just its data. |
||
362 | * This is a resource-intensive job so we prefer to only do it when necessary |
||
363 | * @return void |
||
364 | * @throws \EE_Error |
||
365 | */ |
||
366 | public function initialize_db_if_no_migrations_required($verify_schema = true) |
||
405 | |||
406 | |||
407 | /** |
||
408 | * Used to setup this addon's database tables, but not necessarily any default |
||
409 | * data in them. The default is to actually use the most up-to-date data migration script |
||
410 | * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() |
||
411 | * methods to setup the db. |
||
412 | */ |
||
413 | public function initialize_db() |
||
436 | |||
437 | |||
438 | /** |
||
439 | * If you want to setup default data for the addon, override this method, and call |
||
440 | * parent::initialize_default_data() from within it. This is normally called |
||
441 | * from EE_Addon::initialize_db_if_no_migrations_required(), just after EE_Addon::initialize_db() |
||
442 | * and should verify default data is present (but this is also called |
||
443 | * on reactivations and just after migrations, so please verify you actually want |
||
444 | * to ADD default data, because it may already be present). |
||
445 | * However, please call this parent (currently it just fires a hook which other |
||
446 | * addons may be depending on) |
||
447 | */ |
||
448 | public function initialize_default_data() |
||
461 | |||
462 | |||
463 | /** |
||
464 | * EE Core detected that this addon has been upgraded. We should check if there |
||
465 | * are any new migration scripts, and if so put the site into maintenance mode until |
||
466 | * they're ran |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | View Code Duplication | public function upgrade() |
|
482 | |||
483 | |||
484 | /** |
||
485 | * If Core detects this addon has been downgraded, you may want to invoke some special logic here. |
||
486 | */ |
||
487 | public function downgrade() |
||
498 | |||
499 | |||
500 | /** |
||
501 | * set_db_update_option_name |
||
502 | * Until we do something better, we'll just check for migration scripts upon |
||
503 | * plugin activation only. In the future, we'll want to do it on plugin updates too |
||
504 | * |
||
505 | * @return bool |
||
506 | */ |
||
507 | public function set_db_update_option_name() |
||
520 | |||
521 | |||
522 | /** |
||
523 | * Returns the name of the activation indicator option |
||
524 | * (an option which is set temporarily to indicate that this addon was just activated) |
||
525 | * |
||
526 | * @deprecated since version 4.3.0.alpha.016 |
||
527 | * @return string |
||
528 | */ |
||
529 | public function get_db_update_option_name() |
||
541 | |||
542 | |||
543 | /** |
||
544 | * When the addon is activated, this should be called to set a wordpress option that |
||
545 | * indicates it was activated. This is especially useful for detecting reactivations. |
||
546 | * |
||
547 | * @return bool |
||
548 | */ |
||
549 | public function set_activation_indicator_option() |
||
554 | |||
555 | |||
556 | /** |
||
557 | * Gets the name of the wp option which is used to temporarily indicate that this addon was activated |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | public function get_activation_indicator_option_name() |
||
565 | |||
566 | |||
567 | /** |
||
568 | * Used by EE_System to set the request type of this addon. Should not be used by addon developers |
||
569 | * |
||
570 | * @param int $req_type |
||
571 | */ |
||
572 | public function set_req_type($req_type) |
||
576 | |||
577 | |||
578 | /** |
||
579 | * Returns the request type of this addon (ie, EE_System::req_type_normal, EE_System::req_type_new_activation, |
||
580 | * EE_System::req_type_reactivation, EE_System::req_type_upgrade, or EE_System::req_type_downgrade). This is set by |
||
581 | * EE_System when it is checking for new install or upgrades of addons |
||
582 | */ |
||
583 | public function detect_req_type() |
||
590 | |||
591 | |||
592 | /** |
||
593 | * Detects the request type for this addon (whether it was just activated, upgrades, a normal request, etc.) |
||
594 | * Should only be called once per request |
||
595 | * |
||
596 | * @return void |
||
597 | */ |
||
598 | public function detect_activation_or_upgrade() |
||
642 | |||
643 | /** |
||
644 | * Updates the version history for this addon |
||
645 | * |
||
646 | * @param array $version_history |
||
647 | * @param string $current_version_to_add |
||
648 | * @return boolean success |
||
649 | */ |
||
650 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
663 | |||
664 | /** |
||
665 | * Gets the name of the wp option that stores the activation history |
||
666 | * of this addon |
||
667 | * |
||
668 | * @return string |
||
669 | */ |
||
670 | public function get_activation_history_option_name() |
||
674 | |||
675 | |||
676 | /** |
||
677 | * Gets the wp option which stores the activation history for this addon |
||
678 | * |
||
679 | * @return array |
||
680 | */ |
||
681 | public function get_activation_history() |
||
685 | |||
686 | |||
687 | /** |
||
688 | * @param string $config_section |
||
689 | */ |
||
690 | public function set_config_section($config_section = '') |
||
694 | |||
695 | /** |
||
696 | * Sets the filepath to the main plugin file |
||
697 | * |
||
698 | * @param string $filepath |
||
699 | */ |
||
700 | public function set_main_plugin_file($filepath) |
||
704 | |||
705 | /** |
||
706 | * gets the filepath to teh main file |
||
707 | * |
||
708 | * @return string |
||
709 | */ |
||
710 | public function get_main_plugin_file() |
||
714 | |||
715 | /** |
||
716 | * Gets the filename (no path) of the main file (the main file loaded |
||
717 | * by WP) |
||
718 | * |
||
719 | * @return string |
||
720 | */ |
||
721 | public function get_main_plugin_file_basename() |
||
725 | |||
726 | /** |
||
727 | * Gets the folder name which contains the main plugin file |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function get_main_plugin_file_dirname() |
||
735 | |||
736 | |||
737 | /** |
||
738 | * sets hooks used in the admin |
||
739 | * |
||
740 | * @return void |
||
741 | */ |
||
742 | public function admin_init() |
||
750 | |||
751 | |||
752 | /** |
||
753 | * plugin_actions |
||
754 | * Add a settings link to the Plugins page, so people can go straight from the plugin page to the settings page. |
||
755 | * |
||
756 | * @param $links |
||
757 | * @param $file |
||
758 | * @return array |
||
759 | */ |
||
760 | public function plugin_action_links($links, $file) |
||
773 | |||
774 | |||
775 | /** |
||
776 | * after_plugin_row |
||
777 | * Add additional content to the plugins page plugin row |
||
778 | * Inserts another row |
||
779 | * |
||
780 | * @param $plugin_file |
||
781 | * @param $plugin_data |
||
782 | * @param $status |
||
783 | * @return void |
||
784 | */ |
||
785 | public function after_plugin_row($plugin_file, $plugin_data, $status) |
||
856 | |||
857 | |||
858 | /** |
||
859 | * A safe space for addons to add additional logic like setting hooks that need to be set early in the request. |
||
860 | * Child classes that have logic like that to run can override this method declaration. This was not made abstract |
||
861 | * for back compat reasons. |
||
862 | * |
||
863 | * This will fire on the `AHEE__EE_System__load_espresso_addons__complete` hook at priority 999. |
||
864 | * |
||
865 | * It is recommended, if client code is `de-registering` an add-on, then do it on the |
||
866 | * `AHEE__EE_System__load_espresso_addons__complete` hook before priority 999 so as to ensure any code logic in this |
||
867 | * callback does not get run/set in that request. |
||
868 | * |
||
869 | * Also, keep in mind that if a registered add-on happens to be deactivated via |
||
870 | * EE_System::_deactivate_incompatible_addons() because its incompatible, any code executed in this method |
||
871 | * (including setting hooks etc) will have executed before the plugin was deactivated. If you use |
||
872 | * `after_registration` to set any filter and/or action hooks and want to ensure they are removed on this add-on's |
||
873 | * deactivation, you can override `EE_Addon::deactivation` and unset your hooks and filters there. Just remember |
||
874 | * to call `parent::deactivation`. |
||
875 | * |
||
876 | * @since 4.9.26 |
||
877 | */ |
||
878 | public function after_registration() |
||
882 | } |
||
883 |