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 | ||
| 19 | class Filesystem extends \Symfony\Component\Filesystem\Filesystem | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * Build an absolute file or directory path separated by the OS specific directory separator. | ||
| 23 | * | ||
| 24 | * @param string ...$pathFragments | ||
| 25 | * | ||
| 26 | * @return string | ||
| 27 | */ | ||
| 28 | 64 | public function absolutePath($pathFragments) | |
| 40 | |||
| 41 | /** | ||
| 42 | * Build a file or directory path separated by the OS specific directory separator. | ||
| 43 | * | ||
| 44 | * @param string ...$pathFragments | ||
| 45 | * | ||
| 46 | * @return string | ||
| 47 | */ | ||
| 48 | 19 | public function appendPath($pathFragments) | |
| 52 | |||
| 53 | /** | ||
| 54 | * Copy a file or folder recursively. | ||
| 55 | * | ||
| 56 | * @param string $originFile The original filename | ||
| 57 | * @param string $targetFile The target filename | ||
| 58 | * @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten | ||
| 59 | * | ||
| 60 | * @throws FileNotFoundException When originFile doesn't exist | ||
| 61 | * @throws IOException When copy fails | ||
| 62 | */ | ||
| 63 | public function copy($originFile, $targetFile, $overwriteNewerFiles = false) | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Create an instance of Symfony's SplFileInfo with relative path information. | ||
| 95 | * | ||
| 96 | * @param string $filePath | ||
| 97 | * | ||
| 98 | * @return SplFileInfo | ||
| 99 | */ | ||
| 100 | public function createSplFileInfo($filePath) | ||
| 108 | |||
| 109 | /** | ||
| 110 | * Strip the current working directory from an absolute path. | ||
| 111 | * | ||
| 112 | * @param string $path An absolute path | ||
| 113 | * | ||
| 114 | * @return string | ||
| 115 | */ | ||
| 116 | 49 | public function getRelativePath($path) | |
| 120 | |||
| 121 | /** | ||
| 122 | * Get the name of a given file without the extension. | ||
| 123 | * | ||
| 124 | * @param string $filePath A file path | ||
| 125 | * | ||
| 126 | * @return string | ||
| 127 | */ | ||
| 128 | 39 | public function getBaseName($filePath) | |
| 132 | |||
| 133 | /** | ||
| 134 | * Get the name of a given file. | ||
| 135 | * | ||
| 136 | * @param string $filePath A file path | ||
| 137 | * | ||
| 138 | * @return string | ||
| 139 | */ | ||
| 140 | 13 | public function getFileName($filePath) | |
| 144 | |||
| 145 | /** | ||
| 146 | * Get the parent directory of a given file. | ||
| 147 | * | ||
| 148 | * @param string $filePath A file path | ||
| 149 | * | ||
| 150 | * @return string | ||
| 151 | */ | ||
| 152 | 17 | public function getFolderPath($filePath) | |
| 156 | |||
| 157 | /** | ||
| 158 | * Get the extension of a given file. | ||
| 159 | * | ||
| 160 | * @param string $filename A file path | ||
| 161 | * | ||
| 162 | * @return string The extension of the file | ||
| 163 | */ | ||
| 164 | 113 | public function getExtension($filename) | |
| 168 | |||
| 169 | /** | ||
| 170 | * Check whether or not if a given path is a directory. | ||
| 171 | * | ||
| 172 | * @param string $folderPath | ||
| 173 | * | ||
| 174 | * @return bool | ||
| 175 | */ | ||
| 176 | 13 | public function isDir($folderPath) | |
| 180 | |||
| 181 | /** | ||
| 182 | * Check whether a given file path is a symlink | ||
| 183 | * | ||
| 184 | * @param string $filePath | ||
| 185 | * | ||
| 186 | * @return bool | ||
| 187 | */ | ||
| 188 | public function isSymlink($filePath) | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Only read a file's contents if it's within the current working directory | ||
| 195 | * | ||
| 196 | * @param string $filePath | ||
| 197 | * | ||
| 198 | * @return bool|string | ||
| 199 | */ | ||
| 200 | 14 | public function safeReadFile($filePath) | |
| 214 | |||
| 215 | /** | ||
| 216 | * Get the full path to the file without the extension. | ||
| 217 | * | ||
| 218 | * @param string $filename A file path | ||
| 219 | * | ||
| 220 | * @return string | ||
| 221 | */ | ||
| 222 | 4 | public function removeExtension($filename) | |
| 229 | |||
| 230 | /** | ||
| 231 | * Write a file. | ||
| 232 | * | ||
| 233 | * @param string $targetDir The directory of where the file will be created; the file name is a separate variable | ||
| 234 | * @param string $fileName The name of the file | ||
| 235 | * @param string $content The content that belongs in the file | ||
| 236 | * | ||
| 237 | * @return SplFileInfo A reference to the newly created file | ||
| 238 | */ | ||
| 239 | View Code Duplication | public function writeFile($targetDir, $fileName, $content) | |
| 257 | } | ||
| 258 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.