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_Registry 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_Registry, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
11 | class EE_Registry { |
||
12 | |||
13 | /** |
||
14 | * EE_Registry Object |
||
15 | * @var EE_Registry $_instance |
||
16 | * @access private |
||
17 | */ |
||
18 | private static $_instance = null; |
||
19 | |||
20 | /** |
||
21 | * @var array $_class_abbreviations |
||
22 | * @access protected |
||
23 | */ |
||
24 | protected $_class_abbreviations = array(); |
||
25 | |||
26 | /** |
||
27 | * @var array $_auto_resolve_dependencies |
||
28 | * @access protected |
||
29 | */ |
||
30 | protected $_auto_resolve_dependencies = array(); |
||
31 | |||
32 | /** |
||
33 | * EE_Cart Object |
||
34 | * @access public |
||
35 | * @var EE_Cart $CART |
||
36 | */ |
||
37 | public $CART = null; |
||
38 | |||
39 | /** |
||
40 | * EE_Config Object |
||
41 | * @access public |
||
42 | * @var EE_Config $CFG |
||
43 | */ |
||
44 | public $CFG = null; |
||
45 | |||
46 | /** |
||
47 | * EE_Network_Config Object |
||
48 | * @access public |
||
49 | * @var EE_Network_Config $NET_CFG |
||
50 | */ |
||
51 | public $NET_CFG = null; |
||
52 | |||
53 | /** |
||
54 | * StdClass object for storing library classes in |
||
55 | * @public LIB |
||
56 | */ |
||
57 | public $LIB = null; |
||
58 | |||
59 | /** |
||
60 | * EE_Request_Handler Object |
||
61 | * @access public |
||
62 | * @var EE_Request_Handler $REQ |
||
63 | */ |
||
64 | public $REQ = null; |
||
65 | |||
66 | /** |
||
67 | * EE_Session Object |
||
68 | * @access public |
||
69 | * @var EE_Session $SSN |
||
70 | */ |
||
71 | public $SSN = null; |
||
72 | |||
73 | /** |
||
74 | * holds the ee capabilities object. |
||
75 | * |
||
76 | * @since 4.5.0 |
||
77 | * |
||
78 | * @var EE_Capabilities |
||
79 | */ |
||
80 | public $CAP = null; |
||
81 | |||
82 | /** |
||
83 | * holds the EE_Message_Resource_Manager object. |
||
84 | * |
||
85 | * @since 4.9.0 |
||
86 | * |
||
87 | * @var EE_Message_Resource_Manager |
||
88 | */ |
||
89 | public $MRM = null; |
||
90 | |||
91 | /** |
||
92 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
93 | * @access public |
||
94 | * @var EE_Addon[] |
||
95 | */ |
||
96 | public $addons = null; |
||
97 | |||
98 | /** |
||
99 | * $models |
||
100 | * @access public |
||
101 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
102 | */ |
||
103 | public $models = array(); |
||
104 | |||
105 | /** |
||
106 | * $modules |
||
107 | * @access public |
||
108 | * @var EED_Module[] $modules |
||
109 | */ |
||
110 | public $modules = null; |
||
111 | |||
112 | /** |
||
113 | * $shortcodes |
||
114 | * @access public |
||
115 | * @var EES_Shortcode[] $shortcodes |
||
116 | */ |
||
117 | public $shortcodes = null; |
||
118 | |||
119 | /** |
||
120 | * $widgets |
||
121 | * @access public |
||
122 | * @var WP_Widget[] $widgets |
||
123 | */ |
||
124 | public $widgets = null; |
||
125 | |||
126 | /** |
||
127 | * $non_abstract_db_models |
||
128 | * @access public |
||
129 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
130 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
131 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
132 | * classnames (eg "EEM_Event") |
||
133 | */ |
||
134 | public $non_abstract_db_models = array(); |
||
135 | |||
136 | /** |
||
137 | * $i18n_js_strings - internationalization for JS strings |
||
138 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
139 | * in js file: var translatedString = eei18n.string_key; |
||
140 | * |
||
141 | * @access public |
||
142 | * @var array |
||
143 | */ |
||
144 | public static $i18n_js_strings = array(); |
||
145 | |||
146 | /** |
||
147 | * $main_file - path to espresso.php |
||
148 | * |
||
149 | * @access public |
||
150 | * @var array |
||
151 | */ |
||
152 | public $main_file; |
||
153 | |||
154 | /** |
||
155 | * array of ReflectionClass objects where the key is the class name |
||
156 | * |
||
157 | * @access public |
||
158 | * @var ReflectionClass[] |
||
159 | */ |
||
160 | public $_reflectors; |
||
161 | |||
162 | |||
163 | |||
164 | /** |
||
165 | * @singleton method used to instantiate class object |
||
166 | * @access public |
||
167 | * @return EE_Registry instance |
||
168 | */ |
||
169 | public static function instance() { |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | *protected constructor to prevent direct creation |
||
181 | * @Constructor |
||
182 | * @access protected |
||
183 | * @return \EE_Registry |
||
|
|||
184 | */ |
||
185 | protected function __construct() { |
||
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * init |
||
216 | * |
||
217 | * @access public |
||
218 | * @return void |
||
219 | */ |
||
220 | public function init() { |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * localize_i18n_js_strings |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public static function localize_i18n_js_strings() { |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * @param mixed string | EED_Module $module |
||
250 | */ |
||
251 | public function add_module( $module ) { |
||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * @param string $module_name |
||
267 | * @return mixed EED_Module | NULL |
||
268 | */ |
||
269 | public function get_module( $module_name = '' ) { |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * loads core classes - must be singletons |
||
277 | * |
||
278 | * @access public |
||
279 | * @param string $class_name - simple class name ie: session |
||
280 | * @param mixed $arguments |
||
281 | * @param bool $load_only |
||
282 | * @param bool $resolve_dependencies |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function load_core( $class_name, $arguments = array(), $load_only = false, $resolve_dependencies = false ) { |
||
298 | |||
299 | |||
300 | |||
301 | /** |
||
302 | * loads service classes |
||
303 | * |
||
304 | * @access public |
||
305 | * @param string $class_name - simple class name ie: session |
||
306 | * @param mixed $arguments |
||
307 | * @param bool $load_only |
||
308 | * @return mixed |
||
309 | */ |
||
310 | public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
||
320 | |||
321 | |||
322 | |||
323 | /** |
||
324 | * loads data_migration_scripts |
||
325 | * |
||
326 | * @access public |
||
327 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
328 | * @param mixed $arguments |
||
329 | * @return EE_Data_Migration_Script_Base |
||
330 | */ |
||
331 | public function load_dms( $class_name, $arguments = array() ) { |
||
335 | |||
336 | |||
337 | |||
338 | /** |
||
339 | * loads object creating classes - must be singletons |
||
340 | * |
||
341 | * @param string $class_name - simple class name ie: attendee |
||
342 | * @param mixed $arguments - an array of arguments to pass to the class |
||
343 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
344 | * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
||
345 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
346 | * @return EE_Base_Class |
||
347 | */ |
||
348 | public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
||
357 | |||
358 | |||
359 | |||
360 | /** |
||
361 | * loads helper classes - must be singletons |
||
362 | * |
||
363 | * @param string $class_name - simple class name ie: price |
||
364 | * @param mixed $arguments |
||
365 | * @param bool $load_only |
||
366 | * @return EEH_Base |
||
367 | */ |
||
368 | public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * loads core classes - must be singletons |
||
378 | * |
||
379 | * @access public |
||
380 | * @param string $class_name - simple class name ie: session |
||
381 | * @param mixed $arguments |
||
382 | * @param bool $load_only |
||
383 | * @return mixed |
||
384 | */ |
||
385 | public function load_lib( $class_name, $arguments = array(), $load_only = false ) { |
||
396 | |||
397 | |||
398 | |||
399 | /** |
||
400 | * loads model classes - must be singletons |
||
401 | * |
||
402 | * @param string $class_name - simple class name ie: price |
||
403 | * @param mixed $arguments |
||
404 | * @param bool $load_only |
||
405 | * @return EEM_Base |
||
406 | */ |
||
407 | public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * loads model classes - must be singletons |
||
420 | * |
||
421 | * @param string $class_name - simple class name ie: price |
||
422 | * @param mixed $arguments |
||
423 | * @param bool $load_only |
||
424 | * @return mixed |
||
425 | */ |
||
426 | public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
||
436 | |||
437 | |||
438 | |||
439 | /** |
||
440 | * Determines if $model_name is the name of an actual EE model. |
||
441 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
442 | * @return boolean |
||
443 | */ |
||
444 | public function is_model_name( $model_name ) { |
||
447 | |||
448 | |||
449 | |||
450 | /** |
||
451 | * generic class loader |
||
452 | * |
||
453 | * @param string $path_to_file - directory path to file location, not including filename |
||
454 | * @param string $file_name - file name ie: my_file.php, including extension |
||
455 | * @param string $type - file type - core? class? helper? model? |
||
456 | * @param mixed $arguments |
||
457 | * @param bool $load_only |
||
458 | * @return mixed |
||
459 | */ |
||
460 | public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
||
464 | |||
465 | |||
466 | |||
467 | /** |
||
468 | * load_addon |
||
469 | * |
||
470 | * @param string $path_to_file - directory path to file location, not including filename |
||
471 | * @param string $class_name - full class name ie: My_Class |
||
472 | * @param string $type - file type - core? class? helper? model? |
||
473 | * @param mixed $arguments |
||
474 | * @param bool $load_only |
||
475 | * @return EE_Addon |
||
476 | */ |
||
477 | public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
||
481 | |||
482 | |||
483 | |||
484 | /** |
||
485 | * loads and tracks classes |
||
486 | * |
||
487 | * @param array $file_paths |
||
488 | * @param string $class_prefix - EE or EEM or... ??? |
||
489 | * @param bool|string $class_name - $class name |
||
490 | * @param string $type - file type - core? class? helper? model? |
||
491 | * @param mixed $arguments - an argument or array of arguments to pass to the class upon instantiation |
||
492 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
493 | * @param bool $cache |
||
494 | * @param bool $load_only |
||
495 | * @param bool $resolve_dependencies |
||
496 | * @return null|object |
||
497 | * @internal param string $file_path - file path including file name |
||
498 | */ |
||
499 | private function _load( |
||
534 | |||
535 | |||
536 | |||
537 | /** |
||
538 | * _get_cached_class |
||
539 | * |
||
540 | * attempts to find a cached version of the requested class |
||
541 | * by looking in the following places: |
||
542 | * $this->{$class_abbreviation} ie: $this->CART |
||
543 | * $this->{$class_name} ie: $this->Some_Class |
||
544 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
545 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
546 | * |
||
547 | * @access protected |
||
548 | * @param string $class_name |
||
549 | * @param string $class_prefix |
||
550 | * @return null|object |
||
551 | */ |
||
552 | protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
||
571 | |||
572 | |||
573 | |||
574 | /** |
||
575 | * _resolve_path |
||
576 | * |
||
577 | * attempts to find a full valid filepath for the requested class. |
||
578 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
579 | * then returns that path if the target file has been found and is readable |
||
580 | * |
||
581 | * @access protected |
||
582 | * @param string $class_name |
||
583 | * @param string $type |
||
584 | * @param array $file_paths |
||
585 | * @return string | bool |
||
586 | */ |
||
587 | protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
||
605 | |||
606 | |||
607 | |||
608 | /** |
||
609 | * _require_file |
||
610 | * |
||
611 | * basically just performs a require_once() |
||
612 | * but with some error handling |
||
613 | * |
||
614 | * @access protected |
||
615 | * @param string $path |
||
616 | * @param string $class_name |
||
617 | * @param string $type |
||
618 | * @param array $file_paths |
||
619 | * @return void |
||
620 | * @throws \EE_Error |
||
621 | */ |
||
622 | protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
||
656 | |||
657 | |||
658 | |||
659 | /** |
||
660 | * _create_object |
||
661 | * Attempts to instantiate the requested class via any of the |
||
662 | * commonly used instantiation methods employed throughout EE. |
||
663 | * The priority for instantiation is as follows: |
||
664 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
665 | * - model objects via their 'new_instance_from_db' method |
||
666 | * - model objects via their 'new_instance' method |
||
667 | * - "singleton" classes" via their 'instance' method |
||
668 | * - standard instantiable classes via their __constructor |
||
669 | * Prior to instantiation, if the $resolve_dependencies flag is set to true, |
||
670 | * then the constructor for the requested class will be examined to determine |
||
671 | * if any dependencies exist, and if they can be injected. |
||
672 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
673 | * |
||
674 | * @access protected |
||
675 | * @param string $class_name |
||
676 | * @param array $arguments |
||
677 | * @param string $type |
||
678 | * @param bool $from_db |
||
679 | * @param bool $load_only |
||
680 | * @param bool $resolve_dependencies |
||
681 | * @return null | object |
||
682 | * @throws \EE_Error |
||
683 | */ |
||
684 | protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false, $load_only = false, $resolve_dependencies = true ) { |
||
734 | |||
735 | |||
736 | |||
737 | /** |
||
738 | * getReflectionClass |
||
739 | * |
||
740 | * checks if a ReflectionClass object has already been generated for a class |
||
741 | * and returns that instead of creating a new one |
||
742 | * |
||
743 | * @access public |
||
744 | * @param string $class_name |
||
745 | * @return ReflectionClass |
||
746 | */ |
||
747 | public function get_ReflectionClass( $class_name ) { |
||
756 | |||
757 | |||
758 | |||
759 | /** |
||
760 | * _resolve_dependencies |
||
761 | * |
||
762 | * examines the constructor for the requested class to determine |
||
763 | * if any dependencies exist, and if they can be injected. |
||
764 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
765 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
766 | * For example: |
||
767 | * if attempting to load a class "Foo" with the following constructor: |
||
768 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
769 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
770 | * but only IF they are NOT already present in the incoming arguments array, |
||
771 | * and the correct classes can be loaded |
||
772 | * |
||
773 | * @access protected |
||
774 | * @param ReflectionClass $reflector |
||
775 | * @param string $class_name |
||
776 | * @param array $arguments |
||
777 | * @return array |
||
778 | */ |
||
779 | protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
||
838 | |||
839 | |||
840 | |||
841 | /** |
||
842 | * _set_cached_class |
||
843 | * |
||
844 | * attempts to cache the instantiated class locally |
||
845 | * in one of the following places, in the following order: |
||
846 | * $this->{class_abbreviation} ie: $this->CART |
||
847 | * $this->{$class_name} ie: $this->Some_Class |
||
848 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
849 | * $this->LIB->{$class_name} + ie: $this->LIB->Some_Class + |
||
850 | * + only classes that are NOT model objects with the $cache flag set to true |
||
851 | * will be cached under LIB (libraries) |
||
852 | * |
||
853 | * @access protected |
||
854 | * @param object $class_obj |
||
855 | * @param string $class_name |
||
856 | * @param string $class_prefix |
||
857 | * @param bool $from_db |
||
858 | * @param bool $cache |
||
859 | * @return void |
||
860 | */ |
||
861 | protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false, $cache = true ) { |
||
874 | |||
875 | |||
876 | |||
877 | /** |
||
878 | * Gets the addon by its name/slug (not classname. For that, just |
||
879 | * use the classname as the property name on EE_Config::instance()->addons) |
||
880 | * @param string $name |
||
881 | * @return EE_Addon |
||
882 | */ |
||
883 | public function get_addon_by_name( $name ) { |
||
891 | |||
892 | |||
893 | |||
894 | /** |
||
895 | * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
||
896 | * |
||
897 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
898 | */ |
||
899 | public function get_addons_by_name() { |
||
906 | |||
907 | |||
908 | |||
909 | /** |
||
910 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
911 | * a stale copy of it around |
||
912 | * |
||
913 | * @param string $model_name |
||
914 | * @return \EEM_Base |
||
915 | * @throws \EE_Error |
||
916 | */ |
||
917 | public function reset_model( $model_name ) { |
||
928 | |||
929 | |||
930 | |||
931 | /** |
||
932 | * Resets the registry and everything in it (eventually, getting it to properly |
||
933 | * reset absolutely everything will probably be tricky. right now it just resets |
||
934 | * the config, data migration manager, and the models) |
||
935 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
936 | * the Registry to its state at the beginning of the request |
||
937 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
938 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
939 | * currently reinstantiate the singletons at the moment) |
||
940 | * @return EE_Registry |
||
941 | */ |
||
942 | public static function reset( $hard = false, $reinstantiate = true ) { |
||
954 | |||
955 | |||
956 | |||
957 | /** |
||
958 | * @override magic methods |
||
959 | * @return void |
||
960 | */ |
||
961 | final function __destruct() { |
||
963 | |||
964 | |||
965 | |||
966 | /** |
||
967 | * @param $a |
||
968 | * @param $b |
||
969 | */ |
||
970 | final function __call( $a, $b ) { |
||
972 | |||
973 | |||
974 | |||
975 | /** |
||
976 | * @param $a |
||
977 | */ |
||
978 | final function __get( $a ) { |
||
980 | |||
981 | |||
982 | |||
983 | /** |
||
984 | * @param $a |
||
985 | * @param $b |
||
986 | */ |
||
987 | final function __set( $a, $b ) { |
||
989 | |||
990 | |||
991 | |||
992 | /** |
||
993 | * @param $a |
||
994 | */ |
||
995 | final function __isset( $a ) { |
||
997 | |||
998 | |||
999 | |||
1000 | /** |
||
1001 | * @param $a |
||
1002 | */ |
||
1003 | final function __unset( $a ) { |
||
1005 | |||
1006 | |||
1007 | |||
1008 | /** |
||
1009 | * @return array |
||
1010 | */ |
||
1011 | final function __sleep() { |
||
1014 | |||
1015 | |||
1016 | |||
1017 | final function __wakeup() { |
||
1019 | |||
1020 | |||
1021 | |||
1022 | /** |
||
1023 | * @return string |
||
1024 | */ |
||
1025 | final function __toString() { |
||
1028 | |||
1029 | |||
1030 | |||
1031 | final function __invoke() { |
||
1033 | |||
1034 | |||
1035 | |||
1036 | final function __set_state() { |
||
1038 | |||
1039 | |||
1040 | |||
1041 | final function __clone() { |
||
1043 | |||
1044 | |||
1045 | |||
1046 | /** |
||
1047 | * @param $a |
||
1048 | * @param $b |
||
1049 | */ |
||
1050 | final static function __callStatic( $a, $b ) { |
||
1052 | |||
1053 | /** |
||
1054 | * Gets all the custom post type models defined |
||
1055 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1056 | */ |
||
1057 | public function cpt_models() { |
||
1066 | |||
1067 | |||
1068 | |||
1069 | } |
||
1070 | // End of file EE_Registry.core.php |
||
1072 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.