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 |
||
20 | final class EE_System implements ResettableInterface |
||
21 | { |
||
22 | |||
23 | |||
24 | /** |
||
25 | * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation. |
||
26 | * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc |
||
27 | */ |
||
28 | const req_type_normal = 0; |
||
29 | |||
30 | /** |
||
31 | * Indicates this is a brand new installation of EE so we should install |
||
32 | * tables and default data etc |
||
33 | */ |
||
34 | const req_type_new_activation = 1; |
||
35 | |||
36 | /** |
||
37 | * we've detected that EE has been reactivated (or EE was activated during maintenance mode, |
||
38 | * and we just exited maintenance mode). We MUST check the database is setup properly |
||
39 | * and that default data is setup too |
||
40 | */ |
||
41 | const req_type_reactivation = 2; |
||
42 | |||
43 | /** |
||
44 | * indicates that EE has been upgraded since its previous request. |
||
45 | * We may have data migration scripts to call and will want to trigger maintenance mode |
||
46 | */ |
||
47 | const req_type_upgrade = 3; |
||
48 | |||
49 | /** |
||
50 | * TODO will detect that EE has been DOWNGRADED. We probably don't want to run in this case... |
||
51 | */ |
||
52 | const req_type_downgrade = 4; |
||
53 | |||
54 | /** |
||
55 | * @deprecated since version 4.6.0.dev.006 |
||
56 | * Now whenever a new_activation is detected the request type is still just |
||
57 | * new_activation (same for reactivation, upgrade, downgrade etc), but if we'r ein maintenance mode |
||
58 | * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required |
||
59 | * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode. |
||
60 | * (Specifically, when the migration manager indicates migrations are finished |
||
61 | * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called) |
||
62 | */ |
||
63 | const req_type_activation_but_not_installed = 5; |
||
64 | |||
65 | /** |
||
66 | * option prefix for recording the activation history (like core's "espresso_db_update") of addons |
||
67 | */ |
||
68 | const addon_activation_history_option_prefix = 'ee_addon_activation_history_'; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @var EE_System $_instance |
||
73 | */ |
||
74 | private static $_instance; |
||
75 | |||
76 | /** |
||
77 | * @var EE_Registry $registry |
||
78 | */ |
||
79 | private $registry; |
||
80 | |||
81 | /** |
||
82 | * @var LoaderInterface $loader |
||
83 | */ |
||
84 | private $loader; |
||
85 | |||
86 | /** |
||
87 | * @var EE_Capabilities $capabilities |
||
88 | */ |
||
89 | private $capabilities; |
||
90 | |||
91 | /** |
||
92 | * @var EE_Request $request |
||
93 | */ |
||
94 | private $request; |
||
95 | |||
96 | /** |
||
97 | * @var EE_Maintenance_Mode $maintenance_mode |
||
98 | */ |
||
99 | private $maintenance_mode; |
||
100 | |||
101 | /** |
||
102 | * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*. |
||
103 | * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request. |
||
104 | * |
||
105 | * @var int $_req_type |
||
106 | */ |
||
107 | private $_req_type; |
||
108 | |||
109 | /** |
||
110 | * Whether or not there was a non-micro version change in EE core version during this request |
||
111 | * |
||
112 | * @var boolean $_major_version_change |
||
113 | */ |
||
114 | private $_major_version_change = false; |
||
115 | |||
116 | |||
117 | |||
118 | /** |
||
119 | * @singleton method used to instantiate class object |
||
120 | * @param EE_Registry|null $registry |
||
121 | * @param LoaderInterface|null $loader |
||
122 | * @param EE_Capabilities|null $capabilities |
||
123 | * @param EE_Request|null $request |
||
124 | * @param EE_Maintenance_Mode|null $maintenance_mode |
||
125 | * @return EE_System |
||
126 | */ |
||
127 | public static function instance( |
||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * resets the instance and returns it |
||
145 | * |
||
146 | * @return EE_System |
||
147 | */ |
||
148 | public static function reset() |
||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * sets hooks for running rest of system |
||
164 | * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
||
165 | * starting EE Addons from any other point may lead to problems |
||
166 | * |
||
167 | * @param EE_Registry $registry |
||
168 | * @param LoaderInterface $loader |
||
169 | * @param EE_Capabilities $capabilities |
||
170 | * @param EE_Request $request |
||
171 | * @param EE_Maintenance_Mode $maintenance_mode |
||
172 | */ |
||
173 | private function __construct( |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * load and setup EE_Capabilities |
||
249 | * |
||
250 | * @return void |
||
251 | * @throws EE_Error |
||
252 | */ |
||
253 | public function loadCapabilities() |
||
263 | |||
264 | |||
265 | |||
266 | /** |
||
267 | * create and cache the CommandBus, and also add middleware |
||
268 | * The CapChecker middleware requires the use of EE_Capabilities |
||
269 | * which is why we need to load the CommandBus after Caps are set up |
||
270 | * |
||
271 | * @return void |
||
272 | * @throws EE_Error |
||
273 | */ |
||
274 | public function loadCommandBus() |
||
290 | |||
291 | |||
292 | |||
293 | /** |
||
294 | * @return void |
||
295 | * @throws EE_Error |
||
296 | */ |
||
297 | public function loadPluginApi() |
||
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * load_espresso_addons |
||
308 | * allow addons to load first so that they can set hooks for running DMS's, etc |
||
309 | * this is hooked into both: |
||
310 | * 'AHEE__EE_Bootstrap__load_core_configuration' |
||
311 | * which runs during the WP 'plugins_loaded' action at priority 5 |
||
312 | * and the WP 'activate_plugin' hook point |
||
313 | * |
||
314 | * @access public |
||
315 | * @return void |
||
316 | * @throws EE_Error |
||
317 | */ |
||
318 | public function load_espresso_addons() |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * detect_activations_or_upgrades |
||
347 | * Checks for activation or upgrade of core first; |
||
348 | * then also checks if any registered addons have been activated or upgraded |
||
349 | * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades' |
||
350 | * which runs during the WP 'plugins_loaded' action at priority 3 |
||
351 | * |
||
352 | * @access public |
||
353 | * @return void |
||
354 | */ |
||
355 | public function detect_activations_or_upgrades() |
||
366 | |||
367 | |||
368 | |||
369 | /** |
||
370 | * detect_if_activation_or_upgrade |
||
371 | * Takes care of detecting whether this is a brand new install or code upgrade, |
||
372 | * and either setting up the DB or setting up maintenance mode etc. |
||
373 | * |
||
374 | * @access public |
||
375 | * @return void |
||
376 | */ |
||
377 | public function detect_if_activation_or_upgrade() |
||
413 | |||
414 | |||
415 | |||
416 | /** |
||
417 | * Updates the list of installed versions and sets hooks for |
||
418 | * initializing the database later during the request |
||
419 | * |
||
420 | * @param array $espresso_db_update |
||
421 | */ |
||
422 | private function _handle_core_version_change($espresso_db_update) |
||
431 | |||
432 | |||
433 | |||
434 | /** |
||
435 | * standardizes the wp option 'espresso_db_upgrade' which actually stores |
||
436 | * information about what versions of EE have been installed and activated, |
||
437 | * NOT necessarily the state of the database |
||
438 | * |
||
439 | * @param mixed $espresso_db_update the value of the WordPress option. |
||
440 | * If not supplied, fetches it from the options table |
||
441 | * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction |
||
442 | */ |
||
443 | private function fix_espresso_db_upgrade_option($espresso_db_update = null) |
||
482 | |||
483 | |||
484 | |||
485 | /** |
||
486 | * Does the traditional work of setting up the plugin's database and adding default data. |
||
487 | * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. |
||
488 | * NOTE: if we're in maintenance mode (which would be the case if we detect there are data |
||
489 | * migration scripts that need to be run and a version change happens), enqueues core for database initialization, |
||
490 | * so that it will be done when migrations are finished |
||
491 | * |
||
492 | * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too; |
||
493 | * @param boolean $verify_schema if true will re-check the database tables have the correct schema. |
||
494 | * This is a resource-intensive job |
||
495 | * so we prefer to only do it when necessary |
||
496 | * @return void |
||
497 | * @throws EE_Error |
||
498 | */ |
||
499 | public function initialize_db_if_no_migrations_required($initialize_addons_too = false, $verify_schema = true) |
||
526 | |||
527 | |||
528 | |||
529 | /** |
||
530 | * Initializes the db for all registered addons |
||
531 | * |
||
532 | * @throws EE_Error |
||
533 | */ |
||
534 | public function initialize_addons() |
||
543 | |||
544 | |||
545 | |||
546 | /** |
||
547 | * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed. |
||
548 | * |
||
549 | * @param array $version_history |
||
550 | * @param string $current_version_to_add version to be added to the version history |
||
551 | * @return boolean success as to whether or not this option was changed |
||
552 | */ |
||
553 | public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null) |
||
565 | |||
566 | |||
567 | |||
568 | /** |
||
569 | * Detects if the current version indicated in the has existed in the list of |
||
570 | * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side-effect) |
||
571 | * |
||
572 | * @param array $espresso_db_update array from the wp option stored under the name 'espresso_db_update'. |
||
573 | * If not supplied, fetches it from the options table. |
||
574 | * Also, caches its result so later parts of the code can also know whether |
||
575 | * there's been an update or not. This way we can add the current version to |
||
576 | * espresso_db_update, but still know if this is a new install or not |
||
577 | * @return int one of the constants on EE_System::req_type_ |
||
578 | */ |
||
579 | public function detect_req_type($espresso_db_update = null) |
||
593 | |||
594 | |||
595 | |||
596 | /** |
||
597 | * Returns whether or not there was a non-micro version change (ie, change in either |
||
598 | * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000, |
||
599 | * but not 4.9.0.rc.0001 to 4.9.1.rc.0001 |
||
600 | * |
||
601 | * @param $activation_history |
||
602 | * @return bool |
||
603 | */ |
||
604 | private function _detect_major_version_change($activation_history) |
||
614 | |||
615 | |||
616 | |||
617 | /** |
||
618 | * Returns true if either the major or minor version of EE changed during this request. |
||
619 | * 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 |
||
620 | * |
||
621 | * @return bool |
||
622 | */ |
||
623 | public function is_major_version_change() |
||
627 | |||
628 | |||
629 | |||
630 | /** |
||
631 | * Determines the request type for any ee addon, given three piece of info: the current array of activation |
||
632 | * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily |
||
633 | * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was |
||
634 | * just activated to (for core that will always be espresso_version()) |
||
635 | * |
||
636 | * @param array $activation_history_for_addon the option's value which stores the activation history for this |
||
637 | * ee plugin. for core that's 'espresso_db_update' |
||
638 | * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to |
||
639 | * indicate that this plugin was just activated |
||
640 | * @param string $version_to_upgrade_to the version that was just upgraded to (for core that will be |
||
641 | * espresso_version()) |
||
642 | * @return int one of the constants on EE_System::req_type_* |
||
643 | */ |
||
644 | public static function detect_req_type_given_activation_history( |
||
692 | |||
693 | |||
694 | |||
695 | /** |
||
696 | * Detects if the $version_to_upgrade_to is higher than the most recent version in |
||
697 | * the $activation_history_for_addon |
||
698 | * |
||
699 | * @param array $activation_history_for_addon (keys are versions, values are arrays of times activated, |
||
700 | * sometimes containing 'unknown-date' |
||
701 | * @param string $version_to_upgrade_to (current version) |
||
702 | * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ). |
||
703 | * ie, -1 if $version_to_upgrade_to is LOWER (downgrade); |
||
704 | * 0 if $version_to_upgrade_to MATCHES (reactivation or normal request); |
||
705 | * 1 if $version_to_upgrade_to is HIGHER (upgrade) ; |
||
706 | */ |
||
707 | private static function _new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to) |
||
714 | |||
715 | |||
716 | |||
717 | /** |
||
718 | * Gets the most recently active version listed in the activation history, |
||
719 | * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'. |
||
720 | * |
||
721 | * @param array $activation_history (keys are versions, values are arrays of times activated, |
||
722 | * sometimes containing 'unknown-date' |
||
723 | * @return string |
||
724 | */ |
||
725 | private static function _get_most_recently_active_version_from_activation_history($activation_history) |
||
751 | |||
752 | |||
753 | |||
754 | /** |
||
755 | * This redirects to the about EE page after activation |
||
756 | * |
||
757 | * @return void |
||
758 | */ |
||
759 | public function redirect_to_about_ee() |
||
784 | |||
785 | |||
786 | |||
787 | /** |
||
788 | * load_core_configuration |
||
789 | * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration' |
||
790 | * which runs during the WP 'plugins_loaded' action at priority 5 |
||
791 | * |
||
792 | * @return void |
||
793 | * @throws ReflectionException |
||
794 | */ |
||
795 | public function load_core_configuration() |
||
821 | |||
822 | |||
823 | |||
824 | /** |
||
825 | * cycles through all of the models/*.model.php files, and assembles an array of model names |
||
826 | * |
||
827 | * @return void |
||
828 | * @throws ReflectionException |
||
829 | */ |
||
830 | private function _parse_model_names() |
||
852 | |||
853 | |||
854 | |||
855 | /** |
||
856 | * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks |
||
857 | * that need to be setup before our EE_System launches. |
||
858 | * |
||
859 | * @return void |
||
860 | */ |
||
861 | private function _maybe_brew_regular() |
||
867 | |||
868 | |||
869 | |||
870 | /** |
||
871 | * register_shortcodes_modules_and_widgets |
||
872 | * generate lists of shortcodes and modules, then verify paths and classes |
||
873 | * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets' |
||
874 | * which runs during the WP 'plugins_loaded' action at priority 7 |
||
875 | * |
||
876 | * @access public |
||
877 | * @return void |
||
878 | * @throws Exception |
||
879 | */ |
||
880 | public function register_shortcodes_modules_and_widgets() |
||
900 | |||
901 | |||
902 | |||
903 | /** |
||
904 | * _incompatible_addon_error |
||
905 | * |
||
906 | * @access public |
||
907 | * @return void |
||
908 | */ |
||
909 | private function _incompatible_addon_error() |
||
939 | |||
940 | |||
941 | |||
942 | /** |
||
943 | * brew_espresso |
||
944 | * begins the process of setting hooks for initializing EE in the correct order |
||
945 | * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point |
||
946 | * which runs during the WP 'plugins_loaded' action at priority 9 |
||
947 | * |
||
948 | * @return void |
||
949 | */ |
||
950 | public function brew_espresso() |
||
968 | |||
969 | |||
970 | |||
971 | /** |
||
972 | * set_hooks_for_core |
||
973 | * |
||
974 | * @access public |
||
975 | * @return void |
||
976 | * @throws EE_Error |
||
977 | */ |
||
978 | public function set_hooks_for_core() |
||
986 | |||
987 | |||
988 | |||
989 | /** |
||
990 | * Using the information gathered in EE_System::_incompatible_addon_error, |
||
991 | * deactivates any addons considered incompatible with the current version of EE |
||
992 | */ |
||
993 | private function _deactivate_incompatible_addons() |
||
1008 | |||
1009 | |||
1010 | |||
1011 | /** |
||
1012 | * perform_activations_upgrades_and_migrations |
||
1013 | * |
||
1014 | * @access public |
||
1015 | * @return void |
||
1016 | */ |
||
1017 | public function perform_activations_upgrades_and_migrations() |
||
1025 | |||
1026 | |||
1027 | |||
1028 | /** |
||
1029 | * load_CPTs_and_session |
||
1030 | * |
||
1031 | * @access public |
||
1032 | * @return void |
||
1033 | */ |
||
1034 | public function load_CPTs_and_session() |
||
1041 | |||
1042 | |||
1043 | |||
1044 | /** |
||
1045 | * load_controllers |
||
1046 | * this is the best place to load any additional controllers that needs access to EE core. |
||
1047 | * it is expected that all basic core EE systems, that are not dependant on the current request are loaded at this |
||
1048 | * time |
||
1049 | * |
||
1050 | * @access public |
||
1051 | * @return void |
||
1052 | */ |
||
1053 | public function load_controllers() |
||
1066 | |||
1067 | |||
1068 | |||
1069 | /** |
||
1070 | * core_loaded_and_ready |
||
1071 | * all of the basic EE core should be loaded at this point and available regardless of M-Mode |
||
1072 | * |
||
1073 | * @access public |
||
1074 | * @return void |
||
1075 | */ |
||
1076 | public function core_loaded_and_ready() |
||
1087 | |||
1088 | |||
1089 | |||
1090 | /** |
||
1091 | * initialize |
||
1092 | * this is the best place to begin initializing client code |
||
1093 | * |
||
1094 | * @access public |
||
1095 | * @return void |
||
1096 | */ |
||
1097 | public function initialize() |
||
1101 | |||
1102 | |||
1103 | |||
1104 | /** |
||
1105 | * initialize_last |
||
1106 | * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to |
||
1107 | * initialize has done so |
||
1108 | * |
||
1109 | * @access public |
||
1110 | * @return void |
||
1111 | */ |
||
1112 | public function initialize_last() |
||
1117 | |||
1118 | |||
1119 | |||
1120 | /** |
||
1121 | * @return void |
||
1122 | * @throws EE_Error |
||
1123 | */ |
||
1124 | public function addEspressoToolbar() |
||
1131 | |||
1132 | |||
1133 | |||
1134 | /** |
||
1135 | * do_not_cache |
||
1136 | * sets no cache headers and defines no cache constants for WP plugins |
||
1137 | * |
||
1138 | * @access public |
||
1139 | * @return void |
||
1140 | */ |
||
1141 | public static function do_not_cache() |
||
1160 | |||
1161 | |||
1162 | |||
1163 | /** |
||
1164 | * extra_nocache_headers |
||
1165 | * |
||
1166 | * @access public |
||
1167 | * @param $headers |
||
1168 | * @return array |
||
1169 | */ |
||
1170 | public static function extra_nocache_headers($headers) |
||
1178 | |||
1179 | |||
1180 | |||
1181 | /** |
||
1182 | * nocache_headers |
||
1183 | * |
||
1184 | * @access public |
||
1185 | * @return void |
||
1186 | */ |
||
1187 | public static function nocache_headers() |
||
1191 | |||
1192 | |||
1193 | |||
1194 | |||
1195 | /** |
||
1196 | * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are |
||
1197 | * never returned with the function. |
||
1198 | * |
||
1199 | * @param array $exclude_array any existing pages being excluded are in this array. |
||
1200 | * @return array |
||
1201 | */ |
||
1202 | public function remove_pages_from_wp_list_pages($exclude_array) |
||
1206 | |||
1207 | |||
1208 | |||
1209 | } |
||
1210 | // End of file EE_System.core.php |
||
1212 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):