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 EEH_Autoloader 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 EEH_Autoloader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class EEH_Autoloader extends EEH_Base |
||
| 15 | { |
||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * instance of the EE_System object |
||
| 20 | * |
||
| 21 | * @var $_instance |
||
| 22 | * @access private |
||
| 23 | */ |
||
| 24 | private static $_instance = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * $_autoloaders |
||
| 28 | * @var array $_autoloaders |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | private static $_autoloaders; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * set to "paths" to display autoloader class => path mappings |
||
| 35 | * set to "times" to display autoloader loading times |
||
| 36 | * set to "all" to display both |
||
| 37 | * |
||
| 38 | * @var string $debug |
||
| 39 | * @access private |
||
| 40 | */ |
||
| 41 | public static $debug = false; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * class constructor |
||
| 46 | * |
||
| 47 | * @access private |
||
| 48 | * @return \EEH_Autoloader |
||
|
|
|||
| 49 | * @throws Exception |
||
| 50 | */ |
||
| 51 | private function __construct() |
||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * @access public |
||
| 64 | * @return EEH_Autoloader |
||
| 65 | */ |
||
| 66 | public static function instance() |
||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * espresso_autoloader |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * @param $class_name |
||
| 82 | * @internal param $className |
||
| 83 | * @internal param string $class_name - simple class name ie: session |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public static function espresso_autoloader($class_name) |
||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * register_autoloader |
||
| 97 | * |
||
| 98 | * @access public |
||
| 99 | * @param array | string $class_paths - array of key => value pairings between class names and paths |
||
| 100 | * @param bool $read_check true if we need to check whether the file is readable or not. |
||
| 101 | * @param bool $debug **deprecated** |
||
| 102 | * @throws \EE_Error |
||
| 103 | */ |
||
| 104 | public static function register_autoloader($class_paths, $read_check = true, $debug = false) |
||
| 105 | { |
||
| 106 | $class_paths = is_array($class_paths) ? $class_paths : array( $class_paths ); |
||
| 107 | foreach ($class_paths as $class => $path) { |
||
| 108 | // skip all files that are not PHP |
||
| 109 | if (substr($path, strlen($path) - 3) !== 'php') { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | // don't give up! you gotta... |
||
| 113 | // get some class |
||
| 114 | if (empty($class)) { |
||
| 115 | throw new EE_Error(sprintf(__('No Class name was specified while registering an autoloader for the following path: %s.', 'event_espresso'), $path)); |
||
| 116 | } |
||
| 117 | // one day you will find the path young grasshopper |
||
| 118 | if (empty($path)) { |
||
| 119 | throw new EE_Error(sprintf(__('No path was specified while registering an autoloader for the %s class.', 'event_espresso'), $class)); |
||
| 120 | } |
||
| 121 | // is file readable ? |
||
| 122 | if ($read_check && ! is_readable($path)) { |
||
| 123 | throw new EE_Error(sprintf(__('The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso'), $class, $path)); |
||
| 124 | } |
||
| 125 | if (! isset(self::$_autoloaders[ $class ])) { |
||
| 126 | self::$_autoloaders[ $class ] = str_replace(array( '/', '\\' ), DS, $path); |
||
| 127 | if (EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug )) { |
||
| 128 | EEH_Debug_Tools::printr(self::$_autoloaders[ $class ], $class, __FILE__, __LINE__); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * get_autoloaders |
||
| 139 | * |
||
| 140 | * @access public |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public static function get_autoloaders() |
||
| 144 | { |
||
| 145 | return self::$_autoloaders; |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * register core, model and class 'autoloaders' |
||
| 151 | * |
||
| 152 | * @access private |
||
| 153 | * @return void |
||
| 154 | * @throws EE_Error |
||
| 155 | */ |
||
| 156 | private function _register_custom_autoloaders() |
||
| 157 | { |
||
| 158 | EEH_Autoloader::$debug = ''; |
||
| 159 | \EEH_Autoloader::register_helpers_autoloaders(); |
||
| 160 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces'); |
||
| 161 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE); |
||
| 162 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_INTERFACES, true); |
||
| 163 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_MODELS, true); |
||
| 164 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CLASSES); |
||
| 165 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_FORM_SECTIONS, true); |
||
| 166 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'messages'); |
||
| 167 | if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') { |
||
| 168 | EEH_Debug_Tools::instance()->show_times(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * register core, model and class 'autoloaders' |
||
| 176 | * |
||
| 177 | * @access public |
||
| 178 | */ |
||
| 179 | public static function register_helpers_autoloaders() |
||
| 180 | { |
||
| 181 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS); |
||
| 182 | } |
||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * register core, model and class 'autoloaders' |
||
| 189 | * |
||
| 190 | * @access public |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public static function register_form_sections_autoloaders() |
||
| 194 | { |
||
| 195 | // EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true ); |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * register core, model and class 'autoloaders' |
||
| 201 | * |
||
| 202 | * @access public |
||
| 203 | * @return void |
||
| 204 | * @throws EE_Error |
||
| 205 | */ |
||
| 206 | public static function register_line_item_display_autoloaders() |
||
| 207 | { |
||
| 208 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_display', true); |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * register core, model and class 'autoloaders' |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @return void |
||
| 217 | * @throws EE_Error |
||
| 218 | */ |
||
| 219 | public static function register_line_item_filter_autoloaders() |
||
| 220 | { |
||
| 221 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_filters', true); |
||
| 222 | } |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * register template part 'autoloaders' |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @return void |
||
| 230 | * @throws EE_Error |
||
| 231 | */ |
||
| 232 | public static function register_template_part_autoloaders() |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @return void |
||
| 240 | * @throws EE_Error |
||
| 241 | */ |
||
| 242 | public static function register_business_classes() |
||
| 243 | { |
||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Assumes all the files in this folder have the normal naming scheme (namely that their classname |
||
| 251 | * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. |
||
| 252 | * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly. |
||
| 253 | * Yes this has to scan the directory for files, but it only does it once -- not on EACH |
||
| 254 | * time the autoloader is used |
||
| 255 | * |
||
| 256 | * @param string $folder name, with or without trailing /, doesn't matter |
||
| 257 | * @param bool $recursive |
||
| 258 | * @param bool $debug **deprecated** |
||
| 259 | * @throws \EE_Error |
||
| 260 | */ |
||
| 261 | public static function register_autoloaders_for_each_file_in_folder($folder, $recursive = false, $debug = false) |
||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * add_alias |
||
| 298 | * register additional autoloader based on variation of the classname for an existing autoloader |
||
| 299 | * |
||
| 300 | * @access public |
||
| 301 | * @param string $class_name - simple class name ie: EE_Session |
||
| 302 | * @param string $alias - variation on class name ie: EE_session, session, etc |
||
| 303 | */ |
||
| 304 | public static function add_alias($class_name, $alias) |
||
| 310 | } |
||
| 311 |
Adding a
@returnannotation 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.