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 Config 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Config |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private static $user; |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private static $host; |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private static $port; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private static $password; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private static $charset; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private static $gtid; |
||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private static $slaveId; |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private static $binLogFileName; |
||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | private static $binLogPosition; |
||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private static $eventsOnly; |
||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private static $eventsIgnore; |
||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private static $tablesOnly; |
||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $databasesOnly; |
||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | private static $mariaDbGtid; |
||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | private static $tableCacheSize; |
||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $custom; |
||
75 | /** |
||
76 | * @var int |
||
77 | */ |
||
78 | private static $heartbeatPeriod; |
||
79 | |||
80 | /** |
||
81 | * Config constructor. |
||
82 | * @param string $user |
||
83 | * @param string $host |
||
84 | * @param int $port |
||
85 | * @param string $password |
||
86 | * @param string $charset |
||
87 | * @param string $gtid |
||
88 | * @param string $mariaGtid |
||
89 | * @param int $slaveId |
||
90 | * @param string $binLogFileName |
||
91 | * @param $binLogPosition |
||
92 | * @param array $eventsOnly |
||
93 | * @param array $eventsIgnore |
||
94 | * @param array $tablesOnly |
||
95 | * @param array $databasesOnly |
||
96 | * @param int $tableCacheSize |
||
97 | * @param array $custom |
||
98 | * @param int $heartbeatPeriod |
||
99 | */ |
||
100 | 54 | public function __construct( |
|
137 | |||
138 | /** |
||
139 | * @throws ConfigException |
||
140 | */ |
||
141 | 54 | public static function validate() |
|
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | public static function getCustom() |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | 54 | public static function getUser() |
|
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | */ |
||
232 | 54 | public static function getHost() |
|
236 | |||
237 | /** |
||
238 | * @return int |
||
239 | */ |
||
240 | 54 | public static function getPort() |
|
244 | |||
245 | /** |
||
246 | * @return string |
||
247 | */ |
||
248 | 54 | public static function getPassword() |
|
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | 54 | public static function getCharset() |
|
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | 54 | public static function getGtid() |
|
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | 54 | public static function getMariaDbGtid() |
|
276 | |||
277 | /** |
||
278 | * @return int |
||
279 | */ |
||
280 | 54 | public static function getSlaveId() |
|
284 | |||
285 | /** |
||
286 | * @return string |
||
287 | */ |
||
288 | 54 | public static function getBinLogFileName() |
|
292 | |||
293 | /** |
||
294 | * @return int |
||
295 | */ |
||
296 | 54 | public static function getBinLogPosition() |
|
300 | |||
301 | /** |
||
302 | * @return array |
||
303 | */ |
||
304 | 54 | public static function getEventsOnly() |
|
308 | |||
309 | /** |
||
310 | * @return array |
||
311 | */ |
||
312 | 54 | public static function getEventsIgnore() |
|
316 | |||
317 | /** |
||
318 | * @return array |
||
319 | */ |
||
320 | 52 | public static function getTablesOnly() |
|
324 | |||
325 | /** |
||
326 | * @return array |
||
327 | */ |
||
328 | 52 | public static function getDatabasesOnly() |
|
332 | |||
333 | /** |
||
334 | * @return int |
||
335 | */ |
||
336 | 52 | public static function getTableCacheSize() |
|
340 | |||
341 | /** |
||
342 | * @param string $database |
||
343 | * @return bool |
||
344 | */ |
||
345 | 52 | public static function checkDataBasesOnly($database) |
|
349 | |||
350 | /** |
||
351 | * @param string $table |
||
352 | * @return bool |
||
353 | */ |
||
354 | 52 | public static function checkTablesOnly($table) |
|
358 | |||
359 | /** |
||
360 | * @param int $type |
||
361 | * @return bool |
||
362 | */ |
||
363 | 54 | public static function checkEvent($type) |
|
375 | |||
376 | /** |
||
377 | * @return int |
||
378 | */ |
||
379 | 54 | public static function getHeartbeatPeriod() |
|
383 | } |
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.