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 |
||
16 | class EEH_Autoloader extends EEH_Base { |
||
17 | |||
18 | |||
19 | /** |
||
20 | * instance of the EE_System object |
||
21 | * |
||
22 | * @var $_instance |
||
23 | * @access private |
||
24 | */ |
||
25 | private static $_instance = null; |
||
26 | |||
27 | /** |
||
28 | * $_autoloaders |
||
29 | * @var array $_autoloaders |
||
30 | * @access private |
||
31 | */ |
||
32 | private static $_autoloaders; |
||
33 | |||
34 | /** |
||
35 | * set to "paths" to display autoloader class => path mappings |
||
36 | * set to "times" to display autoloader loading times |
||
37 | * set to "all" to display both |
||
38 | * |
||
39 | * @var string $debug |
||
40 | * @access private |
||
41 | */ |
||
42 | public static $debug = false; |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * class constructor |
||
48 | * |
||
49 | * @access private |
||
50 | * @return \EEH_Autoloader |
||
|
|||
51 | */ |
||
52 | private function __construct() { |
||
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * @access public |
||
64 | * @return EEH_Autoloader |
||
65 | */ |
||
66 | public static function instance() { |
||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * espresso_autoloader |
||
78 | * |
||
79 | * @access public |
||
80 | * @param $class_name |
||
81 | * @internal param $className |
||
82 | * @internal param string $class_name - simple class name ie: session |
||
83 | * @return void |
||
84 | */ |
||
85 | public static function espresso_autoloader( $class_name ) { |
||
90 | |||
91 | |||
92 | |||
93 | /** |
||
94 | * register_autoloader |
||
95 | * |
||
96 | * @access public |
||
97 | * @param array | string $class_paths - array of key => value pairings between class names and paths |
||
98 | * @param bool $read_check true if we need to check whether the file is readable or not. |
||
99 | * @param bool $debug **deprecated** |
||
100 | * @throws \EE_Error |
||
101 | */ |
||
102 | public static function register_autoloader( $class_paths, $read_check = true, $debug = false ) { |
||
103 | $class_paths = is_array( $class_paths ) ? $class_paths : array( $class_paths ); |
||
104 | foreach ( $class_paths as $class => $path ) { |
||
105 | // don't give up! you gotta... |
||
106 | // get some class |
||
107 | if ( empty( $class )) { |
||
108 | throw new EE_Error ( sprintf( __( 'No Class name was specified while registering an autoloader for the following path: %s.','event_espresso' ), $path )); |
||
109 | } |
||
110 | // one day you will find the path young grasshopper |
||
111 | if ( empty( $path )) { |
||
112 | throw new EE_Error ( sprintf( __( 'No path was specified while registering an autoloader for the %s class.','event_espresso' ), $class )); |
||
113 | } |
||
114 | // is file readable ? |
||
115 | if ( $read_check && ! is_readable( $path )) { |
||
116 | 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 )); |
||
117 | } |
||
118 | if ( ! isset( self::$_autoloaders[ $class ] )) { |
||
119 | self::$_autoloaders[ $class ] = str_replace( array( '/', '\\' ), DS, $path ); |
||
120 | if ( EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug ) ) { |
||
121 | EEH_Debug_Tools::printr( self::$_autoloaders[ $class ], $class, __FILE__, __LINE__ ); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * get_autoloaders |
||
132 | * |
||
133 | * @access public |
||
134 | * @return array |
||
135 | */ |
||
136 | public static function get_autoloaders() { |
||
139 | |||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * register core, model and class 'autoloaders' |
||
145 | * |
||
146 | * @access private |
||
147 | * @return void |
||
148 | */ |
||
149 | private function _register_custom_autoloaders() { |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * register core, model and class 'autoloaders' |
||
168 | * |
||
169 | * @access public |
||
170 | */ |
||
171 | public static function register_helpers_autoloaders() { |
||
174 | |||
175 | |||
176 | |||
177 | |||
178 | /** |
||
179 | * register core, model and class 'autoloaders' |
||
180 | * |
||
181 | * @access public |
||
182 | * @return void |
||
183 | */ |
||
184 | public static function register_form_sections_autoloaders() { |
||
187 | |||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | * register core, model and class 'autoloaders' |
||
193 | * |
||
194 | * @access public |
||
195 | * @return void |
||
196 | */ |
||
197 | public static function register_line_item_display_autoloaders() { |
||
200 | |||
201 | |||
202 | |||
203 | |||
204 | /** |
||
205 | * register core, model and class 'autoloaders' |
||
206 | * |
||
207 | * @access public |
||
208 | * @return void |
||
209 | */ |
||
210 | public static function register_line_item_filter_autoloaders() { |
||
213 | |||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * register template part 'autoloaders' |
||
219 | * |
||
220 | * @access public |
||
221 | * @return void |
||
222 | */ |
||
223 | public static function register_template_part_autoloaders() { |
||
226 | |||
227 | |||
228 | |||
229 | /** |
||
230 | * Assumes all the files in this folder have the normal naming scheme (namely that their classname |
||
231 | * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. |
||
232 | * 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. |
||
233 | * Yes this has to scan the directory for files, but it only does it once -- not on EACH |
||
234 | * time the autoloader is used |
||
235 | * |
||
236 | * @param string $folder name, with or without trailing /, doesn't matter |
||
237 | * @param bool $recursive |
||
238 | * @param bool $debug **deprecated** |
||
239 | * @throws \EE_Error |
||
240 | */ |
||
241 | public static function register_autoloaders_for_each_file_in_folder( $folder, $recursive = false, $debug = false ){ |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * add_alias |
||
277 | * register additional autoloader based on variation of the classname for an existing autoloader |
||
278 | * |
||
279 | * @access public |
||
280 | * @param string $class_name - simple class name ie: EE_Session |
||
281 | * @param string $alias - variation on class name ie: EE_session, session, etc |
||
282 | */ |
||
283 | public static function add_alias( $class_name, $alias ) { |
||
288 | |||
289 | |||
290 | |||
291 | } |
||
292 |
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.