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 |
||
| 21 | class Mysqldump extends SimulatorExecutable implements Simulator |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Path to executable. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $pathToMysqldump; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Host to connect to |
||
| 32 | * --host <hostname> |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $host; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Port to connect to |
||
| 40 | * --port <port> |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $port; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * User to connect with |
||
| 48 | * --user <username> |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $user; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Password to authenticate with |
||
| 56 | * --password <password> |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $password; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * List of tables to backup |
||
| 64 | * --tables array of strings |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $tables; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * List of databases to backup |
||
| 72 | * --databases array of strings |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $databases; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * List of tables to ignore |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $ignoreTables; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * List of tables where only the table structure is stored |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $structureOnly; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Table separated data files |
||
| 94 | * --tab |
||
| 95 | * |
||
| 96 | * @var boolean |
||
| 97 | */ |
||
| 98 | private $filePerTable; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Use mysqldump quick mode |
||
| 102 | * -q |
||
| 103 | * |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | private $quick; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Lock tables option |
||
| 110 | * --lock-tables |
||
| 111 | * |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | private $lockTables; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Single Transaction option |
||
| 118 | * --single-transaction |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | private $singleTransaction; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Use mysqldump with compression |
||
| 126 | * -C |
||
| 127 | * |
||
| 128 | * @var boolean |
||
| 129 | */ |
||
| 130 | private $compress; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Use mysqldump with extended insert |
||
| 134 | * -e |
||
| 135 | * |
||
| 136 | * @var boolean |
||
| 137 | */ |
||
| 138 | private $extendedInsert; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Dump blob fields as hex. |
||
| 142 | * --hex-blob |
||
| 143 | * |
||
| 144 | * @var boolean |
||
| 145 | */ |
||
| 146 | private $hexBlob; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Dump only table structures |
||
| 150 | * --no-data |
||
| 151 | * |
||
| 152 | * @var boolean |
||
| 153 | */ |
||
| 154 | private $noData; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add general transaction id statement. |
||
| 158 | * --set-gids-purged |
||
| 159 | * |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | private $gtidPurged; |
||
| 163 | 13 | ||
| 164 | /** |
||
| 165 | 13 | * Setup. |
|
| 166 | * |
||
| 167 | 13 | * @see \phpbu\App\Backup\Source |
|
| 168 | 13 | * @param array $conf |
|
| 169 | 13 | * @throws \phpbu\App\Exception |
|
| 170 | 13 | */ |
|
| 171 | 13 | public function setup(array $conf = []) |
|
| 195 | 13 | ||
| 196 | 13 | /** |
|
| 197 | 13 | * Get tables and databases to backup. |
|
| 198 | 13 | * |
|
| 199 | * @param array $conf |
||
| 200 | */ |
||
| 201 | View Code Duplication | protected function setupSourceData(array $conf) |
|
| 208 | |||
| 209 | 4 | /** |
|
| 210 | * Execute the backup. |
||
| 211 | * |
||
| 212 | 4 | * @see \phpbu\App\Backup\Source |
|
| 213 | 1 | * @param \phpbu\App\Backup\Target $target |
|
| 214 | 1 | * @param \phpbu\App\Result $result |
|
| 215 | 1 | * @return \phpbu\App\Backup\Source\Status |
|
| 216 | * @throws \phpbu\App\Exception |
||
| 217 | */ |
||
| 218 | 4 | public function backup(Target $target, Result $result) : Status |
|
| 237 | 12 | ||
| 238 | 12 | /** |
|
| 239 | 12 | * Create the Executable to run the mysqldump command. |
|
| 240 | 12 | * |
|
| 241 | 12 | * @param \phpbu\App\Backup\Target $target |
|
| 242 | 12 | * @return \phpbu\App\Cli\Executable |
|
| 243 | 12 | */ |
|
| 244 | 12 | protected function createExecutable(Target $target) : Executable |
|
| 270 | 4 | ||
| 271 | 1 | /** |
|
| 272 | * Create backup status. |
||
| 273 | * |
||
| 274 | * @param \phpbu\App\Backup\Target |
||
| 275 | * @return \phpbu\App\Backup\Source\Status |
||
| 276 | 3 | */ |
|
| 277 | 1 | protected function createStatus(Target $target) : Status |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Cann compression be handled via pipe operator. |
||
| 296 | * |
||
| 297 | * @param \phpbu\App\Backup\Target $target |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | private function isHandlingCompression(Target $target) : bool |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return dump target path. |
||
| 307 | * |
||
| 308 | * @param \phpbu\App\Backup\Target $target |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | private function getDumpTarget(Target $target) : string |
||
| 315 | } |
||
| 316 |