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:
Complex classes like File often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class File implements \Countable |
||
19 | { |
||
20 | const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR; |
||
21 | |||
22 | /** @var string the file path. */ |
||
23 | private $path; |
||
24 | |||
25 | /** |
||
26 | * Constructs a File object with the given path. |
||
27 | * |
||
28 | * @param string $path |
||
29 | */ |
||
30 | 32 | public function __construct($path) |
|
38 | |||
39 | /** |
||
40 | * Returns the string representation of the File object. |
||
41 | * |
||
42 | * @return string the string representation of the File object. |
||
43 | */ |
||
44 | 2 | public function __toString() |
|
48 | |||
49 | /** |
||
50 | * Returns the path of the file. |
||
51 | * |
||
52 | * @return string the path of the file. |
||
53 | */ |
||
54 | 5 | public function getPath() |
|
58 | |||
59 | /** |
||
60 | * Returns the parent directory of the file. |
||
61 | * |
||
62 | * @return string the parent directory of the file. |
||
63 | */ |
||
64 | 2 | public function getDirectory() |
|
68 | |||
69 | /** |
||
70 | * Returns the name of the file. |
||
71 | * |
||
72 | * @return string the name of the file. |
||
73 | */ |
||
74 | 1 | public function getName() |
|
78 | |||
79 | /** |
||
80 | * Returns true if the file exists. |
||
81 | * |
||
82 | * @return bool true if the file exists. |
||
83 | */ |
||
84 | 7 | public function exists() |
|
88 | |||
89 | /** |
||
90 | * Returns true if you can execute the file. |
||
91 | * |
||
92 | * @return bool true if you can execute the file. |
||
93 | */ |
||
94 | 1 | public function canExecute() |
|
98 | |||
99 | /** |
||
100 | * Returns true if you can read the file. |
||
101 | * |
||
102 | * @return bool true if you can read the file. |
||
103 | */ |
||
104 | 1 | public function canRead() |
|
108 | |||
109 | /** |
||
110 | * Returns true if you can write the file. |
||
111 | * |
||
112 | * @return bool true if you can write the file. |
||
113 | */ |
||
114 | 1 | public function canWrite() |
|
118 | |||
119 | /** |
||
120 | * Returns true if the file is a file. |
||
121 | * |
||
122 | * @return bool true if the file is a file. |
||
123 | */ |
||
124 | 2 | public function isFile() |
|
128 | |||
129 | /** |
||
130 | * Returns true if the file is a directory. |
||
131 | * |
||
132 | * @return bool true if the file is a directory. |
||
133 | */ |
||
134 | 5 | public function isDirectory() |
|
138 | |||
139 | /** |
||
140 | * Returns the numer of bytes in the file, or -1 on failure. |
||
141 | * |
||
142 | * @return int the number of bytes in the file, or -1 on failure. |
||
143 | */ |
||
144 | 1 | View Code Duplication | public function count() |
152 | |||
153 | /** |
||
154 | * Returns the time of the last modification as a unixtimestap, or -1 on failure. |
||
155 | * |
||
156 | * @return int the time of the last modification as a unixtimestap, or -1 on failure. |
||
157 | */ |
||
158 | 1 | View Code Duplication | public function lastModified() |
166 | |||
167 | /** |
||
168 | * Returns an iterator with the files and directories in the current directory. |
||
169 | * |
||
170 | * @param bool $recursive = false |
||
171 | * @param bool $showHidden = false |
||
172 | * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory. |
||
173 | */ |
||
174 | 4 | private function listAllIterator($recursive = false, $showHidden = false) |
|
192 | |||
193 | /** |
||
194 | * Returns an array with the files and directories in the current directory. |
||
195 | * |
||
196 | * @param bool $recursive = false |
||
197 | * @param bool $showHidden = false |
||
198 | * @return string[] an array with the files and directories in the current directory. |
||
199 | */ |
||
200 | 2 | View Code Duplication | public function listAll($recursive = false, $showHidden = false) |
210 | |||
211 | /** |
||
212 | * Returns an array with the directories in the current directory. |
||
213 | * |
||
214 | * @param bool $recursive = false |
||
215 | * @param bool $showHidden = false |
||
216 | * @return string[] an array with the directories in the current directory. |
||
217 | */ |
||
218 | 1 | View Code Duplication | public function listDirectories($recursive = false, $showHidden = false) |
230 | |||
231 | /** |
||
232 | * Returns an array with the files in the current directory. |
||
233 | * |
||
234 | * @param bool $recursive = false |
||
235 | * @param bool $showHidden = false |
||
236 | * @return string[] an array with the files in the current directory. |
||
237 | */ |
||
238 | 1 | View Code Duplication | public function listFiles($recursive = false, $showHidden = false) |
250 | |||
251 | /** |
||
252 | * Returns true if the file has been created. |
||
253 | * |
||
254 | * @param bool $override = false |
||
255 | * @return bool true if the file has been created. |
||
256 | */ |
||
257 | 1 | public function makeFile($override = false) |
|
265 | |||
266 | /** |
||
267 | * Returns true if the directory has been created. |
||
268 | * |
||
269 | * @param bool $recursive = false |
||
270 | * @param int $permissions = 0755 |
||
271 | * @return bool true if the directory has been created. |
||
272 | */ |
||
273 | 1 | public function makeDirectory($recursive = false, $permissions = 0775) |
|
285 | |||
286 | /** |
||
287 | * Returns true if the file is succesfully moved. |
||
288 | * |
||
289 | * @param string $path |
||
290 | * @param bool $override = false |
||
291 | * @return bool true if the file is succesfully moved. |
||
292 | */ |
||
293 | 2 | public function move($path, $override = false) |
|
309 | |||
310 | /** |
||
311 | * Returns true if the file is succesfully renamed. |
||
312 | * |
||
313 | * @param string $file |
||
314 | * @param bool $override = false |
||
315 | * @return bool true if the file is succesfully renamed. |
||
316 | */ |
||
317 | 1 | public function rename($file, $override = false) |
|
321 | |||
322 | /** |
||
323 | * Returns true if the directory is succesfully removed. |
||
324 | * |
||
325 | * @param bool $recursive = false |
||
326 | * @return bool true if the directory is succesfully removed. |
||
327 | */ |
||
328 | 1 | public function removeDirectory($recursive = false) |
|
340 | |||
341 | /** |
||
342 | * Returns true if the file is succesfully removed. |
||
343 | * |
||
344 | * @return bool true if the file is succesfully removed. |
||
345 | */ |
||
346 | 1 | public function removeFile() |
|
354 | |||
355 | /** |
||
356 | * Returns the content of the file. |
||
357 | * |
||
358 | * @return string the content of the file. |
||
359 | * @throws FileException on failure. |
||
360 | */ |
||
361 | 7 | public function read() |
|
371 | |||
372 | /** |
||
373 | * Append the given content. |
||
374 | * |
||
375 | * @param string $content |
||
376 | * @return null |
||
377 | * @throws FileException on failure. |
||
378 | */ |
||
379 | 3 | public function append($content) |
|
385 | |||
386 | /** |
||
387 | * Write the given content. |
||
388 | * |
||
389 | * @param string $content |
||
390 | * @return null |
||
391 | * @throws FileException on failure. |
||
392 | */ |
||
393 | 3 | public function write($content) |
|
399 | } |
||
400 |
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.