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 |
||
| 25 | class Filesystem extends \Symfony\Component\Filesystem\Filesystem |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Build an absolute file or directory path separated by the OS specific directory separator |
||
| 29 | * |
||
| 30 | * @param string ...$pathFragments |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | 33 | public function absolutePath ($pathFragments) |
|
| 35 | { |
||
| 36 | 33 | if ($this->isAbsolutePath($pathFragments)) |
|
| 37 | { |
||
| 38 | 7 | return $pathFragments; |
|
| 39 | } |
||
| 40 | |||
| 41 | 29 | $args = func_get_args(); |
|
| 42 | 29 | array_unshift($args, getcwd()); |
|
| 43 | |||
| 44 | 29 | return implode(DIRECTORY_SEPARATOR, $args); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Build a file or directory path separated by the OS specific directory separator |
||
| 49 | * |
||
| 50 | * @param string ...$pathFragments |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 18 | public function appendPath ($pathFragments) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Copy a file or folder recursively |
||
| 61 | * |
||
| 62 | * @param string $originFile The original filename |
||
| 63 | * @param string $targetFile The target filename |
||
| 64 | * @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten |
||
| 65 | * |
||
| 66 | * @throws FileNotFoundException When originFile doesn't exist |
||
| 67 | * @throws IOException When copy fails |
||
| 68 | */ |
||
| 69 | public function copy($originFile, $targetFile, $overwriteNewerFiles = false) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create an instance of Symfony's SplFileInfo with relative path information |
||
| 98 | * |
||
| 99 | * @param string $filePath |
||
| 100 | * |
||
| 101 | * @return SplFileInfo |
||
| 102 | */ |
||
| 103 | public function createSplFileInfo ($filePath) |
||
| 104 | { |
||
| 105 | return (new SplFileInfo( |
||
| 106 | $this->absolutePath($filePath), |
||
| 107 | $this->getRelativePath($this->getFolderPath($filePath)), |
||
| 108 | $this->getRelativePath($filePath) |
||
| 109 | )); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Strip the current working directory from an absolute path |
||
| 114 | * |
||
| 115 | * @param string $path An absolute path |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 47 | public function getRelativePath ($path) |
|
| 120 | { |
||
| 121 | 47 | return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the name of a given file without the extension |
||
| 126 | * |
||
| 127 | * @param string $filePath A file path |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 37 | public function getBaseName ($filePath) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the name of a given file |
||
| 138 | * |
||
| 139 | * @param string $filePath A file path |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 3 | public function getFileName ($filePath) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the parent directory of a given file |
||
| 150 | * |
||
| 151 | * @param string $filePath A file path |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 7 | public function getFolderPath ($filePath) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get the extension of a given file |
||
| 162 | * |
||
| 163 | * @param string $filename A file path |
||
| 164 | * |
||
| 165 | * @return string The extension of the file |
||
| 166 | */ |
||
| 167 | 89 | public function getExtension ($filename) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Check whether or not if a given path is a directory |
||
| 174 | * |
||
| 175 | * @param string $folderPath |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | 3 | public function isDir ($folderPath) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get the full path to the file without the extension |
||
| 186 | * |
||
| 187 | * @param string $filename A file path |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 4 | public function removeExtension ($filename) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Write a file |
||
| 201 | * |
||
| 202 | * @param string $targetDir The directory of where the file will be created; the file name is a separate variable |
||
| 203 | * @param string $fileName The name of the file |
||
| 204 | * @param string $content The content that belongs in the file |
||
| 205 | * |
||
| 206 | * @return SplFileInfo A reference to the newly created file |
||
| 207 | */ |
||
| 208 | View Code Duplication | public function writeFile ($targetDir, $fileName, $content) |
|
| 226 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.