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 |
||
| 24 | class FilesystemHelper { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Wrapper for scandir function |
||
| 28 | * @param string $path |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | public function scandir($path){ |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Wrapper for file_exists function |
||
| 37 | * @param string $path |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | public function fileExists($path){ |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Wrapper for is_writable function |
||
| 46 | * @param string $path |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | public function isWritable($path){ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Wrapper for md5_file function |
||
| 55 | * @param string $path |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function md5File($path){ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Wrapper for mkdir |
||
| 64 | * @param string $path |
||
| 65 | * @param bool $isRecursive |
||
| 66 | * @throws \Exception on error |
||
| 67 | */ |
||
| 68 | public function mkdir($path, $isRecursive = false){ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Copy recursive |
||
| 76 | * @param string $src - source path |
||
| 77 | * @param string $dest - destination path |
||
| 78 | * @throws \Exception on error |
||
| 79 | */ |
||
| 80 | public function copyr($src, $dest, $stopOnError = true){ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Moves file/directory |
||
| 106 | * @param string $src - source path |
||
| 107 | * @param string $dest - destination path |
||
| 108 | * @throws \Exception on error |
||
| 109 | */ |
||
| 110 | public function move($src, $dest){ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check permissions recursive |
||
| 118 | * @param string $src - path to check |
||
| 119 | * @param Collection $collection - object to store incorrect permissions |
||
| 120 | */ |
||
| 121 | public function checkr($src, $collection){ |
||
| 140 | |||
| 141 | public function removeIfExists($path) { |
||
| 152 | |||
| 153 | protected function rmdirr($dir) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * |
||
| 174 | * @param string $old |
||
| 175 | * @param string $new |
||
| 176 | * @param string $temp |
||
| 177 | * @param string $dirName |
||
| 178 | */ |
||
| 179 | public function tripleMove($old, $new, $temp, $dirName){ |
||
| 187 | |||
| 188 | } |
||
| 189 |
If you suppress an error, we recommend checking for the error condition explicitly: