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 |
||
10 | class MySql extends DbDumper |
||
11 | { |
||
12 | protected $dbName; |
||
13 | protected $userName; |
||
14 | protected $password; |
||
15 | protected $host = 'localhost'; |
||
16 | protected $port = 3306; |
||
17 | protected $socket; |
||
18 | protected $dumpBinaryPath = ''; |
||
19 | protected $useExtendedInserts = true; |
||
20 | protected $useSingleTransaction = false; |
||
21 | protected $tables = array(); |
||
22 | protected $excludeTables = array(); |
||
23 | protected $timeout; |
||
24 | |||
25 | /** |
||
26 | * @return string |
||
27 | */ |
||
28 | public function getDbName() |
||
32 | |||
33 | /** |
||
34 | * @param string $dbName |
||
35 | * |
||
36 | * @return \Spatie\DbDumper\Databases\MySql |
||
37 | */ |
||
38 | public function setDbName($dbName) |
||
44 | |||
45 | /** |
||
46 | * @param string $userName |
||
47 | * |
||
48 | * @return \Spatie\DbDumper\Databases\MySql |
||
49 | */ |
||
50 | public function setUserName($userName) |
||
56 | |||
57 | /** |
||
58 | * @param string $password |
||
59 | * |
||
60 | * @return \Spatie\DbDumper\Databases\MySql |
||
61 | */ |
||
62 | public function setPassword($password) |
||
68 | |||
69 | /** |
||
70 | * @param string $host |
||
71 | * |
||
72 | * @return \Spatie\DbDumper\Databases\MySql |
||
73 | */ |
||
74 | public function setHost($host) |
||
80 | |||
81 | /** |
||
82 | * @param int $port |
||
83 | * |
||
84 | * @return \Spatie\DbDumper\Databases\MySql |
||
85 | */ |
||
86 | public function setPort($port) |
||
92 | |||
93 | /** |
||
94 | * @param int $socket |
||
95 | * |
||
96 | * @return \Spatie\DbDumper\Databases\MySql |
||
97 | */ |
||
98 | public function setSocket($socket) |
||
104 | |||
105 | /** |
||
106 | * @param int $timeout |
||
107 | * |
||
108 | * @return \Spatie\DbDumper\Databases\PostgreSql |
||
109 | */ |
||
110 | public function setTimeout($timeout) |
||
116 | |||
117 | /** |
||
118 | * @param string $dumpBinaryPath |
||
119 | * |
||
120 | * @return \Spatie\DbDumper\Databases\MySql |
||
121 | */ |
||
122 | View Code Duplication | public function setDumpBinaryPath($dumpBinaryPath) |
|
132 | |||
133 | /** |
||
134 | * @param string/array $tables |
||
135 | * |
||
136 | * @return \Spatie\DbDumper\Databases\MySql |
||
137 | */ |
||
138 | View Code Duplication | public function setTables($tables) |
|
154 | |||
155 | /** |
||
156 | * @param string/array $tables |
||
157 | * |
||
158 | * @return \Spatie\DbDumper\Databases\MySql |
||
159 | */ |
||
160 | View Code Duplication | public function setExcludeTables($tables) |
|
176 | |||
177 | /** |
||
178 | * @return \Spatie\DbDumper\Databases\MySql |
||
179 | */ |
||
180 | public function useExtendedInserts() |
||
186 | |||
187 | /** |
||
188 | * @return \Spatie\DbDumper\Databases\MySql |
||
189 | */ |
||
190 | public function dontUseExtendedInserts() |
||
196 | |||
197 | /** |
||
198 | * @return \Spatie\DbDumper\Databases\MySql |
||
199 | */ |
||
200 | public function useSingleTransaction() |
||
206 | |||
207 | /** |
||
208 | * @return \Spatie\DbDumper\Databases\MySql |
||
209 | */ |
||
210 | public function dontUseSingleTransaction() |
||
216 | |||
217 | /** |
||
218 | * Dump the contents of the database to the given file. |
||
219 | * |
||
220 | * @param string $dumpFile |
||
221 | * |
||
222 | * @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
||
223 | * @throws \Spatie\DbDumper\Exceptions\DumpFailed |
||
224 | */ |
||
225 | View Code Duplication | public function dumpToFile($dumpFile) |
|
245 | |||
246 | /** |
||
247 | * Get the command that should be performed to dump the database. |
||
248 | * |
||
249 | * @param string $dumpFile |
||
250 | * @param string $temporaryCredentialsFile |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getDumpCommand($dumpFile, $temporaryCredentialsFile) |
||
285 | |||
286 | /** |
||
287 | * @return string |
||
288 | */ |
||
289 | public function getContentsOfCredentialsFile() |
||
301 | |||
302 | View Code Duplication | protected function guardAgainstIncompleteCredentials() |
|
310 | } |
||
311 |
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.