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 |
||
| 23 | class Mysqldump extends Cli implements Source, Simulator |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Path to executable. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $pathToMysqldump; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Show stdErr |
||
| 34 | * |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | private $showStdErr; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Host to connect to |
||
| 41 | * --host <hostname> |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $host; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * User to connect with |
||
| 49 | * --user <username> |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $user; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Password to authenticate with |
||
| 57 | * --password <password> |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $password; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List of tables to backup |
||
| 65 | * --tables array of strings |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $tables; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * List of databases to backup |
||
| 73 | * --databases array of strings |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $databases; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * List of tables to ignore |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $ignoreTables; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * List of tables where only the table structure is stored |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $structureOnly; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Use mysqldump quick mode |
||
| 95 | * -q |
||
| 96 | * |
||
| 97 | * @var boolean |
||
| 98 | */ |
||
| 99 | private $quick; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * |
||
| 103 | * Lock tables option |
||
| 104 | * --lock-tables |
||
| 105 | * |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | private $lockTables; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Use mysqldump with compression |
||
| 112 | * -C |
||
| 113 | * |
||
| 114 | * @var boolean |
||
| 115 | */ |
||
| 116 | private $compress; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Use mysqldump with extended insert |
||
| 120 | * -e |
||
| 121 | * |
||
| 122 | * @var boolean |
||
| 123 | */ |
||
| 124 | private $extendedInsert; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Dump blob fields as hex. |
||
| 128 | * --hex-blob |
||
| 129 | * |
||
| 130 | * @var boolean |
||
| 131 | */ |
||
| 132 | private $hexBlob; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Dump only table structures |
||
| 136 | * --no-data |
||
| 137 | * |
||
| 138 | * @var boolean |
||
| 139 | */ |
||
| 140 | private $noData; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Dump target location |
||
| 144 | * @var string |
||
| 145 | */ |
||
| 146 | 5 | private $dumpPathname; |
|
| 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 = []) |
|
| 171 | 5 | ||
| 172 | 5 | /** |
|
| 173 | 5 | * Get tables and databases to backup. |
|
| 174 | * |
||
| 175 | * @param array $conf |
||
| 176 | */ |
||
| 177 | View Code Duplication | protected function setupSourceData(array $conf) |
|
| 184 | 2 | ||
| 185 | /** |
||
| 186 | * (non-PHPDoc) |
||
| 187 | 2 | * |
|
| 188 | 2 | * @see \phpbu\App\Backup\Source |
|
| 189 | * @param \phpbu\App\Backup\Target $target |
||
| 190 | 2 | * @param \phpbu\App\Result $result |
|
| 191 | * @return \phpbu\App\Backup\Source\Status |
||
| 192 | 2 | * @throws \phpbu\App\Exception |
|
| 193 | 1 | */ |
|
| 194 | public function backup(Target $target, Result $result) |
||
| 195 | { |
||
| 196 | 1 | // setup dump location and execute the dump |
|
| 197 | $this->dumpPathname = $target->getPathnamePlain(); |
||
| 198 | $mysqldump = $this->execute($target); |
||
| 199 | |||
| 200 | $result->debug($mysqldump->getCmd()); |
||
| 201 | |||
| 202 | if (!$mysqldump->wasSuccessful()) { |
||
| 203 | throw new Exception('mysqldump failed'); |
||
| 204 | } |
||
| 205 | 5 | ||
| 206 | return Status::create()->uncompressed($this->dumpPathname); |
||
| 207 | 5 | } |
|
| 208 | 3 | ||
| 209 | 3 | /** |
|
| 210 | 3 | * Create the Executable to run the mysqldump command. |
|
| 211 | 3 | * |
|
| 212 | 3 | * @param \phpbu\App\Backup\Target $target |
|
| 213 | 3 | * @return \phpbu\App\Cli\Executable |
|
| 214 | 3 | */ |
|
| 215 | 3 | public function getExecutable(Target $target) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Simulate the backup execution. |
||
| 239 | * |
||
| 240 | * @param \phpbu\App\Backup\Target $target |
||
| 241 | * @param \phpbu\App\Result $result |
||
| 242 | * @return \phpbu\App\Backup\Source\Status |
||
| 243 | */ |
||
| 244 | public function simulate(Target $target, Result $result) |
||
| 249 | } |
||
| 250 |