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 |
||
| 13 | final class FilesystemPath |
||
| 14 | { |
||
| 15 | /** @var string */ |
||
| 16 | private $absolutePath; |
||
| 17 | /** @var string */ |
||
| 18 | private $originalPath; |
||
| 19 | /** @var bool */ |
||
| 20 | private $isWindows; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param string $filePath |
||
| 24 | * @param string $dirSep |
||
| 25 | */ |
||
| 26 | 24 | public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR) |
|
| 27 | { |
||
| 28 | 24 | $this->originalPath = $filePath; |
|
| 29 | 24 | $this->isWindows = ($dirSep === '\\'); |
|
| 30 | |||
| 31 | 24 | if ($this->isWindows) |
|
| 32 | { |
||
| 33 | 2 | $filePath = $this->unixifyPath($filePath); |
|
|
|
|||
| 34 | } |
||
| 35 | |||
| 36 | 24 | $this->absolutePath = (fs::isAbsolutePath($filePath)) ? $filePath : fs::absolutePath($filePath); |
|
| 37 | 24 | } |
|
| 38 | |||
| 39 | 10 | public function __toString() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Append a path to a directory path. |
||
| 46 | * |
||
| 47 | * @param string $append The path to append |
||
| 48 | */ |
||
| 49 | 5 | public function appendToPath($append) |
|
| 50 | { |
||
| 51 | 5 | if ($this->isFile(false)) |
|
| 52 | { |
||
| 53 | 1 | throw new \InvalidArgumentException("Appending to a file's path is not possible"); |
|
| 54 | } |
||
| 55 | |||
| 56 | 4 | $this->absolutePath = $this->buildPath($this->absolutePath, $this->unixifyPath($append)); |
|
| 57 | 4 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Generate a path based off this file path. |
||
| 61 | * |
||
| 62 | * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that. |
||
| 63 | * |
||
| 64 | * @param string $append |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 12 | public function generatePath($append) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Get the absolute path of the file path. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 10 | public function getAbsolutePath() |
|
| 79 | { |
||
| 80 | 10 | if ($this->isWindows) |
|
| 81 | { |
||
| 82 | 2 | return str_replace('/', '\\', $this->absolutePath); |
|
| 83 | } |
||
| 84 | |||
| 85 | 8 | return $this->absolutePath; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Check whether the given path is a directory. |
||
| 90 | * |
||
| 91 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory. |
||
| 92 | * When set to false, this function will guess based on the path ending in a directory |
||
| 93 | * separator. |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | 18 | public function isDir($checkExistence = true) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Check whether the given path is a file. |
||
| 111 | * |
||
| 112 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When |
||
| 113 | * set to false, this function will guess based on the path ending in a directory |
||
| 114 | * separator. |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | 5 | public function isFile($checkExistence = true) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Build a path from multiple strings. |
||
| 132 | * |
||
| 133 | * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use. |
||
| 134 | * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary. |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 16 | View Code Duplication | private function buildPath() |
| 139 | { |
||
| 140 | 16 | $paths = []; |
|
| 141 | |||
| 142 | 16 | foreach (func_get_args() as $arg) |
|
| 143 | { |
||
| 144 | 16 | if ($arg !== '') |
|
| 145 | { |
||
| 146 | 16 | $paths[] = $arg; |
|
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | 16 | return preg_replace('#(?<!:)/+#', '/', join('/', $paths)); |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Convert a Windows path into a blasphemous Unix path. |
||
| 155 | * |
||
| 156 | * @param string $filePath |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | 16 | private function unixifyPath($filePath) |
|
| 164 | } |
||
| 165 |