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 TraitForFileSystem 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 TraitForFileSystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | View Code Duplication | trait TraitForFileSystem |
|
|
|||
30 | { |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * {@inheritDoc} |
||
35 | * |
||
36 | */ |
||
37 | 6 | public function cd($path = false, $validate_dir_name = false) |
|
41 | |||
42 | /** |
||
43 | * rmdir |
||
44 | * |
||
45 | * {@inheritDoc} |
||
46 | * |
||
47 | */ |
||
48 | 2 | public function rm($pathname, $shred = false) |
|
77 | |||
78 | /** |
||
79 | * |
||
80 | * {@inheritDoc} |
||
81 | * |
||
82 | */ |
||
83 | 1 | public function scandir($path = false) |
|
87 | |||
88 | /** |
||
89 | * |
||
90 | * {@inheritDoc} |
||
91 | * |
||
92 | */ |
||
93 | 1 | public function stat($filename = false) |
|
98 | |||
99 | /** |
||
100 | * |
||
101 | * {@inheritDoc} |
||
102 | * |
||
103 | */ |
||
104 | 1 | public function getDiskTotalSpace($partition_location = false, $convert = false) |
|
132 | |||
133 | /** |
||
134 | * |
||
135 | * {@inheritDoc} |
||
136 | * |
||
137 | */ |
||
138 | 1 | public function getDiskFreeSpace($partition_location = false, $convert = false) |
|
174 | |||
175 | /** |
||
176 | * |
||
177 | * {@inheritDoc} |
||
178 | * |
||
179 | */ |
||
180 | 1 | public function chgrp($filename = false, $group = false) |
|
185 | |||
186 | /** |
||
187 | * |
||
188 | * {@inheritDoc} |
||
189 | * |
||
190 | */ |
||
191 | 1 | public function chmod($filename = false, $mode = false) |
|
199 | |||
200 | /** |
||
201 | * |
||
202 | * {@inheritDoc} |
||
203 | * |
||
204 | */ |
||
205 | 1 | public function chown($filename = false, $user = false) |
|
213 | |||
214 | /** |
||
215 | * |
||
216 | * {@inheritDoc} |
||
217 | * |
||
218 | */ |
||
219 | 2 | public function clearstatcache($clear_realpath_cache = false, $filename = false) |
|
226 | |||
227 | /** |
||
228 | * |
||
229 | * {@inheritDoc} |
||
230 | * |
||
231 | */ |
||
232 | 1 | public function copy($source = false, $dest = false, $context = false) |
|
278 | |||
279 | /** |
||
280 | * |
||
281 | * {@inheritDoc} |
||
282 | * |
||
283 | */ |
||
284 | 1 | public function mv($oldname = false, $newname = false, $context = false) |
|
297 | |||
298 | /** |
||
299 | * |
||
300 | * {@inheritDoc} |
||
301 | * |
||
302 | */ |
||
303 | 1 | public function namePatternMatch($pattern = false, $string = false, $flags = false) |
|
313 | |||
314 | /** |
||
315 | * |
||
316 | * {@inheritDoc} |
||
317 | * |
||
318 | */ |
||
319 | 1 | public function getGlob($pattern = false, $flags = 0) |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * {@inheritDoc} |
||
327 | * |
||
328 | */ |
||
329 | 1 | public function isUploadedFile($filename = false) |
|
334 | |||
335 | /** |
||
336 | * |
||
337 | * {@inheritDoc} |
||
338 | * |
||
339 | */ |
||
340 | 1 | public function getRealpathCache() |
|
344 | |||
345 | /** |
||
346 | * |
||
347 | * {@inheritDoc} |
||
348 | * |
||
349 | */ |
||
350 | 1 | public function getRealpathCacheSize() |
|
354 | |||
355 | /** |
||
356 | * |
||
357 | * {@inheritDoc} |
||
358 | * |
||
359 | */ |
||
360 | 2 | public function close($keyword = false) |
|
375 | |||
376 | /** |
||
377 | * |
||
378 | * {@inheritDoc} |
||
379 | * |
||
380 | */ |
||
381 | 1 | public function touch($filename = false, $time = false, $atime = false) |
|
393 | |||
394 | /** |
||
395 | * |
||
396 | * {@inheritDoc} |
||
397 | * |
||
398 | */ |
||
399 | 1 | public function getDirKeys() |
|
403 | |||
404 | /** |
||
405 | * |
||
406 | * {@inheritDoc} |
||
407 | * |
||
408 | */ |
||
409 | 1 | public function getFileKeys() |
|
413 | |||
414 | /** |
||
415 | * |
||
416 | * {@inheritDoc} |
||
417 | * |
||
418 | */ |
||
419 | 2 | public function createStructure($rootpath, $data_array, $skip_debug = false) |
|
441 | } |
||
442 |
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.