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 Filesystem extends \Symfony\Component\Filesystem\Filesystem |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Build an absolute file or directory path separated by the OS specific directory separator |
||
| 28 | * |
||
| 29 | * @param string ...$pathFragments |
||
| 30 | * |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | 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 | public function appendPath ($pathFragments) |
||
| 49 | { |
||
| 50 | return implode(DIRECTORY_SEPARATOR, func_get_args()); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Strip the current working directory from an absolute path |
||
| 55 | * |
||
| 56 | * @param string $path An absolute path |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getRelativePath ($path) |
||
| 64 | |||
| 65 | public function getFinder ($explicitIncludes = array(), $explicitIgnores = array(), $searchIn = "") |
||
| 66 | { |
||
| 67 | $finder = new Finder(); |
||
| 68 | $finder->files() |
||
| 69 | ->ignoreVCS(true) |
||
| 70 | ->ignoreDotFiles(true) |
||
| 71 | ->ignoreUnreadableDirs(); |
||
| 72 | |||
| 73 | $finder->in( |
||
| 74 | empty(trim($searchIn)) ? getcwd() : $searchIn |
||
| 75 | ); |
||
| 76 | |||
| 77 | foreach ($explicitIgnores as $ignoreRule) |
||
| 78 | { |
||
| 79 | $finder->notPath($ignoreRule); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (count($explicitIncludes) > 0) |
||
| 83 | { |
||
| 84 | foreach ($explicitIncludes as &$include) |
||
| 85 | { |
||
| 86 | $include = $this->absolutePath($include); |
||
| 87 | } |
||
| 88 | |||
| 89 | $finder->append($explicitIncludes); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $finder; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the name of a given file without the extension |
||
| 97 | * |
||
| 98 | * @param string $filePath A file path |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getBaseName ($filePath) |
||
| 103 | { |
||
| 104 | return pathinfo($filePath, PATHINFO_FILENAME); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the name of a given file |
||
| 109 | * |
||
| 110 | * @param string $filePath A file path |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 2 | public function getFileName ($filePath) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Get the parent directory of a given file |
||
| 121 | * |
||
| 122 | * @param string $filePath A file path |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getFolderPath ($filePath) |
||
| 127 | { |
||
| 128 | return pathinfo($filePath, PATHINFO_DIRNAME); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the extension of a given file |
||
| 133 | * |
||
| 134 | * @param string $filename A file path |
||
| 135 | * |
||
| 136 | * @return string The extension of the file |
||
| 137 | */ |
||
| 138 | 23 | public function getExtension ($filename) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $folderPath |
||
| 145 | */ |
||
| 146 | public function isDir ($folderPath) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the full path to the file without the extension |
||
| 153 | * |
||
| 154 | * @param string $filename A file path |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function removeExtension ($filename) |
||
| 159 | { |
||
| 160 | return $this->appendPath( |
||
| 161 | $this->getFolderPath($filename), |
||
| 162 | $this->getBaseName($filename) |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Write a file |
||
| 168 | * |
||
| 169 | * @param string $targetDir The directory of where the file will be created; the file name is a separate variable |
||
| 170 | * @param string $fileName The name of the file |
||
| 171 | * @param string $content The content that belongs in the file |
||
| 172 | * |
||
| 173 | * @return SplFileInfo A reference to the newly created file |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function writeFile ($targetDir, $fileName, $content) |
|
| 193 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.