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 |
||
10 | class FileInfo extends \SplFileInfo |
||
11 | { |
||
12 | const SEPARATOR_DIRECTORY = DIRECTORY_SEPARATOR; |
||
13 | const SEPARATOR_EXTENSION = '.'; |
||
14 | |||
15 | /** |
||
16 | * Returns base name of file without extension (or base name of directory). |
||
17 | * |
||
18 | * @param string $suffix |
||
19 | * |
||
20 | * @return string |
||
21 | */ |
||
22 | 13 | public function getBasename($suffix = null) |
|
30 | |||
31 | /** |
||
32 | * Checks whether a file / directory exists. |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | 1 | public function isExists() |
|
40 | |||
41 | /** |
||
42 | * Creates full pathname to the file / directory based on its path, basename and extension |
||
43 | * |
||
44 | * @param string $path Directory path to the file (or directory) |
||
45 | * @param string $basename Base name of file without extension (or base name of directory) |
||
46 | * @param string $extension [OPTIONAL] File extension (empty for directory) |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 13 | public static function createPathname($path, $basename, $extension = '') |
|
66 | |||
67 | /** |
||
68 | * @param string $path |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | 16 | public static function purifyPath($path) |
|
80 | |||
81 | /** |
||
82 | * @param string $basename |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 14 | public static function purifyBasename($basename) |
|
94 | |||
95 | /** |
||
96 | * @param string $extension |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 14 | public static function purifyExtension($extension) |
|
108 | |||
109 | /** |
||
110 | * Changes directory path to the file / directory. |
||
111 | * |
||
112 | * @param string $path |
||
113 | * |
||
114 | * @return FileInfo |
||
115 | */ |
||
116 | 10 | public function changePath($path) |
|
122 | |||
123 | /** |
||
124 | * Changes base name of file without extension (or base name of directory). |
||
125 | * |
||
126 | * @param string $basename |
||
127 | * |
||
128 | * @return FileInfo |
||
129 | */ |
||
130 | 10 | public function changeBasename($basename) |
|
136 | |||
137 | /** |
||
138 | * Changes file extension. |
||
139 | * |
||
140 | * @param string $extension |
||
141 | * |
||
142 | * @return FileInfo |
||
143 | */ |
||
144 | 1 | public function changeExtension($extension) |
|
150 | |||
151 | /** |
||
152 | * @param string $basePath |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 1 | View Code Duplication | public function getPathRelativeTo($basePath) |
168 | |||
169 | /** |
||
170 | * @param string $basePathname |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 5 | View Code Duplication | public function getPathnameRelativeTo($basePathname) |
186 | |||
187 | /** |
||
188 | * Converts object to a string representation. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 11 | public function toString() |
|
196 | } |
||
197 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.