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 |
||
| 18 | final class FilesystemPath |
||
| 19 | { |
||
| 20 | /** @var string */ |
||
| 21 | private $absolutePath; |
||
| 22 | /** @var string */ |
||
| 23 | private $originalPath; |
||
| 24 | /** @var bool */ |
||
| 25 | private $isWindows; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param FilesystemPath|string $filePath |
||
| 29 | * @param string $dirSep |
||
| 30 | */ |
||
| 31 | 25 | public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR) |
|
| 45 | |||
| 46 | 13 | public function __toString() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Append a path to a directory path. |
||
| 53 | * |
||
| 54 | * @param string $append The path to append |
||
| 55 | * |
||
| 56 | * @return self |
||
| 57 | */ |
||
| 58 | 6 | public function appendToPath($append) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Generate a path based off this file path. |
||
| 72 | * |
||
| 73 | * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that. |
||
| 74 | * |
||
| 75 | * @param string $append |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 12 | public function generatePath($append) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get the absolute path of the file path. |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 13 | public function getAbsolutePath() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Check whether the given path is a directory. |
||
| 101 | * |
||
| 102 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory. |
||
| 103 | * When set to false, this function will guess based on the path ending in a directory |
||
| 104 | * separator. |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 19 | public function isDir($checkExistence = true) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Check whether the given path is a file. |
||
| 122 | * |
||
| 123 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When |
||
| 124 | * set to false, this function will guess based on the path ending in a directory |
||
| 125 | * separator. |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 6 | public function isFile($checkExistence = true) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Build a path from multiple strings. |
||
| 143 | * |
||
| 144 | * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use. |
||
| 145 | * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 17 | View Code Duplication | private function buildPath() |
| 163 | |||
| 164 | /** |
||
| 165 | * Convert a Windows path into a blasphemous Unix path. |
||
| 166 | * |
||
| 167 | * @param string $filePath |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 17 | private function unixifyPath($filePath) |
|
| 175 | } |
||
| 176 |