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 UFileSystem 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 UFileSystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class UFileSystem { |
||
| 11 | |||
| 12 | public static function glob_recursive($pattern, $flags=0) { |
||
| 19 | |||
| 20 | public static function deleteAllFilesFromFolder($folder) { |
||
| 27 | |||
| 28 | public static function deleteFile($filename){ |
||
| 33 | |||
| 34 | public static function safeMkdir($dir) { |
||
| 39 | |||
| 40 | public static function cleanPathname($path) { |
||
| 53 | |||
| 54 | public static function cleanFilePathname($path) { |
||
| 64 | |||
| 65 | public static function openReplaceInTemplateFile($source, $keyAndValues) { |
||
| 72 | |||
| 73 | public static function openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues) { |
||
| 79 | |||
| 80 | public static function replaceFromTemplate($content, $keyAndValues) { |
||
| 88 | |||
| 89 | public static function replaceWriteFromContent($content, $destination, $keyAndValues) { |
||
| 92 | |||
| 93 | public static function tryToRequire($file) { |
||
| 100 | |||
| 101 | public static function lastModified($filename) { |
||
| 104 | |||
| 105 | public static function load($filename){ |
||
| 111 | |||
| 112 | public static function save($filename,$content,$flags=LOCK_EX){ |
||
| 115 | |||
| 116 | public static function getDirFromNamespace($ns){ |
||
| 119 | |||
| 120 | public static function xcopy($source, $dest, $permissions = 0755){ |
||
| 140 | |||
| 141 | public static function delTree($dir) { |
||
| 148 | } |
||
| 149 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.