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 Filesystem */ |
||
16 | private $fs; |
||
|
|||
17 | /** @var string */ |
||
18 | private $absolutePath; |
||
19 | /** @var string */ |
||
20 | private $originalPath; |
||
21 | /** @var bool */ |
||
22 | private $isWindows; |
||
23 | |||
24 | /** |
||
25 | * @param string $filePath |
||
26 | * @param string $dirSep |
||
27 | */ |
||
28 | 42 | public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR) |
|
41 | |||
42 | 4 | public function __toString() |
|
46 | |||
47 | /** |
||
48 | * Append a path to a directory path. |
||
49 | * |
||
50 | * @param string $append The path to append |
||
51 | */ |
||
52 | 5 | public function appendToPath($append) |
|
61 | |||
62 | /** |
||
63 | * Generate a path based off this file path. |
||
64 | * |
||
65 | * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that. |
||
66 | * |
||
67 | * @param string $append |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 36 | public function generatePath($append) |
|
75 | |||
76 | /** |
||
77 | * Get the absolute path of the file path. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 4 | public function getAbsolutePath() |
|
90 | |||
91 | /** |
||
92 | * Check whether the given path is a directory. |
||
93 | * |
||
94 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory. |
||
95 | * When set to false, this function will guess based on the path ending in a directory |
||
96 | * separator. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 42 | public function isDir($checkExistence = true) |
|
111 | |||
112 | /** |
||
113 | * Check whether the given path is a file. |
||
114 | * |
||
115 | * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When |
||
116 | * set to false, this function will guess based on the path ending in a directory |
||
117 | * separator. |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 5 | public function isFile($checkExistence = true) |
|
132 | |||
133 | /** |
||
134 | * Build a path from multiple strings. |
||
135 | * |
||
136 | * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use. |
||
137 | * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 40 | View Code Duplication | private function buildPath() |
155 | |||
156 | /** |
||
157 | * Convert a Windows path into a blasphemous Unix path. |
||
158 | * |
||
159 | * @param string $filePath |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 40 | private function unixifyPath($filePath) |
|
167 | } |
||
168 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.