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 |
||
24 | class Factory |
||
25 | { |
||
26 | /** |
||
27 | * Map of available sources, checks, syncs and cleanups. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $classMap = [ |
||
32 | // type |
||
33 | // alias => fqcn |
||
34 | 'adapter' => [ |
||
35 | 'array' => '\\phpbu\\App\\Adapter\\PHPArray', |
||
36 | 'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv', |
||
37 | 'env' => '\\phpbu\\App\\Adapter\\Env', |
||
38 | ], |
||
39 | 'runner' => [ |
||
40 | 'check' => '\\phpbu\\App\\Runner\\Backup\\Check', |
||
41 | 'cleaner' => '\\phpbu\\App\\Runner\\Backup\\Cleaner', |
||
42 | 'crypter' => '\\phpbu\\App\\Runner\\Backup\\Crypter', |
||
43 | 'source' => '\\phpbu\\App\\Runner\\Backup\\Source', |
||
44 | 'sync' => '\\phpbu\\App\\Runner\\Backup\\Sync', |
||
45 | ], |
||
46 | 'logger' => [ |
||
47 | 'json' => '\\phpbu\\App\\Log\\Json', |
||
48 | 'mail' => '\\phpbu\\App\\Log\\Mail', |
||
49 | 'webhook' => '\\phpbu\\App\\Log\\Webhook', |
||
50 | ], |
||
51 | 'source' => [ |
||
52 | 'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump', |
||
53 | 'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump', |
||
54 | 'mongodump' => '\\phpbu\\App\\Backup\\Source\\Mongodump', |
||
55 | 'mysqldump' => '\\phpbu\\App\\Backup\\Source\\Mysqldump', |
||
56 | 'pgdump' => '\\phpbu\\App\\Backup\\Source\\Pgdump', |
||
57 | 'redis' => '\\phpbu\\App\\Backup\\Source\\Redis', |
||
58 | 'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync', |
||
59 | 'tar' => '\\phpbu\\App\\Backup\\Source\\Tar', |
||
60 | 'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup', |
||
61 | ], |
||
62 | 'check' => [ |
||
63 | 'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin', |
||
64 | 'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent', |
||
65 | 'sizediffavgpercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent', |
||
66 | ], |
||
67 | 'crypter' => [ |
||
68 | 'mcrypt' => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt', |
||
69 | 'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL', |
||
70 | ], |
||
71 | 'sync' => [ |
||
72 | 'amazons3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
73 | 'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
74 | 'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2', |
||
75 | 'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox', |
||
76 | 'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp', |
||
77 | 'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive', |
||
78 | 'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync', |
||
79 | 'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp', |
||
80 | 'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer', |
||
81 | ], |
||
82 | 'cleaner' => [ |
||
83 | 'capacity' => '\\phpbu\\App\\Backup\\Cleaner\\Capacity', |
||
84 | 'outdated' => '\\phpbu\\App\\Backup\\Cleaner\\Outdated', |
||
85 | 'stepwise' => '\\phpbu\\App\\Backup\\Cleaner\\Stepwise', |
||
86 | 'quantity' => '\\phpbu\\App\\Backup\\Cleaner\\Quantity', |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Backup Factory. |
||
92 | * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects. |
||
93 | * |
||
94 | * @param string $type |
||
95 | * @param string $alias |
||
96 | * @throws \phpbu\App\Exception |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 24 | View Code Duplication | protected function create($type, $alias) |
110 | |||
111 | /** |
||
112 | * Runner Factory. |
||
113 | * |
||
114 | * @param string $alias |
||
115 | * @param bool $isSimulation |
||
116 | * @return mixed |
||
117 | * @throws \phpbu\App\Exception |
||
118 | */ |
||
119 | 2 | public function createRunner($alias, $isSimulation) |
|
128 | |||
129 | /** |
||
130 | * Adapter Factory. |
||
131 | * |
||
132 | * @param string $alias |
||
133 | * @param array $conf |
||
134 | * @throws \phpbu\App\Exception |
||
135 | * @return \phpbu\App\Adapter |
||
136 | */ |
||
137 | 6 | public function createAdapter($alias, $conf = []) |
|
147 | |||
148 | /** |
||
149 | * Logger Factory. |
||
150 | * |
||
151 | * @param string $alias |
||
152 | * @param array $conf |
||
153 | * @throws \phpbu\App\Exception |
||
154 | * @return \phpbu\App\Log\Logger |
||
155 | */ |
||
156 | 3 | public function createLogger($alias, $conf = []) |
|
169 | |||
170 | /** |
||
171 | * Create a backup target. |
||
172 | * |
||
173 | * @param \phpbu\App\Configuration\Backup\Target $conf |
||
174 | * @return \phpbu\App\Backup\Target |
||
175 | * @throws \phpbu\App\Exception |
||
176 | */ |
||
177 | public function createTarget(Configuration\Backup\Target $conf) |
||
188 | |||
189 | /** |
||
190 | * Source Factory. |
||
191 | * |
||
192 | * @param string $alias |
||
193 | * @param array $conf |
||
194 | * @throws \phpbu\App\Exception |
||
195 | * @return \phpbu\App\Backup\Source |
||
196 | */ |
||
197 | 2 | public function createSource($alias, $conf = []) |
|
207 | |||
208 | /** |
||
209 | * Check Factory. |
||
210 | * |
||
211 | * @param string $alias |
||
212 | * @throws \phpbu\App\Exception |
||
213 | * @return \phpbu\App\Backup\Check |
||
214 | */ |
||
215 | 4 | public function createCheck($alias) |
|
224 | |||
225 | /** |
||
226 | * Crypter Factory. |
||
227 | * |
||
228 | * @param string $alias |
||
229 | * @param array $conf |
||
230 | * @throws \phpbu\App\Exception |
||
231 | * @return \phpbu\App\Backup\Crypter |
||
232 | */ |
||
233 | 2 | public function createCrypter($alias, $conf = []) |
|
243 | |||
244 | /** |
||
245 | * Sync Factory. |
||
246 | * |
||
247 | * @param string $alias |
||
248 | * @param array $conf |
||
249 | * @throws \phpbu\App\Exception |
||
250 | * @return \phpbu\App\Backup\Sync |
||
251 | */ |
||
252 | 3 | public function createSync($alias, $conf = []) |
|
262 | |||
263 | /** |
||
264 | * Cleaner Factory. |
||
265 | * |
||
266 | * @param string $alias |
||
267 | * @param array $conf |
||
268 | * @throws \phpbu\App\Exception |
||
269 | * @return \phpbu\App\Backup\Cleaner |
||
270 | */ |
||
271 | 2 | public function createCleaner($alias, $conf = []) |
|
281 | |||
282 | /** |
||
283 | * Extend the backup factory. |
||
284 | * |
||
285 | * @param string $type Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner' |
||
286 | * @param string $alias Name the class is registered at |
||
287 | * @param string $fqcn Full Qualified Class Name |
||
288 | * @param boolean $force Overwrite already registered class |
||
289 | * @throws \phpbu\App\Exception |
||
290 | */ |
||
291 | 23 | View Code Duplication | public static function register($type, $alias, $fqcn, $force = false) |
301 | |||
302 | /** |
||
303 | * Throws an exception if type is invalid. |
||
304 | * |
||
305 | * @param string $type |
||
306 | * @throws \phpbu\App\Exception |
||
307 | */ |
||
308 | 30 | private static function checkType($type) |
|
316 | } |
||
317 |