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 |
||
14 | abstract class Uploader { |
||
15 | |||
16 | private static $base_name = null, $file_name = null; |
||
17 | |||
18 | /** |
||
19 | * Convert a PHP upload error to a usable error code |
||
20 | */ |
||
21 | |||
22 | private static function getErrorCode(int $error) : string { |
||
42 | |||
43 | /** |
||
44 | * Display an error message |
||
45 | * |
||
46 | * @return false : the method always returns false |
||
47 | */ |
||
48 | |||
49 | View Code Duplication | private static function displayError(string $phrase, bool $popup) : bool { |
|
59 | |||
60 | /** |
||
61 | * Save an uploaded file |
||
62 | * |
||
63 | * @return true|string|false : true on success, an error code on failure, or false if there are no uploaded files |
||
64 | */ |
||
65 | |||
66 | public static function save(string $name, string $dir_name) { |
||
112 | |||
113 | /** |
||
114 | * Save an uploaded file and display an error if appeared |
||
115 | * |
||
116 | * @param $popup : tells to display a popup error message instead of a regular message |
||
117 | * |
||
118 | * @return bool : true on success or false on failure |
||
119 | */ |
||
120 | |||
121 | public static function handle(string $name, string $dir_name, bool $popup = false) : bool { |
||
131 | |||
132 | /** |
||
133 | * Get a basename of a last successfully uploaded file |
||
134 | */ |
||
135 | |||
136 | public static function getBasename() : string { |
||
140 | |||
141 | /** |
||
142 | * Get a filename of a last successfully uploaded file |
||
143 | */ |
||
144 | |||
145 | public static function getFilename() : string { |
||
149 | } |
||
150 | } |
||
151 |
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.