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 string $filePath |
||
| 29 | * @param string $dirSep |
||
| 30 | */ |
||
| 31 | 12 | public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR) |
|
| 43 | |||
| 44 | 10 | public function __toString() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Append a path to a directory path. |
||
| 51 | * |
||
| 52 | * @param string $append The path to append |
||
| 53 | */ |
||
| 54 | 5 | public function appendToPath($append) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Generate a path based off this file path. |
||
| 66 | * |
||
| 67 | * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that. |
||
| 68 | * |
||
| 69 | * @param string $append |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function generatePath($append) |
||
| 74 | { |
||
| 75 | return $this->buildPath($this->absolutePath, $this->unixifyPath($append)); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get the absolute path of the file path. |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 10 | public function getAbsolutePath() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Check whether the given path is a directory. |
||
| 95 | * |
||
| 96 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory. |
||
| 97 | * When set to false, this function will guess based on the path ending in a directory |
||
| 98 | * separator. |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 6 | public function isDir($checkExistence = true) |
|
| 103 | { |
||
| 104 | 6 | $absPath = $this->absolutePath; |
|
| 105 | |||
| 106 | 6 | if ($checkExistence) |
|
| 107 | { |
||
| 108 | return file_exists($absPath) && is_dir($absPath); |
||
| 109 | } |
||
| 110 | |||
| 111 | 6 | return substr($absPath, -1, 1) == '/'; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Check whether the given path is a file. |
||
| 116 | * |
||
| 117 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When |
||
| 118 | * set to false, this function will guess based on the path ending in a directory |
||
| 119 | * separator. |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 5 | public function isFile($checkExistence = true) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Build a path from multiple strings. |
||
| 137 | * |
||
| 138 | * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use. |
||
| 139 | * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 4 | View Code Duplication | private function buildPath() |
| 157 | |||
| 158 | /** |
||
| 159 | * Convert a Windows path into a blasphemous Unix path. |
||
| 160 | * |
||
| 161 | * @param string $filePath |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | 4 | private function unixifyPath($filePath) |
|
| 169 | } |
||
| 170 |