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 |
||
| 26 | class Filesystem extends \Symfony\Component\Filesystem\Filesystem |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Build an absolute file or directory path separated by the OS specific directory separator |
||
| 30 | * |
||
| 31 | * @param string ...$pathFragments |
||
| 32 | * |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | public function absolutePath ($pathFragments) |
||
| 36 | { |
||
| 37 | if ($this->isAbsolutePath($pathFragments)) |
||
| 38 | { |
||
| 39 | return $pathFragments; |
||
| 40 | } |
||
| 41 | |||
| 42 | $args = func_get_args(); |
||
| 43 | array_unshift($args, getcwd()); |
||
| 44 | |||
| 45 | return implode(DIRECTORY_SEPARATOR, $args); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Build a file or directory path separated by the OS specific directory separator |
||
| 50 | * |
||
| 51 | * @param string ...$pathFragments |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | 3 | public function appendPath ($pathFragments) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Copy a file or folder recursively |
||
| 62 | * |
||
| 63 | * @param string $originFile The original filename |
||
| 64 | * @param string $targetFile The target filename |
||
| 65 | * @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten |
||
| 66 | * |
||
| 67 | * @throws FileNotFoundException When originFile doesn't exist |
||
| 68 | * @throws IOException When copy fails |
||
| 69 | */ |
||
| 70 | public function copy($originFile, $targetFile, $overwriteNewerFiles = false) |
||
| 71 | { |
||
| 72 | if ($this->isDir($originFile)) |
||
| 73 | { |
||
| 74 | if (!$this->isDir($targetFile)) |
||
| 75 | { |
||
| 76 | mkdir($targetFile, 0755, true); |
||
| 77 | } |
||
| 78 | |||
| 79 | $dir = dir($originFile); |
||
| 80 | |||
| 81 | while (false !== $entry = $dir->read()) |
||
| 82 | { |
||
| 83 | // Skip pointers |
||
| 84 | if ($entry == '.' || $entry == '..') { continue; } |
||
| 85 | |||
| 86 | $this->copy("$originFile/$entry", "$targetFile/$entry", true); |
||
| 87 | } |
||
| 88 | |||
| 89 | $dir->close(); |
||
| 90 | } |
||
| 91 | else |
||
| 92 | { |
||
| 93 | parent::copy($originFile, $targetFile, $overwriteNewerFiles); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Strip the current working directory from an absolute path |
||
| 99 | * |
||
| 100 | * @param string $path An absolute path |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getRelativePath ($path) |
||
| 108 | |||
| 109 | public function getFinder ($explicitIncludes = array(), $explicitIgnores = array(), $searchIn = "") |
||
| 110 | { |
||
| 111 | $finder = new Finder(); |
||
| 112 | $finder->files() |
||
| 113 | ->ignoreVCS(true) |
||
| 114 | ->ignoreDotFiles(true) |
||
| 115 | ->ignoreUnreadableDirs(); |
||
| 116 | |||
| 117 | $finder->in( |
||
| 118 | empty(trim($searchIn)) ? getcwd() : $searchIn |
||
| 119 | ); |
||
| 120 | |||
| 121 | foreach ($explicitIgnores as $ignoreRule) |
||
| 122 | { |
||
| 123 | $isRegex = @preg_match($ignoreRule, null); |
||
| 124 | |||
| 125 | if (substr($ignoreRule, -1, 1) === '/' || $isRegex !== false) |
||
| 126 | { |
||
| 127 | $finder->notPath($ignoreRule); |
||
| 128 | } |
||
| 129 | else |
||
| 130 | { |
||
| 131 | $finder->notName($ignoreRule); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if (count($explicitIncludes) > 0) |
||
| 136 | { |
||
| 137 | foreach ($explicitIncludes as &$include) |
||
| 138 | { |
||
| 139 | $include = new SplFileInfo($include, $this->getFolderPath($include), $include); |
||
| 140 | } |
||
| 141 | |||
| 142 | $finder->append($explicitIncludes); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $finder; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the name of a given file without the extension |
||
| 150 | * |
||
| 151 | * @param string $filePath A file path |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 3 | public function getBaseName ($filePath) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get the name of a given file |
||
| 162 | * |
||
| 163 | * @param string $filePath A file path |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 2 | public function getFileName ($filePath) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get the parent directory of a given file |
||
| 174 | * |
||
| 175 | * @param string $filePath A file path |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 3 | public function getFolderPath ($filePath) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get the extension of a given file |
||
| 186 | * |
||
| 187 | * @param string $filename A file path |
||
| 188 | * |
||
| 189 | * @return string The extension of the file |
||
| 190 | */ |
||
| 191 | 23 | public function getExtension ($filename) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $folderPath |
||
| 198 | */ |
||
| 199 | public function isDir ($folderPath) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the full path to the file without the extension |
||
| 206 | * |
||
| 207 | * @param string $filename A file path |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 3 | public function removeExtension ($filename) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Write a file |
||
| 221 | * |
||
| 222 | * @param string $targetDir The directory of where the file will be created; the file name is a separate variable |
||
| 223 | * @param string $fileName The name of the file |
||
| 224 | * @param string $content The content that belongs in the file |
||
| 225 | * |
||
| 226 | * @return SplFileInfo A reference to the newly created file |
||
| 227 | */ |
||
| 228 | View Code Duplication | public function writeFile ($targetDir, $fileName, $content) |
|
| 246 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.