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 | * |
||
102 | * Lock tables option |
||
103 | * --lock-tables |
||
104 | * |
||
105 | * @var bool |
||
106 | */ |
||
107 | private $lockTables; |
||
108 | |||
109 | /** |
||
110 | * Use mysqldump with compression |
||
111 | * -C |
||
112 | * |
||
113 | * @var boolean |
||
114 | */ |
||
115 | private $compress; |
||
116 | |||
117 | /** |
||
118 | * Use mysqldump with extended insert |
||
119 | * -e |
||
120 | * |
||
121 | * @var boolean |
||
122 | */ |
||
123 | private $extendedInsert; |
||
124 | |||
125 | /** |
||
126 | * Dump blob fields as hex. |
||
127 | * --hex-blob |
||
128 | * |
||
129 | * @var boolean |
||
130 | */ |
||
131 | private $hexBlob; |
||
132 | |||
133 | /** |
||
134 | * Dump only table structures |
||
135 | * --no-data |
||
136 | * |
||
137 | * @var boolean |
||
138 | */ |
||
139 | private $noData; |
||
140 | |||
141 | /** |
||
142 | * Setup. |
||
143 | * |
||
144 | * @see \phpbu\App\Backup\Source |
||
145 | * @param array $conf |
||
146 | 5 | * @throws \phpbu\App\Exception |
|
147 | */ |
||
148 | 5 | public function setup(array $conf = []) |
|
149 | { |
||
150 | 5 | $this->setupSourceData($conf); |
|
151 | 5 | ||
152 | 5 | $this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump'); |
|
153 | 5 | $this->host = Util\Arr::getValue($conf, 'host'); |
|
154 | 5 | $this->user = Util\Arr::getValue($conf, 'user'); |
|
155 | 5 | $this->password = Util\Arr::getValue($conf, 'password'); |
|
156 | 5 | $this->hexBlob = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false); |
|
157 | 5 | $this->quick = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false); |
|
158 | 5 | $this->lockTables = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false); |
|
159 | 5 | $this->compress = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false); |
|
160 | 5 | $this->extendedInsert = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false); |
|
161 | $this->noData = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false); |
||
162 | $this->filePerTable = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false); |
||
163 | |||
164 | // this doesn't fail, but it doesn't work, so throw an exception so the user understands |
||
165 | if ($this->filePerTable && count($this->structureOnly)) { |
||
166 | throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option'); |
||
167 | 5 | } |
|
168 | } |
||
169 | 5 | ||
170 | 5 | /** |
|
171 | 5 | * Get tables and databases to backup. |
|
172 | 5 | * |
|
173 | 5 | * @param array $conf |
|
174 | */ |
||
175 | View Code Duplication | protected function setupSourceData(array $conf) |
|
176 | { |
||
177 | $this->tables = Util\Str::toList(Util\Arr::getValue($conf, 'tables')); |
||
178 | $this->databases = Util\Str::toList(Util\Arr::getValue($conf, 'databases')); |
||
179 | $this->ignoreTables = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables')); |
||
180 | $this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly')); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | 2 | * Execute the backup. |
|
185 | * |
||
186 | * @see \phpbu\App\Backup\Source |
||
187 | 2 | * @param \phpbu\App\Backup\Target $target |
|
188 | 2 | * @param \phpbu\App\Result $result |
|
189 | * @return \phpbu\App\Backup\Source\Status |
||
190 | 2 | * @throws \phpbu\App\Exception |
|
191 | */ |
||
192 | 2 | public function backup(Target $target, Result $result) |
|
211 | 3 | ||
212 | 3 | /** |
|
213 | 3 | * Create the Executable to run the mysqldump command. |
|
214 | 3 | * |
|
215 | 3 | * @param \phpbu\App\Backup\Target $target |
|
216 | 3 | * @return \phpbu\App\Cli\Executable |
|
217 | 3 | */ |
|
218 | 3 | public function getExecutable(Target $target) |
|
243 | |||
244 | /** |
||
245 | * Create backup status. |
||
246 | * |
||
247 | * @param \phpbu\App\Backup\Target |
||
248 | * @return \phpbu\App\Backup\Source\Status |
||
249 | */ |
||
250 | protected function createStatus(Target $target) |
||
266 | |||
267 | /** |
||
268 | * Cann compression be handled via pipe operator. |
||
269 | * |
||
270 | * @param \phpbu\App\Backup\Target $target |
||
271 | * @return bool |
||
272 | */ |
||
273 | private function isHandlingCompression(Target $target) |
||
277 | |||
278 | /** |
||
279 | * Return dump target path. |
||
280 | * |
||
281 | * @param \phpbu\App\Backup\Target $target |
||
282 | * @return string |
||
283 | */ |
||
284 | private function getDumpTarget(Target $target) |
||
288 | } |
||
289 |