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 |
||
9 | class Configuration |
||
10 | { |
||
11 | /** |
||
12 | * Configuration file name. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected static $configFileName = '.press-cli.json'; |
||
17 | |||
18 | /** |
||
19 | * Creates the configuration file. |
||
20 | * |
||
21 | * @param string $directory |
||
22 | * @param string $name |
||
23 | * @param InputInterface $input |
||
24 | * @param OutputInterface $output |
||
25 | * @param QuestionHelper $helper |
||
26 | */ |
||
27 | public static function create($directory, $name, InputInterface $input, OutputInterface $output, QuestionHelper $helper) |
||
52 | |||
53 | /** |
||
54 | * Writes the configuration to the file. |
||
55 | * |
||
56 | * @param array $config |
||
57 | * @param string $configFile |
||
58 | */ |
||
59 | protected static function write($config, $configFile = null) |
||
66 | |||
67 | /** |
||
68 | * Removes the user password. |
||
69 | */ |
||
70 | public static function cleanUpConfigForVCS() |
||
101 | |||
102 | /** |
||
103 | * Get the configuration. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | View Code Duplication | public static function get() |
|
117 | |||
118 | /** |
||
119 | * Configuration skeleton |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | protected static function configSkeleton($name, InputInterface $input, OutputInterface $output, QuestionHelper $helper) |
||
147 | |||
148 | /** |
||
149 | * Removes duplicate plugins. |
||
150 | * |
||
151 | * @param array $config |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | protected static function removeDuplicatePlugins($config) |
||
167 | |||
168 | /** |
||
169 | * Merges the global configuration into the local configuration. |
||
170 | * |
||
171 | * @param array $config1 |
||
172 | * @param array $config2 |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | protected static function mergeConfiguration($config1, $config2) |
||
188 | |||
189 | /** |
||
190 | * Creates the database name from the initialization name. |
||
191 | * |
||
192 | * @param string $name The name. |
||
193 | * |
||
194 | * @return string The database name. |
||
195 | */ |
||
196 | View Code Duplication | protected static function makeDbName($name) |
|
204 | |||
205 | /** |
||
206 | * Creates the URL from the initialization name. |
||
207 | * |
||
208 | * @param string $name The name. |
||
209 | * |
||
210 | * @return string The URL. |
||
211 | */ |
||
212 | View Code Duplication | protected static function makeURL($name) |
|
220 | |||
221 | /** |
||
222 | * Creates the theme name from the initialization name. |
||
223 | * |
||
224 | * @param string $name The name. |
||
225 | * |
||
226 | * @return string The theme name. |
||
227 | */ |
||
228 | View Code Duplication | protected static function makeThemeName($name) |
|
236 | |||
237 | /** |
||
238 | * Creates the database name from the initialization name. |
||
239 | * |
||
240 | * @param string $value The value. |
||
241 | * |
||
242 | * @return string The sanitized value. |
||
243 | */ |
||
244 | protected static function sanitizeValue($value) |
||
250 | |||
251 | /** |
||
252 | * Checks if the configuration exists. |
||
253 | * |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public static function exists() |
||
260 | |||
261 | /** |
||
262 | * Empty configuration values. |
||
263 | * |
||
264 | * @param array $config The configuration. |
||
265 | * |
||
266 | * @return array The emptied configuration. |
||
267 | */ |
||
268 | protected static function emptyConfigValues($config) { |
||
278 | |||
279 | /** |
||
280 | * Empties the configuration. |
||
281 | * |
||
282 | * @param array $config The configuration. |
||
283 | * |
||
284 | * @return array The emptied configuration. |
||
285 | */ |
||
286 | protected static function emptyConfig($config) { |
||
298 | } |
||
299 |
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.