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 |
||
| 23 | class EE_Registry implements ResettableInterface |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var EE_Registry $_instance |
||
| 28 | */ |
||
| 29 | private static $_instance; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var EE_Dependency_Map $_dependency_map |
||
| 33 | */ |
||
| 34 | protected $_dependency_map; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array $_class_abbreviations |
||
| 38 | */ |
||
| 39 | protected $_class_abbreviations = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var CommandBusInterface $BUS |
||
| 43 | */ |
||
| 44 | public $BUS; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var EE_Cart $CART |
||
| 48 | */ |
||
| 49 | public $CART; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var EE_Config $CFG |
||
| 53 | */ |
||
| 54 | public $CFG; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EE_Network_Config $NET_CFG |
||
| 58 | */ |
||
| 59 | public $NET_CFG; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * StdClass object for storing library classes in |
||
| 63 | * |
||
| 64 | * @var StdClass $LIB |
||
| 65 | */ |
||
| 66 | public $LIB; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var EE_Request_Handler $REQ |
||
| 70 | */ |
||
| 71 | public $REQ; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var EE_Session $SSN |
||
| 75 | */ |
||
| 76 | public $SSN; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @since 4.5.0 |
||
| 80 | * @var EE_Capabilities $CAP |
||
| 81 | */ |
||
| 82 | public $CAP; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @since 4.9.0 |
||
| 86 | * @var EE_Message_Resource_Manager $MRM |
||
| 87 | */ |
||
| 88 | public $MRM; |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @var Registry $AssetsRegistry |
||
| 93 | */ |
||
| 94 | public $AssetsRegistry; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * StdClass object for holding addons which have registered themselves to work with EE core |
||
| 98 | * |
||
| 99 | * @var EE_Addon[] $addons |
||
| 100 | */ |
||
| 101 | public $addons; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
| 105 | * |
||
| 106 | * @var EEM_Base[] $models |
||
| 107 | */ |
||
| 108 | public $models = array(); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var EED_Module[] $modules |
||
| 112 | */ |
||
| 113 | public $modules; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var EES_Shortcode[] $shortcodes |
||
| 117 | */ |
||
| 118 | public $shortcodes; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var WP_Widget[] $widgets |
||
| 122 | */ |
||
| 123 | public $widgets; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
| 127 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
| 128 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
| 129 | * classnames (eg "EEM_Event") |
||
| 130 | * |
||
| 131 | * @var array $non_abstract_db_models |
||
| 132 | */ |
||
| 133 | public $non_abstract_db_models = array(); |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * internationalization for JS strings |
||
| 138 | * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
||
| 139 | * in js file: var translatedString = eei18n.string_key; |
||
| 140 | * |
||
| 141 | * @var array $i18n_js_strings |
||
| 142 | */ |
||
| 143 | public static $i18n_js_strings = array(); |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * $main_file - path to espresso.php |
||
| 148 | * |
||
| 149 | * @var array $main_file |
||
| 150 | */ |
||
| 151 | public $main_file; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * array of ReflectionClass objects where the key is the class name |
||
| 155 | * |
||
| 156 | * @var ReflectionClass[] $_reflectors |
||
| 157 | */ |
||
| 158 | public $_reflectors; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
| 162 | * |
||
| 163 | * @var boolean $_cache_on |
||
| 164 | */ |
||
| 165 | protected $_cache_on = true; |
||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @singleton method used to instantiate class object |
||
| 171 | * @param EE_Dependency_Map $dependency_map |
||
| 172 | * @return EE_Registry instance |
||
| 173 | * @throws InvalidArgumentException |
||
| 174 | * @throws InvalidInterfaceException |
||
| 175 | * @throws InvalidDataTypeException |
||
| 176 | */ |
||
| 177 | public static function instance(EE_Dependency_Map $dependency_map = null) |
||
| 178 | { |
||
| 179 | // check if class object is instantiated |
||
| 180 | if (! self::$_instance instanceof EE_Registry) { |
||
| 181 | self::$_instance = new self($dependency_map); |
||
|
|
|||
| 182 | } |
||
| 183 | return self::$_instance; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * protected constructor to prevent direct creation |
||
| 190 | * |
||
| 191 | * @Constructor |
||
| 192 | * @param EE_Dependency_Map $dependency_map |
||
| 193 | * @throws InvalidDataTypeException |
||
| 194 | * @throws InvalidInterfaceException |
||
| 195 | * @throws InvalidArgumentException |
||
| 196 | */ |
||
| 197 | protected function __construct(EE_Dependency_Map $dependency_map) |
||
| 198 | { |
||
| 199 | $this->_dependency_map = $dependency_map; |
||
| 200 | // $registry_container = new RegistryContainer(); |
||
| 201 | $this->LIB = new RegistryContainer(); |
||
| 202 | $this->addons = new RegistryContainer(); |
||
| 203 | $this->modules = new RegistryContainer(); |
||
| 204 | $this->shortcodes = new RegistryContainer(); |
||
| 205 | $this->widgets = new RegistryContainer(); |
||
| 206 | add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * initialize |
||
| 213 | * |
||
| 214 | * @throws EE_Error |
||
| 215 | * @throws ReflectionException |
||
| 216 | */ |
||
| 217 | public function initialize() |
||
| 218 | { |
||
| 219 | $this->_class_abbreviations = apply_filters( |
||
| 220 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
| 221 | array( |
||
| 222 | 'EE_Config' => 'CFG', |
||
| 223 | 'EE_Session' => 'SSN', |
||
| 224 | 'EE_Capabilities' => 'CAP', |
||
| 225 | 'EE_Cart' => 'CART', |
||
| 226 | 'EE_Network_Config' => 'NET_CFG', |
||
| 227 | 'EE_Request_Handler' => 'REQ', |
||
| 228 | 'EE_Message_Resource_Manager' => 'MRM', |
||
| 229 | 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
||
| 230 | 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
||
| 231 | ) |
||
| 232 | ); |
||
| 233 | $this->load_core('Base', array(), true); |
||
| 234 | // add our request and response objects to the cache |
||
| 235 | $request_loader = $this->_dependency_map->class_loader( |
||
| 236 | 'EventEspresso\core\services\request\Request' |
||
| 237 | ); |
||
| 238 | $this->_set_cached_class( |
||
| 239 | $request_loader(), |
||
| 240 | 'EventEspresso\core\services\request\Request' |
||
| 241 | ); |
||
| 242 | $response_loader = $this->_dependency_map->class_loader( |
||
| 243 | 'EventEspresso\core\services\request\Response' |
||
| 244 | ); |
||
| 245 | $this->_set_cached_class( |
||
| 246 | $response_loader(), |
||
| 247 | 'EventEspresso\core\services\request\Response' |
||
| 248 | ); |
||
| 249 | add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function init() |
||
| 258 | { |
||
| 259 | // Get current page protocol |
||
| 260 | $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
||
| 261 | // Output admin-ajax.php URL with same protocol as current page |
||
| 262 | self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
||
| 263 | self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
||
| 264 | } |
||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * localize_i18n_js_strings |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public static function localize_i18n_js_strings() |
||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * @param mixed string | EED_Module $module |
||
| 288 | * @throws EE_Error |
||
| 289 | * @throws ReflectionException |
||
| 290 | */ |
||
| 291 | public function add_module($module) |
||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $module_name |
||
| 308 | * @return mixed EED_Module | NULL |
||
| 309 | */ |
||
| 310 | public function get_module($module_name = '') |
||
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * loads core classes - must be singletons |
||
| 321 | * |
||
| 322 | * @param string $class_name - simple class name ie: session |
||
| 323 | * @param mixed $arguments |
||
| 324 | * @param bool $load_only |
||
| 325 | * @return mixed |
||
| 326 | * @throws EE_Error |
||
| 327 | * @throws ReflectionException |
||
| 328 | */ |
||
| 329 | public function load_core($class_name, $arguments = array(), $load_only = false) |
||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * loads service classes |
||
| 360 | * |
||
| 361 | * @param string $class_name - simple class name ie: session |
||
| 362 | * @param mixed $arguments |
||
| 363 | * @param bool $load_only |
||
| 364 | * @return mixed |
||
| 365 | * @throws EE_Error |
||
| 366 | * @throws ReflectionException |
||
| 367 | */ |
||
| 368 | public function load_service($class_name, $arguments = array(), $load_only = false) |
||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * loads data_migration_scripts |
||
| 393 | * |
||
| 394 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
| 395 | * @param mixed $arguments |
||
| 396 | * @return EE_Data_Migration_Script_Base|mixed |
||
| 397 | * @throws EE_Error |
||
| 398 | * @throws ReflectionException |
||
| 399 | */ |
||
| 400 | public function load_dms($class_name, $arguments = array()) |
||
| 413 | |||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * loads object creating classes - must be singletons |
||
| 418 | * |
||
| 419 | * @param string $class_name - simple class name ie: attendee |
||
| 420 | * @param mixed $arguments - an array of arguments to pass to the class |
||
| 421 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
||
| 422 | * instantiate |
||
| 423 | * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
||
| 424 | * set this to FALSE (ie. when instantiating model objects from client in a loop) |
||
| 425 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
||
| 426 | * (default) |
||
| 427 | * @return EE_Base_Class | bool |
||
| 428 | * @throws EE_Error |
||
| 429 | * @throws ReflectionException |
||
| 430 | */ |
||
| 431 | public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * loads helper classes - must be singletons |
||
| 457 | * |
||
| 458 | * @param string $class_name - simple class name ie: price |
||
| 459 | * @param mixed $arguments |
||
| 460 | * @param bool $load_only |
||
| 461 | * @return EEH_Base | bool |
||
| 462 | * @throws EE_Error |
||
| 463 | * @throws ReflectionException |
||
| 464 | */ |
||
| 465 | public function load_helper($class_name, $arguments = array(), $load_only = true) |
||
| 481 | |||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * loads core classes - must be singletons |
||
| 486 | * |
||
| 487 | * @param string $class_name - simple class name ie: session |
||
| 488 | * @param mixed $arguments |
||
| 489 | * @param bool $load_only |
||
| 490 | * @param bool $cache whether to cache the object or not. |
||
| 491 | * @return mixed |
||
| 492 | * @throws EE_Error |
||
| 493 | * @throws ReflectionException |
||
| 494 | */ |
||
| 495 | public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | /** |
||
| 520 | * loads model classes - must be singletons |
||
| 521 | * |
||
| 522 | * @param string $class_name - simple class name ie: price |
||
| 523 | * @param mixed $arguments |
||
| 524 | * @param bool $load_only |
||
| 525 | * @return EEM_Base | bool |
||
| 526 | * @throws EE_Error |
||
| 527 | * @throws ReflectionException |
||
| 528 | */ |
||
| 529 | public function load_model($class_name, $arguments = array(), $load_only = false) |
||
| 549 | |||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * loads model classes - must be singletons |
||
| 554 | * |
||
| 555 | * @param string $class_name - simple class name ie: price |
||
| 556 | * @param mixed $arguments |
||
| 557 | * @param bool $load_only |
||
| 558 | * @return mixed | bool |
||
| 559 | * @throws EE_Error |
||
| 560 | * @throws ReflectionException |
||
| 561 | */ |
||
| 562 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
||
| 582 | |||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Determines if $model_name is the name of an actual EE model. |
||
| 587 | * |
||
| 588 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
| 589 | * @return boolean |
||
| 590 | */ |
||
| 591 | public function is_model_name($model_name) |
||
| 595 | |||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * generic class loader |
||
| 600 | * |
||
| 601 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 602 | * @param string $file_name - file name ie: my_file.php, including extension |
||
| 603 | * @param string $type - file type - core? class? helper? model? |
||
| 604 | * @param mixed $arguments |
||
| 605 | * @param bool $load_only |
||
| 606 | * @return mixed |
||
| 607 | * @throws EE_Error |
||
| 608 | * @throws ReflectionException |
||
| 609 | */ |
||
| 610 | public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
||
| 624 | |||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 629 | * @param string $class_name - full class name ie: My_Class |
||
| 630 | * @param string $type - file type - core? class? helper? model? |
||
| 631 | * @param mixed $arguments |
||
| 632 | * @param bool $load_only |
||
| 633 | * @return bool|EE_Addon|object |
||
| 634 | * @throws EE_Error |
||
| 635 | * @throws ReflectionException |
||
| 636 | */ |
||
| 637 | public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
||
| 651 | |||
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * instantiates, caches, and automatically resolves dependencies |
||
| 656 | * for classes that use a Fully Qualified Class Name. |
||
| 657 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
| 658 | * then you need to use one of the existing load_*() methods |
||
| 659 | * which can resolve the classname and filepath from the passed arguments |
||
| 660 | * |
||
| 661 | * @param bool|string $class_name Fully Qualified Class Name |
||
| 662 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
| 663 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 664 | * @param bool $from_db some classes are instantiated from the db |
||
| 665 | * and thus call a different method to instantiate |
||
| 666 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 667 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
| 668 | * @return bool|null|mixed null = failure to load or instantiate class object. |
||
| 669 | * object = class loaded and instantiated successfully. |
||
| 670 | * bool = fail or success when $load_only is true |
||
| 671 | * @throws EE_Error |
||
| 672 | * @throws ReflectionException |
||
| 673 | */ |
||
| 674 | public function create( |
||
| 735 | |||
| 736 | |||
| 737 | |||
| 738 | /** |
||
| 739 | * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
||
| 740 | * |
||
| 741 | * @param string $class_name |
||
| 742 | * @param array $arguments |
||
| 743 | * @param int $attempt |
||
| 744 | * @return mixed |
||
| 745 | */ |
||
| 746 | private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) { |
||
| 771 | |||
| 772 | |||
| 773 | |||
| 774 | /** |
||
| 775 | * instantiates, caches, and injects dependencies for classes |
||
| 776 | * |
||
| 777 | * @param array $file_paths an array of paths to folders to look in |
||
| 778 | * @param string $class_prefix EE or EEM or... ??? |
||
| 779 | * @param bool|string $class_name $class name |
||
| 780 | * @param string $type file type - core? class? helper? model? |
||
| 781 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
| 782 | * @param bool $from_db some classes are instantiated from the db |
||
| 783 | * and thus call a different method to instantiate |
||
| 784 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 785 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 786 | * @return bool|null|object null = failure to load or instantiate class object. |
||
| 787 | * object = class loaded and instantiated successfully. |
||
| 788 | * bool = fail or success when $load_only is true |
||
| 789 | * @throws EE_Error |
||
| 790 | * @throws ReflectionException |
||
| 791 | */ |
||
| 792 | protected function _load( |
||
| 856 | |||
| 857 | |||
| 858 | |||
| 859 | /** |
||
| 860 | * @param string $class_name |
||
| 861 | * @param string $default have to specify something, but not anything that will conflict |
||
| 862 | * @return mixed|string |
||
| 863 | */ |
||
| 864 | protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
||
| 870 | |||
| 871 | |||
| 872 | /** |
||
| 873 | * attempts to find a cached version of the requested class |
||
| 874 | * by looking in the following places: |
||
| 875 | * $this->{$class_abbreviation} ie: $this->CART |
||
| 876 | * $this->{$class_name} ie: $this->Some_Class |
||
| 877 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 878 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
| 879 | * |
||
| 880 | * @param string $class_name |
||
| 881 | * @param string $class_prefix |
||
| 882 | * @param array $arguments |
||
| 883 | * @return mixed |
||
| 884 | */ |
||
| 885 | protected function _get_cached_class( |
||
| 937 | |||
| 938 | |||
| 939 | /** |
||
| 940 | * removes a cached version of the requested class |
||
| 941 | * |
||
| 942 | * @param string $class_name |
||
| 943 | * @param boolean $addon |
||
| 944 | * @param array $arguments |
||
| 945 | * @return boolean |
||
| 946 | */ |
||
| 947 | public function clear_cached_class( |
||
| 974 | |||
| 975 | |||
| 976 | /** |
||
| 977 | * _set_cached_class |
||
| 978 | * attempts to cache the instantiated class locally |
||
| 979 | * in one of the following places, in the following order: |
||
| 980 | * $this->{class_abbreviation} ie: $this->CART |
||
| 981 | * $this->{$class_name} ie: $this->Some_Class |
||
| 982 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
| 983 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 984 | * |
||
| 985 | * @param object $class_obj |
||
| 986 | * @param string $class_name |
||
| 987 | * @param string $class_prefix |
||
| 988 | * @param bool $from_db |
||
| 989 | * @param array $arguments |
||
| 990 | * @return void |
||
| 991 | */ |
||
| 992 | protected function _set_cached_class( |
||
| 1022 | |||
| 1023 | |||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * build a string representation of a class' name and arguments |
||
| 1027 | * |
||
| 1028 | * @param string $class_name |
||
| 1029 | * @param array $arguments |
||
| 1030 | * @return string |
||
| 1031 | */ |
||
| 1032 | private function getClassIdentifier($class_name, $arguments = array()) |
||
| 1050 | |||
| 1051 | |||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * build a string representation of a class' arguments |
||
| 1055 | * (mostly because Closures can't be serialized) |
||
| 1056 | * |
||
| 1057 | * @param array $arguments |
||
| 1058 | * @return string |
||
| 1059 | */ |
||
| 1060 | private function getIdentifierForArguments($arguments = array()) |
||
| 1083 | |||
| 1084 | |||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * attempts to find a full valid filepath for the requested class. |
||
| 1088 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
| 1089 | * then returns that path if the target file has been found and is readable |
||
| 1090 | * |
||
| 1091 | * @param string $class_name |
||
| 1092 | * @param string $type |
||
| 1093 | * @param array $file_paths |
||
| 1094 | * @return string | bool |
||
| 1095 | */ |
||
| 1096 | protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
||
| 1121 | |||
| 1122 | |||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * basically just performs a require_once() |
||
| 1126 | * but with some error handling |
||
| 1127 | * |
||
| 1128 | * @param string $path |
||
| 1129 | * @param string $class_name |
||
| 1130 | * @param string $type |
||
| 1131 | * @param array $file_paths |
||
| 1132 | * @return bool |
||
| 1133 | * @throws EE_Error |
||
| 1134 | * @throws ReflectionException |
||
| 1135 | */ |
||
| 1136 | protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
||
| 1184 | |||
| 1185 | |||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Some of our legacy classes that extended a parent class would simply use a require() statement |
||
| 1189 | * before their class declaration in order to ensure that the parent class was loaded. |
||
| 1190 | * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
||
| 1191 | * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
||
| 1192 | * |
||
| 1193 | * @param string $class_name |
||
| 1194 | */ |
||
| 1195 | protected function resolve_legacy_class_parent($class_name = '') |
||
| 1207 | |||
| 1208 | |||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * _create_object |
||
| 1212 | * Attempts to instantiate the requested class via any of the |
||
| 1213 | * commonly used instantiation methods employed throughout EE. |
||
| 1214 | * The priority for instantiation is as follows: |
||
| 1215 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
| 1216 | * - model objects via their 'new_instance_from_db' method |
||
| 1217 | * - model objects via their 'new_instance' method |
||
| 1218 | * - "singleton" classes" via their 'instance' method |
||
| 1219 | * - standard instantiable classes via their __constructor |
||
| 1220 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
| 1221 | * then the constructor for the requested class will be examined to determine |
||
| 1222 | * if any dependencies exist, and if they can be injected. |
||
| 1223 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 1224 | * |
||
| 1225 | * @param string $class_name |
||
| 1226 | * @param array $arguments |
||
| 1227 | * @param string $type |
||
| 1228 | * @param bool $from_db |
||
| 1229 | * @return null|object |
||
| 1230 | * @throws EE_Error |
||
| 1231 | * @throws ReflectionException |
||
| 1232 | */ |
||
| 1233 | protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
||
| 1287 | |||
| 1288 | |||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
| 1292 | * @param array $array |
||
| 1293 | * @return bool |
||
| 1294 | */ |
||
| 1295 | protected function _array_is_numerically_and_sequentially_indexed(array $array) |
||
| 1301 | |||
| 1302 | |||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * getReflectionClass |
||
| 1306 | * checks if a ReflectionClass object has already been generated for a class |
||
| 1307 | * and returns that instead of creating a new one |
||
| 1308 | * |
||
| 1309 | * @param string $class_name |
||
| 1310 | * @return ReflectionClass |
||
| 1311 | * @throws ReflectionException |
||
| 1312 | */ |
||
| 1313 | View Code Duplication | public function get_ReflectionClass($class_name) |
|
| 1323 | |||
| 1324 | |||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * _resolve_dependencies |
||
| 1328 | * examines the constructor for the requested class to determine |
||
| 1329 | * if any dependencies exist, and if they can be injected. |
||
| 1330 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 1331 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
| 1332 | * For example: |
||
| 1333 | * if attempting to load a class "Foo" with the following constructor: |
||
| 1334 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
| 1335 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
| 1336 | * but only IF they are NOT already present in the incoming arguments array, |
||
| 1337 | * and the correct classes can be loaded |
||
| 1338 | * |
||
| 1339 | * @param ReflectionClass $reflector |
||
| 1340 | * @param string $class_name |
||
| 1341 | * @param array $arguments |
||
| 1342 | * @return array |
||
| 1343 | * @throws EE_Error |
||
| 1344 | * @throws ReflectionException |
||
| 1345 | */ |
||
| 1346 | protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
||
| 1418 | |||
| 1419 | |||
| 1420 | |||
| 1421 | /** |
||
| 1422 | * @param string $class_name |
||
| 1423 | * @param string $param_class |
||
| 1424 | * @param array $arguments |
||
| 1425 | * @param mixed $index |
||
| 1426 | * @param array $argument_keys |
||
| 1427 | * @return array |
||
| 1428 | * @throws EE_Error |
||
| 1429 | * @throws ReflectionException |
||
| 1430 | * @throws InvalidArgumentException |
||
| 1431 | * @throws InvalidInterfaceException |
||
| 1432 | * @throws InvalidDataTypeException |
||
| 1433 | */ |
||
| 1434 | protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
||
| 1479 | |||
| 1480 | |||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
| 1484 | * |
||
| 1485 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
| 1486 | * in the EE_Dependency_Map::$_class_loaders array, |
||
| 1487 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
| 1488 | * @param array $arguments |
||
| 1489 | * @return object |
||
| 1490 | */ |
||
| 1491 | public static function factory($classname, $arguments = array()) |
||
| 1502 | |||
| 1503 | |||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Gets the addon by its class name |
||
| 1507 | * |
||
| 1508 | * @param string $class_name |
||
| 1509 | * @return EE_Addon |
||
| 1510 | */ |
||
| 1511 | public function getAddon($class_name) |
||
| 1516 | |||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * removes the addon from the internal cache |
||
| 1520 | * |
||
| 1521 | * @param string $class_name |
||
| 1522 | * @return void |
||
| 1523 | */ |
||
| 1524 | public function removeAddon($class_name) |
||
| 1529 | |||
| 1530 | |||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Gets the addon by its name/slug (not classname. For that, just |
||
| 1534 | * use the get_addon() method above |
||
| 1535 | * |
||
| 1536 | * @param string $name |
||
| 1537 | * @return EE_Addon |
||
| 1538 | */ |
||
| 1539 | public function get_addon_by_name($name) |
||
| 1548 | |||
| 1549 | |||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Gets an array of all the registered addons, where the keys are their names. |
||
| 1553 | * (ie, what each returns for their name() function) |
||
| 1554 | * They're already available on EE_Registry::instance()->addons as properties, |
||
| 1555 | * where each property's name is the addon's classname, |
||
| 1556 | * So if you just want to get the addon by classname, |
||
| 1557 | * OR use the get_addon() method above. |
||
| 1558 | * PLEASE NOTE: |
||
| 1559 | * addons with Fully Qualified Class Names |
||
| 1560 | * have had the namespace separators converted to underscores, |
||
| 1561 | * so a classname like Fully\Qualified\ClassName |
||
| 1562 | * would have been converted to Fully_Qualified_ClassName |
||
| 1563 | * |
||
| 1564 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
| 1565 | */ |
||
| 1566 | public function get_addons_by_name() |
||
| 1574 | |||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
| 1578 | * a stale copy of it around |
||
| 1579 | * |
||
| 1580 | * @param string $model_name |
||
| 1581 | * @return \EEM_Base |
||
| 1582 | * @throws \EE_Error |
||
| 1583 | */ |
||
| 1584 | public function reset_model($model_name) |
||
| 1603 | |||
| 1604 | |||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * Resets the registry. |
||
| 1608 | * The criteria for what gets reset is based on what can be shared between sites on the same request when |
||
| 1609 | * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
||
| 1610 | * - $_dependency_map |
||
| 1611 | * - $_class_abbreviations |
||
| 1612 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
| 1613 | * - $REQ: Still on the same request so no need to change. |
||
| 1614 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
| 1615 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
||
| 1616 | * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
| 1617 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
| 1618 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
| 1619 | * switch or on the restore. |
||
| 1620 | * - $modules |
||
| 1621 | * - $shortcodes |
||
| 1622 | * - $widgets |
||
| 1623 | * |
||
| 1624 | * @param boolean $hard [deprecated] |
||
| 1625 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
| 1626 | * or just reset without re-instantiating (handy to set to FALSE if you're not |
||
| 1627 | * sure if you CAN currently reinstantiate the singletons at the moment) |
||
| 1628 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
||
| 1629 | * client |
||
| 1630 | * code instead can just change the model context to a different blog id if |
||
| 1631 | * necessary |
||
| 1632 | * @return EE_Registry |
||
| 1633 | * @throws EE_Error |
||
| 1634 | * @throws ReflectionException |
||
| 1635 | */ |
||
| 1636 | public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
||
| 1659 | |||
| 1660 | |||
| 1661 | |||
| 1662 | /** |
||
| 1663 | * if passed object implements ResettableInterface, then call it's reset() method |
||
| 1664 | * if passed object implements InterminableInterface, then return false, |
||
| 1665 | * to indicate that it should NOT be cleared from the Registry cache |
||
| 1666 | * |
||
| 1667 | * @param $object |
||
| 1668 | * @param bool $reset_models |
||
| 1669 | * @return bool returns true if cached object should be unset |
||
| 1670 | */ |
||
| 1671 | private static function _reset_and_unset_object($object, $reset_models) |
||
| 1698 | |||
| 1699 | |||
| 1700 | |||
| 1701 | /** |
||
| 1702 | * Gets all the custom post type models defined |
||
| 1703 | * |
||
| 1704 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
| 1705 | */ |
||
| 1706 | public function cpt_models() |
||
| 1716 | |||
| 1717 | |||
| 1718 | |||
| 1719 | /** |
||
| 1720 | * @return \EE_Config |
||
| 1721 | */ |
||
| 1722 | public static function CFG() |
||
| 1726 | |||
| 1727 | |||
| 1728 | } |
||
| 1729 | // End of file EE_Registry.core.php |
||
| 1731 |
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):