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 | * User to connect with |
||
| 40 | * --user <username> |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $user; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Password to authenticate with |
||
| 48 | * --password <password> |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $password; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * List of tables to backup |
||
| 56 | * --tables array of strings |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $tables; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * List of databases to backup |
||
| 64 | * --databases array of strings |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $databases; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * List of tables to ignore |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $ignoreTables; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * List of tables where only the table structure is stored |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $structureOnly; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Table separated data files |
||
| 86 | * --tab |
||
| 87 | * |
||
| 88 | * @var boolean |
||
| 89 | */ |
||
| 90 | private $filePerTable; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Use mysqldump quick mode |
||
| 94 | * -q |
||
| 95 | * |
||
| 96 | * @var boolean |
||
| 97 | */ |
||
| 98 | private $quick; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Lock tables option |
||
| 102 | * --lock-tables |
||
| 103 | * |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | private $lockTables; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Single Transaction option |
||
| 110 | * --single-transaction |
||
| 111 | * |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | private $singleTransaction; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Use mysqldump with compression |
||
| 118 | * -C |
||
| 119 | * |
||
| 120 | * @var boolean |
||
| 121 | */ |
||
| 122 | private $compress; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Use mysqldump with extended insert |
||
| 126 | * -e |
||
| 127 | * |
||
| 128 | * @var boolean |
||
| 129 | */ |
||
| 130 | private $extendedInsert; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Dump blob fields as hex. |
||
| 134 | * --hex-blob |
||
| 135 | * |
||
| 136 | * @var boolean |
||
| 137 | */ |
||
| 138 | private $hexBlob; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Dump only table structures |
||
| 142 | * --no-data |
||
| 143 | * |
||
| 144 | * @var boolean |
||
| 145 | */ |
||
| 146 | 5 | private $noData; |
|
| 147 | |||
| 148 | 5 | /** |
|
| 149 | * Setup. |
||
| 150 | 5 | * |
|
| 151 | 5 | * @see \phpbu\App\Backup\Source |
|
| 152 | 5 | * @param array $conf |
|
| 153 | 5 | * @throws \phpbu\App\Exception |
|
| 154 | 5 | */ |
|
| 155 | 5 | public function setup(array $conf = []) |
|
| 156 | 5 | { |
|
| 157 | 5 | $this->setupSourceData($conf); |
|
| 158 | 5 | ||
| 159 | 5 | $this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump', ''); |
|
| 160 | 5 | $this->host = Util\Arr::getValue($conf, 'host', ''); |
|
| 161 | $this->user = Util\Arr::getValue($conf, 'user', ''); |
||
| 162 | $this->password = Util\Arr::getValue($conf, 'password', ''); |
||
| 163 | $this->hexBlob = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false); |
||
| 164 | $this->quick = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false); |
||
| 165 | $this->lockTables = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false); |
||
| 166 | $this->singleTransaction = Util\Str::toBoolean(Util\Arr::getValue($conf, 'singleTransaction', ''), false); |
||
| 167 | 5 | $this->compress = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false); |
|
| 168 | $this->extendedInsert = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false); |
||
| 169 | 5 | $this->noData = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false); |
|
| 170 | 5 | $this->filePerTable = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false); |
|
| 171 | 5 | ||
| 172 | 5 | // this doesn't fail, but it doesn't work, so throw an exception so the user understands |
|
| 173 | 5 | if ($this->filePerTable && count($this->structureOnly)) { |
|
| 174 | throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option'); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get tables and databases to backup. |
||
| 180 | * |
||
| 181 | * @param array $conf |
||
| 182 | */ |
||
| 183 | View Code Duplication | protected function setupSourceData(array $conf) |
|
| 190 | 2 | ||
| 191 | /** |
||
| 192 | 2 | * Execute the backup. |
|
| 193 | 1 | * |
|
| 194 | * @see \phpbu\App\Backup\Source |
||
| 195 | * @param \phpbu\App\Backup\Target $target |
||
| 196 | 1 | * @param \phpbu\App\Result $result |
|
| 197 | * @return \phpbu\App\Backup\Source\Status |
||
| 198 | * @throws \phpbu\App\Exception |
||
| 199 | */ |
||
| 200 | public function backup(Target $target, Result $result) : Status |
||
| 219 | 3 | ||
| 220 | 3 | /** |
|
| 221 | 3 | * Create the Executable to run the mysqldump command. |
|
| 222 | 3 | * |
|
| 223 | 5 | * @param \phpbu\App\Backup\Target $target |
|
| 224 | * @return \phpbu\App\Cli\Executable |
||
| 225 | */ |
||
| 226 | protected function createExecutable(Target $target) : Executable |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create backup status. |
||
| 253 | * |
||
| 254 | * @param \phpbu\App\Backup\Target |
||
| 255 | * @return \phpbu\App\Backup\Source\Status |
||
| 256 | */ |
||
| 257 | protected function createStatus(Target $target) : Status |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Cann compression be handled via pipe operator. |
||
| 276 | * |
||
| 277 | * @param \phpbu\App\Backup\Target $target |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | private function isHandlingCompression(Target $target) : bool |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return dump target path. |
||
| 287 | * |
||
| 288 | * @param \phpbu\App\Backup\Target $target |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function getDumpTarget(Target $target) : string |
||
| 295 | } |
||
| 296 |