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 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
84 | * @access public |
||
85 | * @var EE_Addon[] |
||
86 | */ |
||
87 | public $addons = null; |
||
88 | |||
89 | /** |
||
90 | * $models |
||
91 | * @access public |
||
92 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
93 | */ |
||
94 | public $models = array(); |
||
95 | |||
96 | /** |
||
97 | * $modules |
||
98 | * @access public |
||
99 | * @var EED_Module[] $modules |
||
100 | */ |
||
101 | public $modules = null; |
||
102 | |||
103 | /** |
||
104 | * $shortcodes |
||
105 | * @access public |
||
106 | * @var EES_Shortcode[] $shortcodes |
||
107 | */ |
||
108 | public $shortcodes = null; |
||
109 | |||
110 | /** |
||
111 | * $widgets |
||
112 | * @access public |
||
113 | * @var WP_Widget[] $widgets |
||
114 | */ |
||
115 | public $widgets = null; |
||
116 | |||
117 | /** |
||
118 | * $non_abstract_db_models |
||
119 | * @access public |
||
120 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
121 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
122 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
123 | * classnames (eg "EEM_Event") |
||
124 | */ |
||
125 | public $non_abstract_db_models = array(); |
||
126 | |||
127 | /** |
||
128 | * $i18n_js_strings - internationalization for JS strings |
||
129 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
130 | * in js file: var translatedString = eei18n.string_key; |
||
131 | * |
||
132 | * @access public |
||
133 | * @var array |
||
134 | */ |
||
135 | public static $i18n_js_strings = array(); |
||
136 | |||
137 | /** |
||
138 | * $main_file - path to espresso.php |
||
139 | * |
||
140 | * @access public |
||
141 | * @var array |
||
142 | */ |
||
143 | public $main_file; |
||
144 | |||
145 | /** |
||
146 | * array of ReflectionClass objects where the key is the class name |
||
147 | * |
||
148 | * @access public |
||
149 | * @var ReflectionClass[] |
||
150 | */ |
||
151 | public $_reflectors; |
||
152 | |||
153 | |||
154 | |||
155 | /** |
||
156 | * @singleton method used to instantiate class object |
||
157 | * @access public |
||
158 | * @return EE_Registry instance |
||
159 | */ |
||
160 | public static function instance() { |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | *protected constructor to prevent direct creation |
||
172 | * @Constructor |
||
173 | * @access protected |
||
174 | * @return \EE_Registry |
||
|
|||
175 | */ |
||
176 | protected function __construct() { |
||
228 | |||
229 | |||
230 | |||
231 | /** |
||
232 | * init |
||
233 | * |
||
234 | * @access public |
||
235 | * @return void |
||
236 | */ |
||
237 | public function init() { |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * localize_i18n_js_strings |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | public static function localize_i18n_js_strings() { |
||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * @param mixed string | EED_Module $module |
||
267 | */ |
||
268 | public function add_module( $module ) { |
||
279 | |||
280 | |||
281 | |||
282 | /** |
||
283 | * @param string $module_name |
||
284 | * @return mixed EED_Module | NULL |
||
285 | */ |
||
286 | public function get_module( $module_name = '' ) { |
||
289 | |||
290 | |||
291 | |||
292 | /** |
||
293 | * loads core classes - must be singletons |
||
294 | * |
||
295 | * @access public |
||
296 | * @param string $class_name - simple class name ie: session |
||
297 | * @param mixed $arguments |
||
298 | * @param bool $load_only |
||
299 | * @param bool $resolve_dependencies |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function load_core( $class_name, $arguments = array(), $load_only = false, $resolve_dependencies = false ) { |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * loads service classes |
||
320 | * |
||
321 | * @access public |
||
322 | * @param string $class_name - simple class name ie: session |
||
323 | * @param mixed $arguments |
||
324 | * @param bool $load_only |
||
325 | * @return mixed |
||
326 | */ |
||
327 | public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * loads data_migration_scripts |
||
342 | * |
||
343 | * @access public |
||
344 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
345 | * @param mixed $arguments |
||
346 | * @return EE_Data_Migration_Script_Base |
||
347 | */ |
||
348 | public function load_dms( $class_name, $arguments = array() ) { |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * loads object creating classes - must be singletons |
||
357 | * |
||
358 | * @param string $class_name - simple class name ie: attendee |
||
359 | * @param mixed $arguments - an array of arguments to pass to the class |
||
360 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
361 | * @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) |
||
362 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
363 | * @return EE_Base_Class |
||
364 | */ |
||
365 | public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
||
374 | |||
375 | |||
376 | |||
377 | /** |
||
378 | * loads helper classes - must be singletons |
||
379 | * |
||
380 | * @param string $class_name - simple class name ie: price |
||
381 | * @param mixed $arguments |
||
382 | * @param bool $load_only |
||
383 | * @return EEH_Base |
||
384 | */ |
||
385 | public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
||
390 | |||
391 | |||
392 | |||
393 | /** |
||
394 | * loads core classes - must be singletons |
||
395 | * |
||
396 | * @access public |
||
397 | * @param string $class_name - simple class name ie: session |
||
398 | * @param mixed $arguments |
||
399 | * @param bool $load_only |
||
400 | * @return mixed |
||
401 | */ |
||
402 | public function load_lib( $class_name, $arguments = array(), $load_only = false ) { |
||
413 | |||
414 | |||
415 | |||
416 | /** |
||
417 | * loads model classes - must be singletons |
||
418 | * |
||
419 | * @param string $class_name - simple class name ie: price |
||
420 | * @param mixed $arguments |
||
421 | * @param bool $load_only |
||
422 | * @return EEM_Base |
||
423 | */ |
||
424 | public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
||
432 | |||
433 | |||
434 | |||
435 | /** |
||
436 | * loads model classes - must be singletons |
||
437 | * |
||
438 | * @param string $class_name - simple class name ie: price |
||
439 | * @param mixed $arguments |
||
440 | * @param bool $load_only |
||
441 | * @return mixed |
||
442 | */ |
||
443 | public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
||
453 | |||
454 | |||
455 | |||
456 | /** |
||
457 | * Determines if $model_name is the name of an actual EE model. |
||
458 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
459 | * @return boolean |
||
460 | */ |
||
461 | public function is_model_name( $model_name ) { |
||
464 | |||
465 | |||
466 | |||
467 | /** |
||
468 | * generic class loader |
||
469 | * |
||
470 | * @param string $path_to_file - directory path to file location, not including filename |
||
471 | * @param string $file_name - file name ie: my_file.php, including extension |
||
472 | * @param string $type - file type - core? class? helper? model? |
||
473 | * @param mixed $arguments |
||
474 | * @param bool $load_only |
||
475 | * @return mixed |
||
476 | */ |
||
477 | public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
||
481 | |||
482 | |||
483 | |||
484 | /** |
||
485 | * load_addon |
||
486 | * |
||
487 | * @param string $path_to_file - directory path to file location, not including filename |
||
488 | * @param string $class_name - full class name ie: My_Class |
||
489 | * @param string $type - file type - core? class? helper? model? |
||
490 | * @param mixed $arguments |
||
491 | * @param bool $load_only |
||
492 | * @return EE_Addon |
||
493 | */ |
||
494 | public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
||
498 | |||
499 | |||
500 | |||
501 | /** |
||
502 | * loads and tracks classes |
||
503 | * |
||
504 | * @param array $file_paths |
||
505 | * @param string $class_prefix - EE or EEM or... ??? |
||
506 | * @param bool|string $class_name - $class name |
||
507 | * @param string $type - file type - core? class? helper? model? |
||
508 | * @param mixed $arguments - an argument or array of arguments to pass to the class upon instantiation |
||
509 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
510 | * @param bool $cache |
||
511 | * @param bool $load_only |
||
512 | * @param bool $resolve_dependencies |
||
513 | * @return null|object |
||
514 | * @internal param string $file_path - file path including file name |
||
515 | */ |
||
516 | private function _load( |
||
551 | |||
552 | |||
553 | |||
554 | /** |
||
555 | * _get_cached_class |
||
556 | * |
||
557 | * attempts to find a cached version of the requested class |
||
558 | * by looking in the following places: |
||
559 | * $this->{$class_abbreviation} ie: $this->CART |
||
560 | * $this->{$class_name} ie: $this->Some_Class |
||
561 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
562 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
563 | * |
||
564 | * @access protected |
||
565 | * @param string $class_name |
||
566 | * @param string $class_prefix |
||
567 | * @return null|object |
||
568 | */ |
||
569 | protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
||
588 | |||
589 | |||
590 | |||
591 | /** |
||
592 | * _resolve_path |
||
593 | * |
||
594 | * attempts to find a full valid filepath for the requested class. |
||
595 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
596 | * then returns that path if the target file has been found and is readable |
||
597 | * |
||
598 | * @access protected |
||
599 | * @param string $class_name |
||
600 | * @param string $type |
||
601 | * @param array $file_paths |
||
602 | * @return string | bool |
||
603 | */ |
||
604 | protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
||
622 | |||
623 | |||
624 | |||
625 | /** |
||
626 | * _require_file |
||
627 | * |
||
628 | * basically just performs a require_once() |
||
629 | * but with some error handling |
||
630 | * |
||
631 | * @access protected |
||
632 | * @param string $path |
||
633 | * @param string $class_name |
||
634 | * @param string $type |
||
635 | * @param array $file_paths |
||
636 | * @return void |
||
637 | * @throws \EE_Error |
||
638 | */ |
||
639 | protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
||
673 | |||
674 | |||
675 | |||
676 | /** |
||
677 | * _create_object |
||
678 | * Attempts to instantiate the requested class via any of the |
||
679 | * commonly used instantiation methods employed throughout EE. |
||
680 | * The priority for instantiation is as follows: |
||
681 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
682 | * - model objects via their 'new_instance_from_db' method |
||
683 | * - model objects via their 'new_instance' method |
||
684 | * - "singleton" classes" via their 'instance' method |
||
685 | * - standard instantiable classes via their __constructor |
||
686 | * Prior to instantiation, if the $resolve_dependencies flag is set to true, |
||
687 | * then the constructor for the requested class will be examined to determine |
||
688 | * if any dependencies exist, and if they can be injected. |
||
689 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
690 | * |
||
691 | * @access protected |
||
692 | * @param string $class_name |
||
693 | * @param array $arguments |
||
694 | * @param string $type |
||
695 | * @param bool $from_db |
||
696 | * @param bool $load_only |
||
697 | * @param bool $resolve_dependencies |
||
698 | * @return null | object |
||
699 | * @throws \EE_Error |
||
700 | */ |
||
701 | protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false, $load_only = false, $resolve_dependencies = true ) { |
||
751 | |||
752 | |||
753 | |||
754 | /** |
||
755 | * getReflectionClass |
||
756 | * |
||
757 | * checks if a ReflectionClass object has already been generated for a class |
||
758 | * and returns that instead of creating a new one |
||
759 | * |
||
760 | * @access public |
||
761 | * @param string $class_name |
||
762 | * @return ReflectionClass |
||
763 | */ |
||
764 | public function get_ReflectionClass( $class_name ) { |
||
773 | |||
774 | |||
775 | |||
776 | /** |
||
777 | * _resolve_dependencies |
||
778 | * |
||
779 | * examines the constructor for the requested class to determine |
||
780 | * if any dependencies exist, and if they can be injected. |
||
781 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
782 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
783 | * For example: |
||
784 | * if attempting to load a class "Foo" with the following constructor: |
||
785 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
786 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
787 | * but only IF they are NOT already present in the incoming arguments array, |
||
788 | * and the correct classes can be loaded |
||
789 | * |
||
790 | * @access protected |
||
791 | * @param ReflectionClass $reflector |
||
792 | * @param string $class_name |
||
793 | * @param array $arguments |
||
794 | * @return array |
||
795 | */ |
||
796 | protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
||
855 | |||
856 | |||
857 | |||
858 | /** |
||
859 | * _set_cached_class |
||
860 | * |
||
861 | * attempts to cache the instantiated class locally |
||
862 | * in one of the following places, in the following order: |
||
863 | * $this->{class_abbreviation} ie: $this->CART |
||
864 | * $this->{$class_name} ie: $this->Some_Class |
||
865 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
866 | * $this->LIB->{$class_name} + ie: $this->LIB->Some_Class + |
||
867 | * + only classes that are NOT model objects with the $cache flag set to true |
||
868 | * will be cached under LIB (libraries) |
||
869 | * |
||
870 | * @access protected |
||
871 | * @param object $class_obj |
||
872 | * @param string $class_name |
||
873 | * @param string $class_prefix |
||
874 | * @param bool $from_db |
||
875 | * @param bool $cache |
||
876 | * @return void |
||
877 | */ |
||
878 | protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false, $cache = true ) { |
||
891 | |||
892 | |||
893 | |||
894 | /** |
||
895 | * Gets the addon by its name/slug (not classname. For that, just |
||
896 | * use the classname as the property name on EE_Config::instance()->addons) |
||
897 | * @param string $name |
||
898 | * @return EE_Addon |
||
899 | */ |
||
900 | public function get_addon_by_name( $name ) { |
||
908 | |||
909 | |||
910 | |||
911 | /** |
||
912 | * 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} |
||
913 | * |
||
914 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
915 | */ |
||
916 | public function get_addons_by_name() { |
||
923 | |||
924 | |||
925 | |||
926 | /** |
||
927 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
928 | * a stale copy of it around |
||
929 | * |
||
930 | * @param string $model_name |
||
931 | * @return \EEM_Base |
||
932 | * @throws \EE_Error |
||
933 | */ |
||
934 | public function reset_model( $model_name ) { |
||
945 | |||
946 | |||
947 | |||
948 | /** |
||
949 | * Resets the registry and everything in it (eventually, getting it to properly |
||
950 | * reset absolutely everything will probably be tricky. right now it just resets |
||
951 | * the config, data migration manager, and the models) |
||
952 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
953 | * the Registry to its state at the beginning of the request |
||
954 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
955 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
956 | * currently reinstantiate the singletons at the moment) |
||
957 | * @return EE_Registry |
||
958 | */ |
||
959 | public static function reset( $hard = false, $reinstantiate = true ) { |
||
971 | |||
972 | |||
973 | |||
974 | /** |
||
975 | * @override magic methods |
||
976 | * @return void |
||
977 | */ |
||
978 | final function __destruct() { |
||
980 | |||
981 | |||
982 | |||
983 | /** |
||
984 | * @param $a |
||
985 | * @param $b |
||
986 | */ |
||
987 | final function __call( $a, $b ) { |
||
989 | |||
990 | |||
991 | |||
992 | /** |
||
993 | * @param $a |
||
994 | */ |
||
995 | final function __get( $a ) { |
||
997 | |||
998 | |||
999 | |||
1000 | /** |
||
1001 | * @param $a |
||
1002 | * @param $b |
||
1003 | */ |
||
1004 | final function __set( $a, $b ) { |
||
1006 | |||
1007 | |||
1008 | |||
1009 | /** |
||
1010 | * @param $a |
||
1011 | */ |
||
1012 | final function __isset( $a ) { |
||
1014 | |||
1015 | |||
1016 | |||
1017 | /** |
||
1018 | * @param $a |
||
1019 | */ |
||
1020 | final function __unset( $a ) { |
||
1022 | |||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * @return array |
||
1027 | */ |
||
1028 | final function __sleep() { |
||
1031 | |||
1032 | |||
1033 | |||
1034 | final function __wakeup() { |
||
1036 | |||
1037 | |||
1038 | |||
1039 | /** |
||
1040 | * @return string |
||
1041 | */ |
||
1042 | final function __toString() { |
||
1045 | |||
1046 | |||
1047 | |||
1048 | final function __invoke() { |
||
1050 | |||
1051 | |||
1052 | |||
1053 | final function __set_state() { |
||
1055 | |||
1056 | |||
1057 | |||
1058 | final function __clone() { |
||
1060 | |||
1061 | |||
1062 | |||
1063 | /** |
||
1064 | * @param $a |
||
1065 | * @param $b |
||
1066 | */ |
||
1067 | final static function __callStatic( $a, $b ) { |
||
1069 | |||
1070 | /** |
||
1071 | * Gets all the custom post type models defined |
||
1072 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1073 | */ |
||
1074 | public function cpt_models() { |
||
1083 | |||
1084 | |||
1085 | |||
1086 | } |
||
1087 | // End of file EE_Registry.core.php |
||
1089 |
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.