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 MySql 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 MySql, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class MySql extends DbDumper |
||
10 | { |
||
11 | /** @var bool */ |
||
12 | protected $skipComments = true; |
||
13 | |||
14 | /** @var bool */ |
||
15 | protected $useExtendedInserts = true; |
||
16 | |||
17 | /** @var bool */ |
||
18 | protected $useSingleTransaction = false; |
||
19 | |||
20 | /** @var bool */ |
||
21 | protected $skipLockTables = false; |
||
22 | |||
23 | /** @var bool */ |
||
24 | protected $doNotUseColumnStatistics = false; |
||
25 | |||
26 | /** @var bool */ |
||
27 | protected $useQuick = false; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $defaultCharacterSet = ''; |
||
31 | |||
32 | /** @var bool */ |
||
33 | protected $dbNameWasSetAsExtraOption = false; |
||
34 | |||
35 | /** @var bool */ |
||
36 | protected $allDatabasesWasSetAsExtraOption = false; |
||
37 | |||
38 | /** @var string */ |
||
39 | protected $setGtidPurged = 'AUTO'; |
||
40 | |||
41 | /** @var bool */ |
||
42 | protected $createTables = true; |
||
43 | |||
44 | public function __construct() |
||
48 | |||
49 | /** |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function skipComments() |
||
58 | |||
59 | /** |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function dontSkipComments() |
||
68 | |||
69 | /** |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function useExtendedInserts() |
||
78 | |||
79 | /** |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function dontUseExtendedInserts() |
||
88 | |||
89 | /** |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function useSingleTransaction() |
||
98 | |||
99 | /** |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function dontUseSingleTransaction() |
||
108 | |||
109 | /** |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function skipLockTables() |
||
118 | |||
119 | /** |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function doNotUseColumnStatistics() |
||
123 | { |
||
124 | $this->doNotUseColumnStatistics = true; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function dontSkipLockTables() |
||
138 | |||
139 | /** |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function useQuick() |
||
148 | |||
149 | /** |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function dontUseQuick() |
||
158 | |||
159 | /** |
||
160 | * @param string $characterSet |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setDefaultCharacterSet(string $characterSet) |
||
170 | |||
171 | /** |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function setGtidPurged(string $setGtidPurged) |
||
180 | |||
181 | /** |
||
182 | * Dump the contents of the database to the given file. |
||
183 | * |
||
184 | * @param string $dumpFile |
||
185 | * |
||
186 | * @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
||
187 | * @throws \Spatie\DbDumper\Exceptions\DumpFailed |
||
188 | */ |
||
189 | View Code Duplication | public function dumpToFile(string $dumpFile) |
|
205 | |||
206 | public function addExtraOption(string $extraOption) |
||
220 | |||
221 | /** |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function doNotCreateTables() |
||
230 | |||
231 | /** |
||
232 | * Get the command that should be performed to dump the database. |
||
233 | * |
||
234 | * @param string $dumpFile |
||
235 | * @param string $temporaryCredentialsFile |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string |
||
309 | |||
310 | public function getContentsOfCredentialsFile(): string |
||
322 | |||
323 | protected function guardAgainstIncompleteCredentials() |
||
335 | } |
||
336 |
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.