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 FilesystemHelper 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 FilesystemHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class FilesystemHelper { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Wrapper for filemtime function |
||
| 28 | * @param string $path |
||
| 29 | * @return integer |
||
| 30 | */ |
||
| 31 | public function filemtime($path){ |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Wrapper for scandir function. |
||
| 37 | * Filters current and parent directories |
||
| 38 | * @param string $path |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function scandirFiltered($path){ |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Wrapper for scandir function |
||
| 51 | * @param string $path |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function scandir($path){ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Wrapper for file_exists function |
||
| 60 | * @param string $path |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function fileExists($path){ |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Wrapper for is_writable function |
||
| 69 | * @param string $path |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function isWritable($path){ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Wrapper for is_dir function |
||
| 78 | * @param string $path |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function isDir($path){ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Wrapper for md5_file function |
||
| 87 | * @param string $path |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function md5File($path){ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Wrapper for mkdir |
||
| 96 | * @param string $path |
||
| 97 | * @param bool $isRecursive |
||
| 98 | * @throws \Exception on error |
||
| 99 | */ |
||
| 100 | public function mkdir($path, $isRecursive = false){ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Copy recursive |
||
| 108 | * @param string $src - source path |
||
| 109 | * @param string $dest - destination path |
||
| 110 | * @throws \Exception on error |
||
| 111 | */ |
||
| 112 | public function copyr($src, $dest, $stopOnError = true){ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Moves file/directory |
||
| 138 | * @param string $src - source path |
||
| 139 | * @param string $dest - destination path |
||
| 140 | * @throws \Exception on error |
||
| 141 | */ |
||
| 142 | public function move($src, $dest){ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Check permissions recursive |
||
| 150 | * @param string $src - path to check |
||
| 151 | * @param Collection $collection - object to store incorrect permissions |
||
| 152 | */ |
||
| 153 | public function checkr($src, $collection){ |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $path |
||
| 175 | */ |
||
| 176 | public function removeIfExists($path) { |
||
| 187 | |||
| 188 | public function rmdirr($dir) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * @param string $old |
||
| 210 | * @param string $new |
||
| 211 | * @param string $temp |
||
| 212 | * @param string $dirName |
||
| 213 | */ |
||
| 214 | public function tripleMove($old, $new, $temp, $dirName){ |
||
| 224 | |||
| 225 | } |
||
| 226 |
If you suppress an error, we recommend checking for the error condition explicitly: