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 |
||
22 | class Mysqldump extends SimulatorExecutable implements Simulator, Restorable |
||
23 | { |
||
24 | /** |
||
25 | * Path to mysql executable. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $pathToMysql; |
||
30 | |||
31 | /** |
||
32 | * Path to mysqldump executable. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $pathToMysqldump; |
||
37 | |||
38 | /** |
||
39 | * Path to mysqlimport executable. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $pathToMysqlimport; |
||
44 | |||
45 | /** |
||
46 | * Host to connect to |
||
47 | * --host <hostname> |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $host; |
||
52 | |||
53 | /** |
||
54 | * Port to connect to |
||
55 | * --port <port> |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $port; |
||
60 | |||
61 | /** |
||
62 | * Port to connect to |
||
63 | * --protocol <TCP|SOCKET|PIPE|MEMORY> |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $protocol; |
||
68 | |||
69 | /** |
||
70 | * User to connect with |
||
71 | * --user <username> |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $user; |
||
76 | |||
77 | /** |
||
78 | * Password to authenticate with |
||
79 | * --password <password> |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $password; |
||
84 | |||
85 | /** |
||
86 | * List of tables to backup |
||
87 | * --tables array of strings |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | private $tables; |
||
92 | |||
93 | /** |
||
94 | * List of databases to backup |
||
95 | * --databases array of strings |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | private $databases; |
||
100 | |||
101 | /** |
||
102 | * List of tables to ignore |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | private $ignoreTables; |
||
107 | |||
108 | /** |
||
109 | * List of tables where only the table structure is stored |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private $structureOnly; |
||
114 | |||
115 | /** |
||
116 | * Table separated data files |
||
117 | * --tab |
||
118 | * |
||
119 | * @var boolean |
||
120 | */ |
||
121 | private $filePerTable; |
||
122 | |||
123 | /** |
||
124 | * Use mysqldump quick mode |
||
125 | * -q |
||
126 | * |
||
127 | * @var boolean |
||
128 | */ |
||
129 | private $quick; |
||
130 | |||
131 | /** |
||
132 | * Lock tables option |
||
133 | * --lock-tables |
||
134 | * |
||
135 | * @var bool |
||
136 | */ |
||
137 | private $lockTables; |
||
138 | |||
139 | /** |
||
140 | * Single Transaction option |
||
141 | * --single-transaction |
||
142 | * |
||
143 | * @var bool |
||
144 | */ |
||
145 | private $singleTransaction; |
||
146 | |||
147 | /** |
||
148 | * Use mysqldump with compression |
||
149 | * -C |
||
150 | * |
||
151 | * @var boolean |
||
152 | */ |
||
153 | private $compress; |
||
154 | |||
155 | /** |
||
156 | * Use mysqldump with extended insert |
||
157 | * -e |
||
158 | * |
||
159 | * @var boolean |
||
160 | */ |
||
161 | private $extendedInsert; |
||
162 | |||
163 | /** |
||
164 | * Dump blob fields as hex. |
||
165 | * --hex-blob |
||
166 | * |
||
167 | * @var boolean |
||
168 | */ |
||
169 | private $hexBlob; |
||
170 | |||
171 | /** |
||
172 | * Dump only table structures |
||
173 | * --no-data |
||
174 | * |
||
175 | * @var boolean |
||
176 | */ |
||
177 | private $noData; |
||
178 | |||
179 | /** |
||
180 | * Add general transaction id statement. |
||
181 | * --set-gids-purged=['ON', 'OFF', 'AUTO'] |
||
182 | * |
||
183 | * @var string |
||
184 | */ |
||
185 | private $gtidPurged; |
||
186 | |||
187 | /** |
||
188 | * Dump procedures and functions. |
||
189 | * --routines |
||
190 | * |
||
191 | * @var bool |
||
192 | */ |
||
193 | private $routines; |
||
194 | |||
195 | /** |
||
196 | * Setup. |
||
197 | * |
||
198 | * @see \phpbu\App\Backup\Source |
||
199 | * @param array $conf |
||
200 | * @throws \phpbu\App\Exception |
||
201 | */ |
||
202 | 17 | public function setup(array $conf = []) |
|
230 | |||
231 | /** |
||
232 | * Get tables and databases to backup. |
||
233 | * |
||
234 | * @param array $conf |
||
235 | */ |
||
236 | 17 | View Code Duplication | protected function setupSourceData(array $conf) |
243 | |||
244 | /** |
||
245 | * Execute the backup. |
||
246 | * |
||
247 | * @see \phpbu\App\Backup\Source |
||
248 | * @param \phpbu\App\Backup\Target $target |
||
249 | * @param \phpbu\App\Result $result |
||
250 | * @return \phpbu\App\Backup\Source\Status |
||
251 | * @throws \phpbu\App\Exception |
||
252 | */ |
||
253 | 4 | public function backup(Target $target, Result $result) : Status |
|
272 | |||
273 | /** |
||
274 | * Restore the backup |
||
275 | * |
||
276 | * @param \phpbu\App\Backup\Target $target |
||
277 | * @param \phpbu\App\Backup\Restore\Plan $plan |
||
278 | * @return \phpbu\App\Backup\Source\Status |
||
279 | * @throws \phpbu\App\Exception |
||
280 | */ |
||
281 | 2 | public function restore(Target $target, Plan $plan): Status |
|
308 | |||
309 | /** |
||
310 | * Create the Executable to run the mysqldump command. |
||
311 | * |
||
312 | * @param \phpbu\App\Backup\Target $target |
||
313 | * @return \phpbu\App\Cli\Executable |
||
314 | */ |
||
315 | 14 | protected function createExecutable(Target $target) : Executable |
|
343 | |||
344 | /** |
||
345 | * Create backup status. |
||
346 | * |
||
347 | * @param \phpbu\App\Backup\Target $target |
||
348 | * @return \phpbu\App\Backup\Source\Status |
||
349 | */ |
||
350 | 4 | protected function createStatus(Target $target) : Status |
|
366 | |||
367 | /** |
||
368 | * Can compression be handled via pipe operator. |
||
369 | * |
||
370 | * @param \phpbu\App\Backup\Target $target |
||
371 | * @return bool |
||
372 | */ |
||
373 | 14 | private function isHandlingCompression(Target $target) : bool |
|
377 | |||
378 | /** |
||
379 | * Return dump target path. |
||
380 | * |
||
381 | * @param \phpbu\App\Backup\Target $target |
||
382 | * @return string |
||
383 | */ |
||
384 | 14 | private function getDumpTarget(Target $target) : string |
|
388 | |||
389 | /** |
||
390 | * Create the Executable to run the mysql command. |
||
391 | * |
||
392 | * @return \phpbu\App\Cli\Executable\Mysql |
||
393 | */ |
||
394 | 2 | View Code Duplication | private function createMysqlExecutable(): Executable\Mysql |
406 | |||
407 | /** |
||
408 | * Create the Executable to run the mysqlimport command. |
||
409 | * |
||
410 | * @param string $sourceFilename |
||
411 | * @param string $targetDatabase |
||
412 | * |
||
413 | * @return \phpbu\App\Cli\Executable\Mysqlimport |
||
414 | */ |
||
415 | 1 | View Code Duplication | private function createMysqlimportExecutable(string $sourceFilename, string $targetDatabase): Executable\Mysqlimport |
427 | } |
||
428 |