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 |
||
| 14 | class File |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Default Path |
||
| 18 | */ |
||
| 19 | protected $_filePath = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Contructor |
||
| 23 | * |
||
| 24 | * @param string $defaultPath |
||
| 25 | */ |
||
| 26 | public function __construct($defaultPath = null) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Create Directory |
||
| 41 | * If path doesn't exist, create it |
||
| 42 | */ |
||
| 43 | private function createDir($path) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Write string to file |
||
| 55 | * |
||
| 56 | * @param string $content |
||
| 57 | * @param string $filename |
||
| 58 | * @param string $pathToFile |
||
| 59 | * @param string $mode - Default mode: w |
||
| 60 | * @return int - bytes written to file |
||
| 61 | */ |
||
| 62 | public function write($content, $filename, $pathToFile = null, $mode = "w") |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Read string to file |
||
| 79 | * |
||
| 80 | * @param string $filename |
||
| 81 | * @param string $pathToFile |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function read($filename, $pathToFile = null) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Delete a file |
||
| 98 | * |
||
| 99 | * @param string $filename |
||
| 100 | * @param string $pathToFile |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function delete($filename, $pathToFile = null) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Move a file |
||
| 117 | * |
||
| 118 | * @param string $filename |
||
| 119 | * @param string $pathTo |
||
| 120 | * @param string $pathToFrom |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function move($filename, $pathTo, $pathFrom = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Rename a file |
||
| 138 | * |
||
| 139 | * @param string $oldName |
||
| 140 | * @param string $pathTo |
||
| 141 | * @param string $pathToFrom |
||
| 142 | * @return bool |
||
| 143 | * @todo consider merging rename() and move() into one method |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function rename($oldName, $newName, $pathToFile = null) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Copy a file |
||
| 160 | * |
||
| 161 | * @param string $filename |
||
| 162 | * @param string $newFilePath |
||
| 163 | * @param string $newFileName |
||
| 164 | * @param string $pathToFile |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function copy($filename, $newFilePath, $newFileName = null, $pathToFile = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if a file exists |
||
| 184 | * |
||
| 185 | * @param string $filename |
||
| 186 | * @param string $pathToFile |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function fileExists($filename, $pathToFile = null) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Destructor |
||
| 199 | */ |
||
| 200 | public function __destruct() |
||
| 203 | } |
||
| 204 |