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 |
||
16 | class File extends \SplFileInfo |
||
17 | { |
||
18 | /** |
||
19 | * @param string $path The path to the file |
||
20 | * @param bool $checkPath Whether to check the path or not |
||
21 | * @throws FileNotFoundException |
||
22 | */ |
||
23 | public function __construct(string $path, bool $checkPath = true) |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | public function guessExtension(): string |
||
39 | |||
40 | /** |
||
41 | * @return string |
||
42 | */ |
||
43 | public function guessMimeType(): string |
||
47 | |||
48 | /** |
||
49 | * Moves the file to a new location. |
||
50 | * |
||
51 | * @param string $directory Destination folder |
||
52 | * @param string $name New file name |
||
53 | * @return File |
||
54 | * @throws FileException |
||
55 | */ |
||
56 | public function move(string $directory, string $name = ''): File |
||
69 | |||
70 | /** |
||
71 | * @param string $directory |
||
72 | * @param string $name |
||
73 | * @return File |
||
74 | * @throws FileException |
||
75 | */ |
||
76 | protected function getTargetFile(string $directory, string $name = ''): File |
||
90 | |||
91 | /** |
||
92 | * Chmod function with exception |
||
93 | * |
||
94 | * @param $target |
||
95 | * @param $mode |
||
96 | * @throws FileException |
||
97 | */ |
||
98 | protected function customChmod(string $target, $mode = 0666) |
||
104 | |||
105 | /** |
||
106 | * Returns locale independent base name of the given path. |
||
107 | * |
||
108 | * @param string $name The new file name |
||
109 | * @return string containing |
||
110 | */ |
||
111 | protected function getName(string $name): string |
||
119 | } |
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.