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 |
||
11 | class Config |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $user; |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $host; |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $port; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $password; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $dbName; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $charset; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $gtid; |
||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $slaveId; |
||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $binLogFileName; |
||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $binLogPosition; |
||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $eventsOnly; |
||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $eventsIgnore; |
||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $tablesOnly; |
||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $databasesOnly; |
||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $mariaDbGtid; |
||
73 | /** |
||
74 | * @var int |
||
75 | */ |
||
76 | private $tableCacheSize; |
||
77 | |||
78 | /** |
||
79 | * Config constructor. |
||
80 | * @param string $user |
||
81 | * @param string $host |
||
82 | * @param int $port |
||
83 | * @param string $password |
||
84 | * @param string $dbName |
||
85 | * @param string $charset |
||
86 | * @param string $gtid |
||
87 | * @param string $mariaGtid |
||
88 | * @param int $slaveId |
||
89 | * @param string $binLogFileName |
||
90 | * @param $binLogPosition |
||
91 | * @param array $eventsOnly |
||
92 | * @param array $eventsIgnore |
||
93 | * @param array $tablesOnly |
||
94 | * @param array $databasesOnly |
||
95 | * @param int $tableCacheSize |
||
96 | */ |
||
97 | public function __construct( |
||
132 | |||
133 | /** |
||
134 | * @throws ConfigException |
||
135 | */ |
||
136 | public function validate() |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getUser() |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getHost() |
||
213 | |||
214 | /** |
||
215 | * @return int |
||
216 | */ |
||
217 | public function getPort() |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getPassword() |
||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getDbName() |
||
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getCharset() |
||
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | */ |
||
249 | public function getGtid() |
||
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getMariaDbGtid() |
||
261 | |||
262 | /** |
||
263 | * @return int |
||
264 | */ |
||
265 | public function getSlaveId() |
||
269 | |||
270 | /** |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getBinLogFileName() |
||
277 | |||
278 | /** |
||
279 | * @return int |
||
280 | */ |
||
281 | public function getBinLogPosition() |
||
285 | |||
286 | /** |
||
287 | * @return array |
||
288 | */ |
||
289 | public function getEventsOnly() |
||
293 | |||
294 | /** |
||
295 | * @return array |
||
296 | */ |
||
297 | public function getEventsIgnore() |
||
301 | |||
302 | /** |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getTablesOnly() |
||
309 | |||
310 | /** |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getDatabasesOnly() |
||
317 | |||
318 | /** |
||
319 | * @return int |
||
320 | */ |
||
321 | public function getTableCacheSize() |
||
325 | } |
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.