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 FrontEndFileHandler 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 FrontEndFileHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class FrontEndFileHandler extends Handler |
||
9 | { |
||
10 | |||
11 | static $isSSL = null; |
||
12 | |||
13 | /** |
||
14 | * Map for css |
||
15 | * @var array |
||
16 | */ |
||
17 | var $cssMap = array(); |
||
18 | |||
19 | /** |
||
20 | * Map for Javascript at head |
||
21 | * @var array |
||
22 | */ |
||
23 | var $jsHeadMap = array(); |
||
24 | |||
25 | /** |
||
26 | * Map for Javascript at body |
||
27 | * @var array |
||
28 | */ |
||
29 | var $jsBodyMap = array(); |
||
30 | |||
31 | /** |
||
32 | * Index for css |
||
33 | * @var array |
||
34 | */ |
||
35 | var $cssMapIndex = array(); |
||
36 | |||
37 | /** |
||
38 | * Index for javascript at head |
||
39 | * @var array |
||
40 | */ |
||
41 | var $jsHeadMapIndex = array(); |
||
42 | |||
43 | /** |
||
44 | * Index for javascript at body |
||
45 | * @var array |
||
46 | */ |
||
47 | var $jsBodyMapIndex = array(); |
||
48 | |||
49 | /** |
||
50 | * Check SSL |
||
51 | * |
||
52 | * @return bool If using ssl returns true, otherwise returns false. |
||
53 | * @deprecated |
||
54 | */ |
||
55 | function isSsl() |
||
67 | |||
68 | /** |
||
69 | * Load front end file |
||
70 | * |
||
71 | * The $args is use as below. File type(js, css) is detected by file extension. |
||
72 | * |
||
73 | * <pre> |
||
74 | * case js |
||
75 | * $args[0]: file name |
||
76 | * $args[1]: type (head | body) |
||
77 | * $args[2]: target IE |
||
78 | * $args[3]: index |
||
79 | * case css |
||
80 | * $args[0]: file name |
||
81 | * $args[1]: media |
||
82 | * $args[2]: target IE |
||
83 | * $args[3]: index |
||
84 | * </pre> |
||
85 | * |
||
86 | * @param array $args Arguments |
||
87 | * @return void |
||
88 | * */ |
||
89 | function loadFile($args) |
||
134 | |||
135 | /** |
||
136 | * Get file information |
||
137 | * |
||
138 | * @param string $fileName The file name |
||
139 | * @param string $targetIe Target IE of file |
||
140 | * @param string $media Media of file |
||
141 | * @return stdClass The file information |
||
142 | */ |
||
143 | private function getFileInfo($fileName, $targetIe = '', $media = 'all') |
||
207 | |||
208 | /** |
||
209 | * Unload front end file |
||
210 | * |
||
211 | * @param string $fileName The file name to unload |
||
212 | * @param string $targetIe Target IE of file to unload |
||
213 | * @param string $media Media of file to unload. Only use when file is css. |
||
214 | * @return void |
||
215 | */ |
||
216 | function unloadFile($fileName, $targetIe = '', $media = 'all') |
||
242 | |||
243 | /** |
||
244 | * Unload all front end file |
||
245 | * |
||
246 | * @param string $type Type to unload. all, css, js |
||
247 | * @return void |
||
248 | */ |
||
249 | function unloadAllFiles($type = 'all') |
||
265 | |||
266 | /** |
||
267 | * Get css file list |
||
268 | * |
||
269 | * @return array Returns css file list. Array contains file, media, targetie. |
||
270 | */ |
||
271 | function getCssFileList() |
||
306 | |||
307 | /** |
||
308 | * Get javascript file list |
||
309 | * |
||
310 | * @param string $type Type of javascript. head, body |
||
311 | * @return array Returns javascript file list. Array contains file, targetie. |
||
312 | */ |
||
313 | function getJsFileList($type = 'head') |
||
355 | |||
356 | /** |
||
357 | * Sort a map |
||
358 | * |
||
359 | * @param array $map Array to sort |
||
360 | * @param array $index Not used |
||
361 | * @return void |
||
362 | */ |
||
363 | function _sortMap(&$map, &$index) |
||
367 | |||
368 | /** |
||
369 | * Normalize File path |
||
370 | * |
||
371 | * @param string $path Path to normalize |
||
372 | * @return string Normalized path |
||
373 | */ |
||
374 | function _normalizeFilePath($path) |
||
394 | |||
395 | /** |
||
396 | * Get absolute file url |
||
397 | * |
||
398 | * @param string $path Path to get absolute url |
||
399 | * @return string Absolute url |
||
400 | */ |
||
401 | function _getAbsFileUrl($path) |
||
424 | |||
425 | /** |
||
426 | * Arrage css index |
||
427 | * |
||
428 | * @param string $dirName First directory name of css path |
||
429 | * @param array $file file info. |
||
430 | * @return void |
||
431 | */ |
||
432 | function _arrangeCssIndex($dirName, &$file) |
||
445 | |||
446 | } |
||
447 | /* End of file FrontEndFileHandler.class.php */ |
||
449 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.