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_System 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_System, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | final class EE_System implements ResettableInterface |
||
24 | { |
||
25 | |||
26 | |||
27 | /** |
||
28 | * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
||
29 | * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
||
30 | */ |
||
31 | const req_type_normal = 0; |
||
32 | |||
33 | /** |
||
34 | * Indicates this is a brand new installation of EE so we should install |
||
35 | * tables and default data etc |
||
36 | */ |
||
37 | const req_type_new_activation = 1; |
||
38 | |||
39 | /** |
||
40 | * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
||
41 | * and we just exited maintenance mode). We MUST check the database is setup properly |
||
42 | * and that default data is setup too |
||
43 | */ |
||
44 | const req_type_reactivation = 2; |
||
45 | |||
46 | /** |
||
47 | * indicates that EE has been upgraded since its previous request. |
||
48 | * We may have data migration scripts to call and will want to trigger maintenance mode |
||
49 | */ |
||
50 | const req_type_upgrade = 3; |
||
51 | |||
52 | /** |
||
53 | * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
||
54 | */ |
||
55 | const req_type_downgrade = 4; |
||
56 | |||
57 | /** |
||
58 | * @deprecated since version 4.6.0.dev.006 |
||
59 | * Now whenever a new_activation is detected the request type is still just |
||
60 | * new_activation (same for reactivation, upgrade, downgrade etc), but if we'r ein maintenance mode |
||
61 | * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
||
62 | * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
||
63 | * (Specifically, when the migration manager indicates migrations are finished |
||
64 | * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
||
65 | */ |
||
66 | const req_type_activation_but_not_installed = 5; |
||
67 | |||
68 | /** |
||
69 | * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
||
70 | */ |
||
71 | const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
||
72 | |||
73 | |||
74 | /** |
||
75 | * @var EE_System $_instance |
||
76 | */ |
||
77 | private static $_instance; |
||
78 | |||
79 | /** |
||
80 | * @var EE_Registry $registry |
||
81 | */ |
||
82 | private $registry; |
||
83 | |||
84 | /** |
||
85 | * @var LoaderInterface $loader |
||
86 | */ |
||
87 | private $loader; |
||
88 | |||
89 | /** |
||
90 | * @var EE_Capabilities $capabilities |
||
91 | */ |
||
92 | private $capabilities; |
||
93 | |||
94 | /** |
||
95 | * @var RequestInterface $request |
||
96 | */ |
||
97 | private $request; |
||
98 | |||
99 | /** |
||
100 | * @var EE_Maintenance_Mode $maintenance_mode |
||
101 | */ |
||
102 | private $maintenance_mode; |
||
103 | |||
104 | /** |
||
105 | * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
||
106 | * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
||
107 | * |
||
108 | * @var int $_req_type |
||
109 | */ |
||
110 | private $_req_type; |
||
111 | |||
112 | /** |
||
113 | * Whether or not there was a non-micro version change in EE core version during this request |
||
114 | * |
||
115 | * @var boolean $_major_version_change |
||
116 | */ |
||
117 | private $_major_version_change = false; |
||
118 | |||
119 | /** |
||
120 | * A Context DTO dedicated solely to identifying the current request type. |
||
121 | * |
||
122 | * @var RequestTypeContextCheckerInterface $request_type |
||
123 | */ |
||
124 | private $request_type; |
||
|
|||
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @singleton method used to instantiate class object |
||
130 | * @param EE_Registry|null $registry |
||
131 | * @param LoaderInterface|null $loader |
||
132 | * @param RequestInterface|null $request |
||
133 | * @param EE_Maintenance_Mode|null $maintenance_mode |
||
134 | * @return EE_System |
||
135 | */ |
||
136 | public static function instance( |
||
137 | EE_Registry $registry = null, |
||
138 | LoaderInterface $loader = null, |
||
139 | RequestInterface $request = null, |
||
140 | EE_Maintenance_Mode $maintenance_mode = null |
||
141 | ) { |
||
142 | // check if class object is instantiated |
||
143 | if (! self::$_instance instanceof EE_System) { |
||
144 | self::$_instance = new self($registry, $loader, $request, $maintenance_mode); |
||
145 | } |
||
146 | return self::$_instance; |
||
147 | } |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * resets the instance and returns it |
||
153 | * |
||
154 | * @return EE_System |
||
155 | */ |
||
156 | public static function reset() |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * sets hooks for running rest of system |
||
172 | * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
||
173 | * starting EE Addons from any other point may lead to problems |
||
174 | * |
||
175 | * @param EE_Registry $registry |
||
176 | * @param LoaderInterface $loader |
||
177 | * @param RequestInterface $request |
||
178 | * @param EE_Maintenance_Mode $maintenance_mode |
||
179 | */ |
||
180 | private function __construct( |
||
249 | |||
250 | |||
251 | /** |
||
252 | * load and setup EE_Capabilities |
||
253 | * |
||
254 | * @return void |
||
255 | * @throws EE_Error |
||
256 | */ |
||
257 | public function loadCapabilities() |
||
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * create and cache the CommandBus, and also add middleware |
||
273 | * The CapChecker middleware requires the use of EE_Capabilities |
||
274 | * which is why we need to load the CommandBus after Caps are set up |
||
275 | * |
||
276 | * @return void |
||
277 | * @throws EE_Error |
||
278 | */ |
||
279 | public function loadCommandBus() |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * @return void |
||
300 | * @throws EE_Error |
||
301 | */ |
||
302 | public function loadPluginApi() |
||
309 | |||
310 | |||
311 | /** |
||
312 | * @param string $addon_name |
||
313 | * @param string $version_constant |
||
314 | * @param string $min_version_required |
||
315 | * @param string $load_callback |
||
316 | * @param string $plugin_file_constant |
||
317 | * @return void |
||
318 | */ |
||
319 | private function deactivateIncompatibleAddon( |
||
351 | |||
352 | |||
353 | /** |
||
354 | * load_espresso_addons |
||
355 | * allow addons to load first so that they can set hooks for running DMS's, etc |
||
356 | * this is hooked into both: |
||
357 | * 'AHEE__EE_Bootstrap__load_core_configuration' |
||
358 | * which runs during the WP 'plugins_loaded' action at priority 5 |
||
359 | * and the WP 'activate_plugin' hook point |
||
360 | * |
||
361 | * @access public |
||
362 | * @return void |
||
363 | */ |
||
364 | public function load_espresso_addons() |
||
400 | |||
401 | |||
402 | |||
403 | /** |
||
404 | * detect_activations_or_upgrades |
||
405 | * Checks for activation or upgrade of core first; |
||
406 | * then also checks if any registered addons have been activated or upgraded |
||
407 | * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
||
408 | * which runs during the WP 'plugins_loaded' action at priority 3 |
||
409 | * |
||
410 | * @access public |
||
411 | * @return void |
||
412 | */ |
||
413 | public function detect_activations_or_upgrades() |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * detect_if_activation_or_upgrade |
||
429 | * Takes care of detecting whether this is a brand new install or code upgrade, |
||
430 | * and either setting up the DB or setting up maintenance mode etc. |
||
431 | * |
||
432 | * @access public |
||
433 | * @return void |
||
434 | */ |
||
435 | public function detect_if_activation_or_upgrade() |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * Updates the list of installed versions and sets hooks for |
||
475 | * initializing the database later during the request |
||
476 | * |
||
477 | * @param array $espresso_db_update |
||
478 | */ |
||
479 | private function _handle_core_version_change($espresso_db_update) |
||
488 | |||
489 | |||
490 | |||
491 | /** |
||
492 | * standardizes the wp option 'espresso_db_upgrade' which actually stores |
||
493 | * information about what versions of EE have been installed and activated, |
||
494 | * NOT necessarily the state of the database |
||
495 | * |
||
496 | * @param mixed $espresso_db_update the value of the WordPress option. |
||
497 | * If not supplied, fetches it from the options table |
||
498 | * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
||
499 | */ |
||
500 | private function fix_espresso_db_upgrade_option($espresso_db_update = null) |
||
539 | |||
540 | |||
541 | |||
542 | /** |
||
543 | * Does the traditional work of setting up the plugin's database and adding default data. |
||
544 | * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
||
545 | * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
||
546 | * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
||
547 | * so that it will be done when migrations are finished |
||
548 | * |
||
549 | * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too; |
||
550 | * @param boolean $verify_schema if true will re-check the database tables have the correct schema. |
||
551 | * This is a resource-intensive job |
||
552 | * so we prefer to only do it when necessary |
||
553 | * @return void |
||
554 | * @throws EE_Error |
||
555 | */ |
||
556 | public function initialize_db_if_no_migrations_required($initialize_addons_too = false, $verify_schema = true) |
||
583 | |||
584 | |||
585 | |||
586 | /** |
||
587 | * Initializes the db for all registered addons |
||
588 | * |
||
589 | * @throws EE_Error |
||
590 | */ |
||
591 | public function initialize_addons() |
||
600 | |||
601 | |||
602 | |||
603 | /** |
||
604 | * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
||
605 | * |
||
606 | * @param array $version_history |
||
607 | * @param string $current_version_to_add version to be added to the version history |
||
608 | * @return boolean success as to whether or not this option was changed |
||
609 | */ |
||
610 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
622 | |||
623 | |||
624 | |||
625 | /** |
||
626 | * Detects if the current version indicated in the has existed in the list of |
||
627 | * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side-effect) |
||
628 | * |
||
629 | * @param array $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
||
630 | * If not supplied, fetches it from the options table. |
||
631 | * Also, caches its result so later parts of the code can also know whether |
||
632 | * there's been an update or not. This way we can add the current version to |
||
633 | * espresso_db_update, but still know if this is a new install or not |
||
634 | * @return int one of the constants on EE_System::req_type_ |
||
635 | */ |
||
636 | public function detect_req_type($espresso_db_update = null) |
||
651 | |||
652 | |||
653 | |||
654 | /** |
||
655 | * Returns whether or not there was a non-micro version change (ie, change in either |
||
656 | * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
||
657 | * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
||
658 | * |
||
659 | * @param $activation_history |
||
660 | * @return bool |
||
661 | */ |
||
662 | private function _detect_major_version_change($activation_history) |
||
672 | |||
673 | |||
674 | |||
675 | /** |
||
676 | * Returns true if either the major or minor version of EE changed during this request. |
||
677 | * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | public function is_major_version_change() |
||
685 | |||
686 | |||
687 | |||
688 | /** |
||
689 | * Determines the request type for any ee addon, given three piece of info: the current array of activation |
||
690 | * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
||
691 | * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
||
692 | * just activated to (for core that will always be espresso_version()) |
||
693 | * |
||
694 | * @param array $activation_history_for_addon the option's value which stores the activation history for this |
||
695 | * ee plugin. for core that's 'espresso_db_update' |
||
696 | * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
||
697 | * indicate that this plugin was just activated |
||
698 | * @param string $version_to_upgrade_to the version that was just upgraded to (for core that will be |
||
699 | * espresso_version()) |
||
700 | * @return int one of the constants on EE_System::req_type_* |
||
701 | */ |
||
702 | public static function detect_req_type_given_activation_history( |
||
750 | |||
751 | |||
752 | |||
753 | /** |
||
754 | * Detects if the $version_to_upgrade_to is higher than the most recent version in |
||
755 | * the $activation_history_for_addon |
||
756 | * |
||
757 | * @param array $activation_history_for_addon (keys are versions, values are arrays of times activated, |
||
758 | * sometimes containing 'unknown-date' |
||
759 | * @param string $version_to_upgrade_to (current version) |
||
760 | * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
||
761 | * ie, -1 if $version_to_upgrade_to is LOWER (downgrade); |
||
762 | * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
||
763 | * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
||
764 | */ |
||
765 | private static function _new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to) |
||
772 | |||
773 | |||
774 | |||
775 | /** |
||
776 | * Gets the most recently active version listed in the activation history, |
||
777 | * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
||
778 | * |
||
779 | * @param array $activation_history (keys are versions, values are arrays of times activated, |
||
780 | * sometimes containing 'unknown-date' |
||
781 | * @return string |
||
782 | */ |
||
783 | private static function _get_most_recently_active_version_from_activation_history($activation_history) |
||
811 | |||
812 | |||
813 | |||
814 | /** |
||
815 | * This redirects to the about EE page after activation |
||
816 | * |
||
817 | * @return void |
||
818 | */ |
||
819 | public function redirect_to_about_ee() |
||
843 | |||
844 | |||
845 | |||
846 | /** |
||
847 | * load_core_configuration |
||
848 | * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
||
849 | * which runs during the WP 'plugins_loaded' action at priority 5 |
||
850 | * |
||
851 | * @return void |
||
852 | * @throws ReflectionException |
||
853 | */ |
||
854 | public function load_core_configuration() |
||
880 | |||
881 | |||
882 | |||
883 | /** |
||
884 | * cycles through all of the models/*.model.php files, and assembles an array of model names |
||
885 | * |
||
886 | * @return void |
||
887 | * @throws ReflectionException |
||
888 | */ |
||
889 | private function _parse_model_names() |
||
911 | |||
912 | |||
913 | |||
914 | /** |
||
915 | * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
||
916 | * that need to be setup before our EE_System launches. |
||
917 | * |
||
918 | * @return void |
||
919 | */ |
||
920 | private function _maybe_brew_regular() |
||
926 | |||
927 | |||
928 | |||
929 | /** |
||
930 | * register_shortcodes_modules_and_widgets |
||
931 | * generate lists of shortcodes and modules, then verify paths and classes |
||
932 | * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
||
933 | * which runs during the WP 'plugins_loaded' action at priority 7 |
||
934 | * |
||
935 | * @access public |
||
936 | * @return void |
||
937 | * @throws Exception |
||
938 | */ |
||
939 | public function register_shortcodes_modules_and_widgets() |
||
961 | |||
962 | |||
963 | |||
964 | /** |
||
965 | * _incompatible_addon_error |
||
966 | * |
||
967 | * @access public |
||
968 | * @return void |
||
969 | */ |
||
970 | private function _incompatible_addon_error() |
||
1000 | |||
1001 | |||
1002 | |||
1003 | /** |
||
1004 | * brew_espresso |
||
1005 | * begins the process of setting hooks for initializing EE in the correct order |
||
1006 | * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
||
1007 | * which runs during the WP 'plugins_loaded' action at priority 9 |
||
1008 | * |
||
1009 | * @return void |
||
1010 | */ |
||
1011 | public function brew_espresso() |
||
1029 | |||
1030 | |||
1031 | |||
1032 | /** |
||
1033 | * set_hooks_for_core |
||
1034 | * |
||
1035 | * @access public |
||
1036 | * @return void |
||
1037 | * @throws EE_Error |
||
1038 | */ |
||
1039 | public function set_hooks_for_core() |
||
1047 | |||
1048 | |||
1049 | |||
1050 | /** |
||
1051 | * Using the information gathered in EE_System::_incompatible_addon_error, |
||
1052 | * deactivates any addons considered incompatible with the current version of EE |
||
1053 | */ |
||
1054 | private function _deactivate_incompatible_addons() |
||
1069 | |||
1070 | |||
1071 | |||
1072 | /** |
||
1073 | * perform_activations_upgrades_and_migrations |
||
1074 | * |
||
1075 | * @access public |
||
1076 | * @return void |
||
1077 | */ |
||
1078 | public function perform_activations_upgrades_and_migrations() |
||
1086 | |||
1087 | |||
1088 | |||
1089 | /** |
||
1090 | * load_CPTs_and_session |
||
1091 | * |
||
1092 | * @access public |
||
1093 | * @return void |
||
1094 | */ |
||
1095 | public function load_CPTs_and_session() |
||
1102 | |||
1103 | |||
1104 | |||
1105 | /** |
||
1106 | * load_controllers |
||
1107 | * this is the best place to load any additional controllers that needs access to EE core. |
||
1108 | * it is expected that all basic core EE systems, that are not dependant on the current request are loaded at this |
||
1109 | * time |
||
1110 | * |
||
1111 | * @access public |
||
1112 | * @return void |
||
1113 | */ |
||
1114 | public function load_controllers() |
||
1130 | |||
1131 | |||
1132 | |||
1133 | /** |
||
1134 | * core_loaded_and_ready |
||
1135 | * all of the basic EE core should be loaded at this point and available regardless of M-Mode |
||
1136 | * |
||
1137 | * @access public |
||
1138 | * @return void |
||
1139 | */ |
||
1140 | public function core_loaded_and_ready() |
||
1162 | |||
1163 | |||
1164 | |||
1165 | /** |
||
1166 | * initialize |
||
1167 | * this is the best place to begin initializing client code |
||
1168 | * |
||
1169 | * @access public |
||
1170 | * @return void |
||
1171 | */ |
||
1172 | public function initialize() |
||
1176 | |||
1177 | |||
1178 | |||
1179 | /** |
||
1180 | * initialize_last |
||
1181 | * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
||
1182 | * initialize has done so |
||
1183 | * |
||
1184 | * @access public |
||
1185 | * @return void |
||
1186 | */ |
||
1187 | public function initialize_last() |
||
1192 | |||
1193 | |||
1194 | |||
1195 | /** |
||
1196 | * @return void |
||
1197 | * @throws EE_Error |
||
1198 | */ |
||
1199 | public function addEspressoToolbar() |
||
1206 | |||
1207 | |||
1208 | |||
1209 | /** |
||
1210 | * do_not_cache |
||
1211 | * sets no cache headers and defines no cache constants for WP plugins |
||
1212 | * |
||
1213 | * @access public |
||
1214 | * @return void |
||
1215 | */ |
||
1216 | public static function do_not_cache() |
||
1235 | |||
1236 | |||
1237 | |||
1238 | /** |
||
1239 | * extra_nocache_headers |
||
1240 | * |
||
1241 | * @access public |
||
1242 | * @param $headers |
||
1243 | * @return array |
||
1244 | */ |
||
1245 | public static function extra_nocache_headers($headers) |
||
1253 | |||
1254 | |||
1255 | |||
1256 | /** |
||
1257 | * nocache_headers |
||
1258 | * |
||
1259 | * @access public |
||
1260 | * @return void |
||
1261 | */ |
||
1262 | public static function nocache_headers() |
||
1266 | |||
1267 | |||
1268 | |||
1269 | /** |
||
1270 | * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
||
1271 | * never returned with the function. |
||
1272 | * |
||
1273 | * @param array $exclude_array any existing pages being excluded are in this array. |
||
1274 | * @return array |
||
1275 | */ |
||
1276 | public function remove_pages_from_wp_list_pages($exclude_array) |
||
1280 | |||
1281 | |||
1282 | |||
1283 | } |
||
1284 | // End of file EE_System.core.php |
||
1286 |
This check marks private properties in classes that are never used. Those properties can be removed.