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:
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 | * @return void |
||
100 | * @throws \EE_Error |
||
101 | */ |
||
102 | public static function register_autoloader( $class_paths, $read_check = true ) { |
||
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() { |
||
162 | |||
163 | |||
164 | |||
165 | /** |
||
166 | * register core, model and class 'autoloaders' |
||
167 | * |
||
168 | * @access public |
||
169 | */ |
||
170 | public static function register_helpers_autoloaders() { |
||
173 | |||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * register core, model and class 'autoloaders' |
||
179 | * |
||
180 | * @access public |
||
181 | * @return void |
||
182 | */ |
||
183 | public static function register_form_sections_autoloaders() { |
||
186 | |||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * register core, model and class 'autoloaders' |
||
192 | * |
||
193 | * @access public |
||
194 | * @return void |
||
195 | */ |
||
196 | public static function register_line_item_display_autoloaders() { |
||
199 | |||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * register core, model and class 'autoloaders' |
||
205 | * |
||
206 | * @access public |
||
207 | * @return void |
||
208 | */ |
||
209 | public static function register_line_item_filter_autoloaders() { |
||
212 | |||
213 | |||
214 | |||
215 | |||
216 | /** |
||
217 | * register template part 'autoloaders' |
||
218 | * |
||
219 | * @access public |
||
220 | * @return void |
||
221 | */ |
||
222 | public static function register_template_part_autoloaders() { |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * Assumes all the files in this folder have the normal naming scheme (namely that their classname |
||
230 | * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. |
||
231 | * 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. |
||
232 | * Yes this has to scan the directory for files, but it only does it once -- not on EACH |
||
233 | * time the autoloader is used |
||
234 | * |
||
235 | * @param string $folder name, with or without trailing /, doesn't matter |
||
236 | * @param bool $recursive |
||
237 | * @return void |
||
238 | * @throws \EE_Error |
||
239 | */ |
||
240 | public static function register_autoloaders_for_each_file_in_folder( $folder, $recursive = false){ |
||
271 | |||
272 | |||
273 | |||
274 | |||
275 | |||
276 | } |
||
277 |
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.