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 executable. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $pathToMysqldump; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Host to connect to |
||
| 33 | * --host <hostname> |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $host; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Port to connect to |
||
| 41 | * --port <port> |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $port; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Port to connect to |
||
| 49 | * --protocol <TCP|SOCKET|PIPE|MEMORY> |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $protocol; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * User to connect with |
||
| 57 | * --user <username> |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $user; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Password to authenticate with |
||
| 65 | * --password <password> |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $password; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * List of tables to backup |
||
| 73 | * --tables array of strings |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $tables; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * List of databases to backup |
||
| 81 | * --databases array of strings |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $databases; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * List of tables to ignore |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $ignoreTables; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * List of tables where only the table structure is stored |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $structureOnly; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Table separated data files |
||
| 103 | * --tab |
||
| 104 | * |
||
| 105 | * @var boolean |
||
| 106 | */ |
||
| 107 | private $filePerTable; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Use mysqldump quick mode |
||
| 111 | * -q |
||
| 112 | * |
||
| 113 | * @var boolean |
||
| 114 | */ |
||
| 115 | private $quick; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Lock tables option |
||
| 119 | * --lock-tables |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | private $lockTables; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Single Transaction option |
||
| 127 | * --single-transaction |
||
| 128 | * |
||
| 129 | * @var bool |
||
| 130 | */ |
||
| 131 | private $singleTransaction; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Use mysqldump with compression |
||
| 135 | * -C |
||
| 136 | * |
||
| 137 | * @var boolean |
||
| 138 | */ |
||
| 139 | private $compress; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Use mysqldump with extended insert |
||
| 143 | * -e |
||
| 144 | * |
||
| 145 | * @var boolean |
||
| 146 | */ |
||
| 147 | private $extendedInsert; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Dump blob fields as hex. |
||
| 151 | * --hex-blob |
||
| 152 | * |
||
| 153 | * @var boolean |
||
| 154 | */ |
||
| 155 | private $hexBlob; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Dump only table structures |
||
| 159 | * --no-data |
||
| 160 | * |
||
| 161 | * @var boolean |
||
| 162 | */ |
||
| 163 | private $noData; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add general transaction id statement. |
||
| 167 | * --set-gids-purged=['ON', 'OFF', 'AUTO'] |
||
| 168 | * |
||
| 169 | * @var string |
||
| 170 | */ |
||
| 171 | private $gtidPurged; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Dump procedures and functions. |
||
| 175 | * --routines |
||
| 176 | * |
||
| 177 | * @var bool |
||
| 178 | */ |
||
| 179 | private $routines; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Setup. |
||
| 183 | * |
||
| 184 | * @see \phpbu\App\Backup\Source |
||
| 185 | * @param array $conf |
||
| 186 | * @throws \phpbu\App\Exception |
||
| 187 | */ |
||
| 188 | 17 | public function setup(array $conf = []) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get tables and databases to backup. |
||
| 217 | * |
||
| 218 | * @param array $conf |
||
| 219 | */ |
||
| 220 | 17 | View Code Duplication | protected function setupSourceData(array $conf) |
| 227 | |||
| 228 | /** |
||
| 229 | * Execute the backup. |
||
| 230 | * |
||
| 231 | * @see \phpbu\App\Backup\Source |
||
| 232 | * @param \phpbu\App\Backup\Target $target |
||
| 233 | * @param \phpbu\App\Result $result |
||
| 234 | * @return \phpbu\App\Backup\Source\Status |
||
| 235 | * @throws \phpbu\App\Exception |
||
| 236 | */ |
||
| 237 | 4 | public function backup(Target $target, Result $result) : Status |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Restore the backup |
||
| 259 | * |
||
| 260 | * @param \phpbu\App\Backup\Target $target |
||
| 261 | * @param \phpbu\App\Backup\Restore\Plan $plan |
||
| 262 | * @return \phpbu\App\Backup\Source\Status |
||
| 263 | * @throws \phpbu\App\Exception |
||
| 264 | */ |
||
| 265 | 2 | public function restore(Target $target, Plan $plan): Status |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Create the Executable to run the mysqldump command. |
||
| 300 | * |
||
| 301 | * @param \phpbu\App\Backup\Target $target |
||
| 302 | * @return \phpbu\App\Cli\Executable |
||
| 303 | */ |
||
| 304 | 14 | protected function createExecutable(Target $target) : Executable |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Create backup status. |
||
| 335 | * |
||
| 336 | * @param \phpbu\App\Backup\Target $target |
||
| 337 | * @return \phpbu\App\Backup\Source\Status |
||
| 338 | */ |
||
| 339 | 4 | protected function createStatus(Target $target) : Status |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Cann compression be handled via pipe operator. |
||
| 358 | * |
||
| 359 | * @param \phpbu\App\Backup\Target $target |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | 14 | private function isHandlingCompression(Target $target) : bool |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Return dump target path. |
||
| 369 | * |
||
| 370 | * @param \phpbu\App\Backup\Target $target |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | 14 | private function getDumpTarget(Target $target) : string |
|
| 377 | } |
||
| 378 |